PDA

View Full Version : Php coding help



econstas
08-23-2006, 10:35 AM
All,
I would like to know where I could find a good tutorial for php. I learning this language..and I run into problems.

Probably easy:
$choice1= $HTTP_POST_VARS['choice1'];
$choice2= $HTTP_POST_VARS['choice2'];
$choice3= $HTTP_POST_VARS['choice3'];

$ch1=strlen($choice1);
$ch2=strlen($choice2);
$ch3=strlen($choice3);

if ($ch1 == 0 || $ch2 == 0 || $ch3 == 0)
{
echo "One of your fields is missing. Please use the Back Button to return to the form. Thank you!\n";
exit;
}

[/URL]

Basically I have created a form: http://www.holleyjohnson.net/Holidaymusic.html

I am working on the choices / time options for this form.. I would like to make it easier for down the road incase I need to add more choice options. I don't want to continously add if statements, becausethis code can get very long.

Ideas?

Thanks
Stratis
Website: [URL="http://www.holleyjohnson.net/"]www.holleyjohnson.net (http://www.holleyjohnson.net)

Andy128
08-23-2006, 01:47 PM
The problem I can see with this is that your customer will become frustrated with being given an error statement but no specific remedy. As you say- your list can be very long. If one statement is to cover 8 fields- then potentially- they could be given a generic error statement several times with out knowing exactly how to fix it.

I prefer echo error statements because they are easier than creating an entire page dedicated to one error. Additionally- I make them specific to each field.

Just a thought

Andy

navaldesign
08-23-2006, 06:50 PM
The echo statement only provides a blank page with the error message. Not very nice. What i think is needed here is:
1. A loop, that will look at array keys, to see which fields are related to the name "choice". This can easily be done by only considering the first 7 letters of the $key, and attributing an incrementing index to the variable. The second part inside the loop, will be the validation.
2. A second loop, for the second group of choices, of similar type.

Consequently, if a validation error is found, the error gets a number, equal to the index itself.
If you put the validation script in the beggining of the same page as the form, and the form calls itself as action, with a double if() statement, the first to see if the fields had already been filled in or not, and the second to check if there are any errors or not, and if there are, print ALL the errors in a pre-deternined text box, your work could be done with a very elegant and short code but you need to know some php to accomplish this.

Andy128
08-24-2006, 02:02 AM
Yeah- what he said.

econstas
08-24-2006, 08:14 AM
Yeah, I come from a PERL background. Do you know of any good places to learn some PHP? Tutorials / books ??

I was thinking of doing something similar on the option with loops to figure out If they are filled and not. If not, a value is given. Then at the end any values created will be printed onto the screen so the customer would know the problems. If all filled then onto the next step ...

navaldesign
08-24-2006, 08:51 AM
Yeah- what he said.

Hi Andy, this is what i mean:


http://www.navaldesign.info/Tips/selfsubmittingform.JPG


Particular conditions require particular soloutions. The validating part of script could be based in loops so if in the future he adds more options, it will automatically take them into account.

econstas
08-24-2006, 10:03 AM
Wow, I haven't seen flow charts since my programming days in school.. :) Its been awhile.

Andy128
08-24-2006, 03:26 PM
Navaldesign-
I am but a pup in the php field- I have lots to learn. Thanks for showing me another thing to learn.



Yeah- what he said

I was just being funny in that -it was way over my head.

I must disagree with the echo statement method though. Instead of a balnk page one could put the php script inside an html box and place it in the middle of a regular web page matching the theme of ones website. It doesn't have to be a blank page. I prefer this to having to make up several error pages. One draw back is that you cannot have a header re-direct as was recently discussed on this forum. Because the echo statement is output to the browser- if you then have a header redirect it gives an error. In that instance I have to use -
echo'<meta http-equiv="refresh" content="0; URL =location:http://whateverpage.com"/>';
which is not as clean as the header redirect.

again Navaldesign- thaks a bunch for all you help with php that you have given.

Andy

econstas
08-25-2006, 11:11 AM
I was able to incorporate some loops into the script. I like to look a script and break it down. Now, its basically piece by piece.. :(




$choice[1]= $HTTP_POST_VARS['choice1'];
$choice[2]= $HTTP_POST_VARS['choice2'];
$choice[3]= $HTTP_POST_VARS['choice3'];
$max=3;



for ( $counter = 1; $counter <= $max; $counter += 1){

if (strlen($ch[$counter]) == 0){
echo "test $ch[$counter] = false\n";
}else {
echo "test $ch[$counter] = true\n";
}
}


I got this part to work.. its very basic.. but it works for the time. I will have to look more into the code. I am thinking of adding something like:
every false statement create another array .e.

$false[$f]=" Field $choice[$choice] needs to be filled\n";
$f+=1;

Then set another for loop after all the work is done and run the loop that will print out all false statements. Thinking on the code itself.


Thanks again,
Stratis

navaldesign
08-25-2006, 03:09 PM
As said above, create another loop that will grab not only ALL the choices but all required fields as well. Th e key to this is that :
1. You get a -R f.e. in your field names: choice1-R where -R stands for required.
2. The script will look for the last two characters in the $key variable, so if a field is required, it will perform the validation, if not, it won't.
3. The same loop will create an array with all the required fields for which it has not found a value, as well as relevant error messages, so, according to the flow chart posted above, it will submit to itself, so it can then, when reloaded, grab again the variables, as well as the error messages, and display them in an appropriate textbox, or empty the fields and print the field titles in another colour.

davidundalicia
08-26-2006, 12:47 AM
Navaldesign:
I would love to see your coding for that elegant solution above !!

navaldesign
08-26-2006, 07:48 AM
I will prepare an example form in the next days.

davidundalicia
09-04-2006, 12:22 AM
Looking forward to seeing it naval...................

econstas
09-04-2006, 09:17 AM
I ended up getting the situation fixed.

Basic good goes like:

$name = $HTTP_POST_VARS['name'];
$email = $HTTP_POST_VARS['email'];
$cellphone = $HTTP_POST_VARS['cellphone'];
$venue_yes = $HTTP_POST_VARS['venueyes'];

# Array to have all sites checked.

$myarray[1]= $groupname;
$myarray[2]= $name;
$myarray[3]= $participant;
$myarray[4]= $cellphone;
# This array is set up to have individual Error Messages. I can cator each error to a different kind of message.

$charray[1]= "No Group Name";
$charray[2]= "No Contact Name";
$charray[3]= "How many are performing?";
$charray[4]= "No Contact Phone Number";


$max=4;
for ( $counter = 1; $counter <= $max; $counter += 1){

if ($myarray[$counter] == ""){
$e_array[$a]=$charray[$counter];
$a +=1;
}}

if ($e_array){
$esize=count($e_array);

print "<FONT=14>One of your coices for performing fields is missing. Please use the Back Button to return to the form. Thank you!</FONT><br>";

PRINT "----------------------------------------------------------<BR>";
for($ecount=1; $ecount<=$esize; $ecount +=1){
if ($e_array[$ecount]){
print "FAILED: $e_array[$ecount]<br>\n";
}
# else{ "PASSED##: $e_array[$ecount]<br>\n";}

}
exit;}

Then I had the script email me the results..

I would still like to see the idea that was mentioned earlier.

Thanks,
Stratis

navaldesign
09-07-2006, 10:41 AM
Hi Straris,

you might want to take a look at http://www.vodahost.com/vodatalk/forms/12077-form-help.html

regarding the use of loops for sending repeated fields as in your case.