473,805 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

filling out form and displaying errors

tolkienarda
316 Contributor
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
5 1619
Motoma
3,237 Recognized Expert Specialist
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
4,258 Recognized Expert Specialist
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.p hp";
}[/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:re d"><ul><li><b>' ;
print implode('</b></li><li><b>',$er rors);
print '</b></li></ul></span>';
}[/php]
Ronald :cool:
Feb 1 '07 #3
tolkienarda
316 Contributor
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
3,237 Recognized Expert Specialist
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
4,258 Recognized Expert Specialist
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

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

Similar topics

5
4264
by: TG | last post by:
Dear PHP Group, I have two forms that are used to collect user information. The first one takes user inputted values such as fullname, city, address etc. I want these values to display in the second form when it is called. Both forms are .htm files that call themselves when the submit button is press via the following command in each form: <form method="post" action="<?php $server?>">
1
2385
by: Dave | last post by:
Hello, I've been waisting tons of time on this, so I'll just ask and hopefully someone will be able to help. I am opening an excel document and placing values into it's "letterhead" section (i have no problem doing this). The values that I am writing to the excel document come from a couple different tables.
20
8917
by: Peter E. Granger | last post by:
I'm having a strange problem (or at least it seems strange to me) trying to display a MessageBox in a VC++ .NET forms application. If I put the call to MessageBox::Show in the form's .h file, it works just fine. If I put the call in the .cpp file, I get the following two errors: error C2653: 'MessageBoxA': is not a class or namespace name error C2660: 'System::Windows::Forms::Control::Show': function does not take
0
977
by: Corobori | last post by:
I have got a form for data input. When the "Save" button is pressed I am doing the Insert thing in the database and depending on some data I have to either say "Ok, your ads is published"; or say "You need to pay $ssss for publishing your ad". In the situation when the user have to pay I have to submit some data to a cgi which will be displaying some form not under my control. I used to do it like this <form id="Form1" method="post"
3
1515
by: Fernando Rodríguez | last post by:
Hi, I'm writing code to validate fields in a form before saving to a db. All the validating functions are in a separate script which is required. All the validating functions add an error message to an array if the data doesn't validate. I check if something went wrong with count($theArray). Here's my code: ---------------------------------------------------------------------------
4
2266
by: rukkie | last post by:
After a redisplay of the form, caused by errors in other fields, the text which is in the "Observ" textarea is shifted (some 8 places) to the right (and this each time after a redisplay). The code used in case of redisplay is : <tr> <td align="right"> Observations : </td> <td>
3
2344
by: steve | last post by:
Hi All I have a VB.net 2005 App which has a form set as the Application splash screen in Project properties Another form is set as the startup form All works great until the splash screen closes or I close it to allow error messages to be displayed from Licence or Database checking errors
1
3333
by: chromis | last post by:
Hi, I'm having trouble fully implementing the edit section of a contact admin system, so far I have written the following: - Bean (Contact.cfc) - Data Access object (ContactDAO.cfc) - Gateway (ContactGateway.cfc) - index.cfm - Deals with the business logic - display/form.cfm - Produces the form for both add and edit behaviour
6
8808
by: emrodge | last post by:
I'm using Access 2007 on Windows Vista and am having some problems trying to configure a simple button. There are two tables called Clients and Contacts which are linked on clients.id and contacts.client_id. There are two forms associated with each table and I am trying to create a button to navigate from the Clients form to the Contacts form (which is a split form) but only to display the contacts where the current clients.id =...
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10614
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10369
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.