Page 1 of 1

Questoin related to Php Form

Posted: Tue Apr 18, 2006 11:58 pm
by lhua059
hi there, i need to write a single php file which would analyse the form data(the form is within the same php file) such as ID, Car Registration number etc.. i need to validate the user data too, if it is all vaid then display the summary page about the user data, if the user data is invalid, i need to re-display the form again with the correct data staying in the field and empty those filed with invalid data, so my question is how i can display the form inside if(valid){} can i make call to the form? Any help would be appreciated!!

Code: Select all

<html> 
        $studentID=$_Post['stuID']; 
        $stuGender =    $_Post['gender']; 

        if(studentid not in a valid format){ 
             display the form again with the correct gender showing in the field 
             and the stuID field should be empty here, 
             i'm not quite sure how to do here as i m new to php 
        } 

        <h1>Tamaki online car park booking system</h1> 
        <form action="comboform.php" method="post"> 
          <p>Please enter your AUID: 
          <input type="text" name="stuID" value="<?php echo $_POST['stuID']?>"> 
          <input type="text" name="gender" value="<?php echo $_POST['gender']?>"> 
          <input type="submit" value="Submit"> 
        </form> 
      <?php 
      } 
    ?> 
  </body> 
</html>

Posted: Wed Apr 19, 2006 12:44 am
by tristanlee85
I'm not sure how you want to validate the student ID, but here is a scenerio where the student life the ID field empty. I used a switch() statement because I was just working on it for my page. So, I'm sure there is probably an easier way to do this, but this works.

Code: Select all

<?php
$studentID=$_POST['stuID'];
$stuGender=$_POST['gender'];

switch($_GET['action'])
{
   case "post":
   if (empty($studentID))
	{
	//Displays the form if the student ID isn't valid
	echo "<i>You did not enter in a user ID</i><br>
	<h1>Tamaki online car park booking system</h1>
	<form action=\"comboform.php?action=post\" method=\"post\">
	Please enter your AUID:
	<input type=\"text\" name=\"stuID\"><br>
	Please enter your gender:
	<input type=\"text\" name=\"gender\">
	<input type=\"submit\" value=\"Submit\">
	</form>";
   	die();
   	}
   else
   	{
   //Displays the fields on the page
	echo "<h1>Tamaki online car park booking system</h1>
	Student ID: $studentID<br>
	Gender: $stuGender";
	die();
	}
   break;
}
	//Displays the form
	echo "<h1>Tamaki online car park booking system</h1>
	<form action=\"comboform.php?action=post\" method=\"post\">
	Please enter your AUID:
	<input type=\"text\" name=\"stuID\"><br>
	Please enter your gender:
	<input type=\"text\" name=\"gender\">
	<input type=\"submit\" value=\"Submit\">
	</form>";
   	die();
?>
Here is how it works: http://65.186.87.93/fedex1/comboform.php

code

Posted: Wed Apr 19, 2006 3:06 am
by haimiyahya
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<html>
   
   <h1>Tamaki online car park booking system</h1>
   
     <?php
      
        if(isset($_POST['stuID']) & isset($_POST['gender'])){
          $studentID = $_POST['stuID'];
          $stuGender = $_POST['gender'];
        }
        
        $stuGender = toLower($stuGender);
    
        if(!is_numeric($studentID) | !($stuGender == 'male' | $stuGender == 'female' )){ 
        // i think this should be okey if student_id only consist of digit
             print '<form action="'.$_SERVER['PHP_SELF'].'" method="post">
                    <p>Please enter your AUID:
                    <input type="text" name="stuID" value="'.$_POST['stuID'].'">
                    <input type="text" name="gender" value="'.$_POST['gender'].'">
                    <input type="submit" value="Submit">
                  </form>';
        }
        else{
          print "Student id: ".$studentID."<br />";
          print "Gender: ".$stuGender."<br />";
        }        
    ?>
  </body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Apr 19, 2006 8:53 pm
by lhua059
Thank you guys for the reply. i sort of fix the problem with displaying the form again when invalid data is entered ..Bascially what i program does is that getting the student to enter their student ID and their car registraton number, and choose from within the next five following day(counting from one day after the form being submited successfuly) eg if today is the 20th April, then there will be five check boxes with the date(21 April,22 April.....25 April) as the label the student can choose from . The student ID field must be 7 digits, and the registration field either 2 Upper case letter followed by 1 to 4 digitss or 3 UPPER CASE letter followed by 1 to 3 digits. below is a brief summy of my program.


Code: Select all

<?php
	if(count($_POST)>0){
			
			$AUID=$_POST['stuID'];
			$REGO=$_POST['regNO'];
			
			
			//check the validity of the student ID
			if(!preg_match("/^(\d{7})$/",$AUID)){
						$IDNotValid=true; $field="AUID";
						$AUID = "";
			}
			
			//check the validity of the registration number
			if(!(preg_match("/^[A-Z]{2}[0-9]{1,4}$/",$REGO)||preg_match("/^[A-z]{3}[0-9]{1,3}$/",$REGO))){
							$RegNotValid=true; $field="Registration Number";
							$REGO="";
			}
			
			if($IDNotValid){
			
			//	display the form again,the stuID field would be empty in this case but with the correct registration number
			//in the regNO field in the form 		
				echo " some form data  here" 
			}
			
			if($RegNotValid){
			// display the form again,the regNO field would be empty in this case but with the correct Student ID in the stuID field
				echo "some form data here" 
			}


	}
	else{?>
		<html>
			<body>
				<p>Welcome To Tamaki Online Car Park Booking System</p>
				<form action="comboform.php" method="post" />
				<input type="text" name="stuID" value="<?php echo $_POST['stuID'] ?>">
				<input type="text"  name="regNO" value="<?php echo $_POST['regNO']?>">
				<input type="checkbox" name="dates[]" value="day1">
					.
					.
					.
				<input type="checkbox" name="dates[]" value="day5">
			</body>
		</html>

    }

?>

My question is when invalid info is provided , the form should keep displaying until the student have provided all the valid information. but how can i refer to the correct info entered previously by the student? thanks in advance

Posted: Wed Apr 19, 2006 9:13 pm
by RobertGonzalez
You need a few steps.

1. First, check to make sure the form is posted.
2. Set your post vars to vars to be used for testing.
3. Create a var called $processData and set it to false.
4. Create another var called $validCounter and set it to 0.
5. Check each of the form inputs and read them into vars. If they validate, add 1 to $validCounter. If the don't, blank the var.
6. Knowing the number of vars you are checking, after the complete processing of the data, if the $validCounter is the same count as your post vars, then you are golden, process the data. If it is not, echo the form with all of the post vars filled into the fields. The good vars will echo out and the bad vars will be blank because your validation procedures emptied the bad var.

This is a little drawn out, I know. But give it a shot and see what it does for you.

Re: Questoin related to Php Form

Posted: Thu Apr 20, 2006 4:23 am
by Zythan
Hello lhua059,

Take a look here and tell me if this is what you are trying to do.

http://zfutura.free.fr/fillform/inscriptfive.php

Regards.

Zythan

Posted: Thu Apr 20, 2006 4:55 am
by lhua059
cheers everah for the advice...there is question which come to my mind...as when the student enter the incorrect info at the first time. the form data would be displayed again asking the student to enter the correct ID or registration again..so my question is when the submit button is pressed the second time, will be post data be updated as well?

Re: Questoin related to Php Form

Posted: Thu Apr 20, 2006 6:27 am
by Zythan
Hello,

Your form data should be 'updated' if you pass the default value.

Code: Select all

<p>Name:<font color=red>*</font><br>
<input name="Name" type="text" id="Name" size="50" value="<? echo $Name; ?>">
</p>
HTH

Zythan

Posted: Thu Apr 20, 2006 8:43 am
by RobertGonzalez
lhua059 wrote:cheers everah for the advice...there is question which come to my mind...as when the student enter the incorrect info at the first time. the form data would be displayed again asking the student to enter the correct ID or registration again..so my question is when the submit button is pressed the second time, will be post data be updated as well?
Updated is probably not the best word. You data will be retained because you set it as a default. If you are only dealing with two fields you can probably do away with the validation counter, but you might still want to use the check value. Try something like this...

Code: Select all

<?php
$process_form = false; //Put this here as a sort of global check

if ( isset($_POST['submit']) ) { // Checks the submit button
	$AUID=$_POST['stuID'];
	$REGO=$_POST['regNO'];
	
	//check the validity of the student ID
	if(!preg_match("/^(\d{7})$/",$AUID)) {
		$AUID = "";
	}
				   
	//check the validity of the registration number
	if(!(preg_match("/^[A-Z]{2}[0-9]{1,4}$/",$REGO)||preg_match("/^[A-z]{3}[0-9]{1,3}$/",$REGO))){
		$REGO="";
	}
				   
	if ( !empty($AUID) && !empty($REGO) ) {
		$process_form = true;
	}
	
}

if ($process_form) {
	// Do what you do with a good form submit
}
else { ?>
	<html>
		<body>
				<p>Welcome To Tamaki Online Car Park Booking System</p>
				<form action="comboform.php" method="post" />
				<input type="text" name="stuID" value="<?php echo $AUID; ?>">
				<input type="text"  name="regNO" value="<?php echo $REGO; ?>">
				<input type="checkbox" name="dates[]" value="day1">
						.
						.
						.
				<input type="checkbox" name="dates[]" value="day5">
		</body>
	</html>
<?php
}
?>