By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,748 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,748 IT Pros & Developers. It's quick & easy.

filling out form and displaying errors

tolkienarda
100+
P: 316
hi all

I am working on a form valiadation for now i can tell people that they made a mistake filling out the form but my script to tell them which fields they missed isn't working. I think what my problem is that i am declaring session varabies using a variable in the brackets here is somthing similar to what i am doing
Expand|Select|Wrap|Line Numbers
  1. if ($a = 1)
  2. {
  3. $_SESSION[$i] = "text";
  4. $i++;
  5. }
  6.  
  7. if (b = 1)
  8. {
  9. $_SESSION[$i] = "text2";
  10. $i++;
  11. }
  12.  
I know i am getting inside my if statements without any problems but the setting of a session variable with a variable in the [] seems to be a problem.
here is the actual code i am using
Expand|Select|Wrap|Line Numbers
  1.     session_start();
  2.     $i=1;
  3.  
  4.     $ThanksURL = 'submitted.htm';
  5.     if (strlen(trim($_POST[first_name])) < 1)
  6.     {
  7.     $_SESSION[failed] = 1;
  8.     $_SESSION['$i'] = 'email';
  9.     $i++;
  10.     $ThanksURL= "registration.php";
  11.     }
  12.  
  13.     if (strlen(trim($_POST[first_name])) < 1)
  14.     {
  15.     $_SESSION[failed] = 1;
  16.     $_SESSION[$i] = 'First Name';
  17.     $i++;
  18.     $ThanksURL= "registration.php";
  19.     }
  20.  
  21.     if (strlen(trim($_POST[last_name])) < 1)
  22.     {
  23.     $_SESSION[failed] = 1;
  24.     $_SESSION[$i] = 'Last Name';
  25.     $i++;
  26.     $ThanksURL= "registration.php";
  27.     }
  28.  
  29.     if (strlen(trim($_POST[address])) < 1)
  30.     {
  31.     $_SESSION[failed] = 1;
  32.     $_SESSION[$i] = 'Address';
  33.     $i++;
  34.     $ThanksURL= "registration.php";
  35.     }
  36.  
  37.     if (strlen(trim($_POST[city])) < 1)
  38.     {
  39.     $_SESSION[failed] = 1;
  40.     $_SESSION[$i] = 'City';
  41.     $i++;
  42.     $ThanksURL= "registration.php";
  43.     }
  44.  
  45.     if (strlen(trim($_POST[state])) < 1)
  46.     {
  47.     $_SESSION[failed] = 1;
  48.     $_SESSION[$i] = 'State';
  49.     $i++;
  50.     $ThanksURL= "registration.php";
  51.     }
  52.  
  53.     if (strlen(trim($_POST[zip_code])) < 1)
  54.     {
  55.     $_SESSION[failed] = 1;
  56.     $_SESSION[$i] = 'Zip Code';
  57.     $i++;
  58.     $ThanksURL= "registration.php";
  59.     }
  60.  
  61.     if (strlen(trim($_POST[how_much_submit])) < 1)
  62.     {
  63.     $_SESSION[failed] = 1;
  64.     $_SESSION[$i] = 'Submission Amount';
  65.     $i++;
  66.     $ThanksURL= "registration.php";
  67.     }
  68.  
  69.  
and here is the code that spits out the results on my page
now i do start a session earlier in the script, i am getting into the if statement because it prints 'please fill in the fields'
Expand|Select|Wrap|Line Numbers
  1. if (isset($_SESSION[failed]))
  2. {
  3.     echo 'please fill in the &nbsp;';
  4.     $i = 1;
  5.     while (isset($_SESSION['$i']))
  6.     {
  7.         echo $_SESSION['$i'];
  8.         echo 'loop has been entered';
  9.         $i++;
  10.     }
  11.     echo 'fields';
  12. }                
  13.  
thanks for any thoughts or advice

eric
Feb 1 '07 #1
Share this Question
Share on Google+
5 Replies


Motoma
Expert 2.5K+
P: 3,235
You will need to quote the names inside the $_SESSION array:
[PHP]
$_SESSION[finished]; //Incorrect
$_SESSION['finished']; //Correct
[/PHP]

Also, do not single-quote inside when you are using a variable to iterate through:

[PHP]
$_SESSION['$i']; //Incorrect
$_SESSION[$i]; //Correct
[/PHP]
Feb 1 '07 #2

ronverdonk
Expert 2.5K+
P: 4,258
Is there some special reason for using the $_SESSION array for this? If I may, I like to suggest a much simpler approach: store the errors in an array and print out that array at the end. Like the following snippet for the first check. You can replace the other checks with similar code:
[php]
// initialize the errors array
$errors = array();

// now check it
$ThanksURL = 'submitted.htm';
if (strlen(trim($_POST[first_name])) < 1)
{
$errors[] = 'email';
$ThanksURL= "registration.php";
}[/php]
And at the end, where you spit out the $_SESSION errors, you use this code. It shows the errors in red.
[php] if ($errors) {
print '<span style="color:red"><ul><li><b>';
print implode('</b></li><li><b>',$errors);
print '</b></li></ul></span>';
}[/php]
Ronald :cool:
Feb 1 '07 #3

tolkienarda
100+
P: 316
sorry about the lack of clarity in my question but the print and check statements are on two different pages. I may however be able to move the check function to the print page not sure.
if i do then i have one question about your advice.
what does the implode function do, i don't doubt it will work just curious.

in regards to motoma's responce. i tries single quoting the $i and using it without quotes, i am not sure i understand your last section of code and its explination. will a double quote work when using a variable or should i leave it quote free.

thank you both
eric
Feb 1 '07 #4

Motoma
Expert 2.5K+
P: 3,235
sorry about the lack of clarity in my question but the print and check statements are on two different pages. I may however be able to move the check function to the print page not sure.
if i do then i have one question about your advice.
what does the implode function do, i don't doubt it will work just curious.

in regards to motoma's responce. i tries single quoting the $i and using it without quotes, i am not sure i understand your last section of code and its explination. will a double quote work when using a variable or should i leave it quote free.

thank you both
eric
You won't need quotes around the $i.
Feb 1 '07 #5

ronverdonk
Expert 2.5K+
P: 4,258
sorry about the lack of clarity in my question but the print and check statements are on two different pages. I may however be able to move the check function to the print page not sure.
if i do then i have one question about your advice.
what does the implode function do, i don't doubt it will work just curious.

in regards to motoma's responce. i tries single quoting the $i and using it without quotes, i am not sure i understand your last section of code and its explination. will a double quote work when using a variable or should i leave it quote free.

thank you both
eric
implode() joins array elements with a string, or as the PHP doc states:
string implode ( string glue, array pieces )

Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.
In your case it generates an unordered list of all error messages in the array.

Ronald :cool:
Feb 1 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.