Jump to content

[SOLVED] how to validate this form


mraza

Recommended Posts

ok i did this please have a look at my code now i am getting undefined variables error for all of the variable i declared: please can you tell where i am wrong its not going to other pages:

 

<div id="form" style="margin: 90px;">
<?php 

if(isset($_POST['sub1'])){

$title = $_POST['title'];
$name = $_POST['name'];
$insurance = $_POST['insurance'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$email = $_POST['title'];
$registration = $_POST['registration'];
$comments = $_POST['comments'];

$error = "";

if (!$title)
$error = $error. "Title <br />";
if (!$name)
$error = $error. "Name <br />";
if (!$insurance)
$error = $error. "National Insurance Number <br />";
if (!$address)
$error = $error. "Address <br />";
if (!$phone)
$error = $error. "Telephone Number <br />";
if (!$email)
$error = $error. "Email Address <br />";
if (!$error !="")
  echo "Please Fill in The Following Required Fields <br /> $error";
  else {
  header('Location: payments.php');
  }
}elseif(isset($_POST['sub2'])){

$title = $_POST['title'];
$name = $_POST['name'];
$insurance = $_POST['insurance'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$registration = $_POST['registration'];
$comments = $_POST['comments'];

$error = "";

if (!$title)
$error = $error. "Title <br />";
if (!$name)
$error = $error. "Name <br />";
if (!$insurance)
$error = $error. "National Insurance Number <br />";
if (!$address)
$error = $error. "Address <br />";
if (!$phone)
$error = $error. "Telephone Number <br />";
if (!$email)
$error = $error. "Email Address <br />";
if (!$error !="")
  echo "<br /><br />Please Fill in The Following Required Fields <br /> $error";
  else {
  header('Location: confirmation.php');
  }
}
?>
<form action="" method="POST">
<table>
	<tr>
	<td align="right">
	Title:<span style="color:red;">*</span>
	</td>
	<td>
	   <input  style="border:1px solid #4088b8;" type="text" size="29" name="title" value="<?php echo "$title" ?>">
	</td>
	</tr>
	<tr>
	<td align="right">
	Name:<span style="color:red;">*</span>
	</td>
	<td>
	   <input  style="border:1px solid #4088b8;" type="text" size="29" name="name" value="<?php echo "$name" ?>">
	</td>
	</tr>
	<tr>
	<td align="right">
	National Insurance<br /> Number:<span style="color:red;">*</span>
	</td>
	<td>
	   <input  style="border:1px solid #4088b8;" type="text" size="29" name="insurance" value="<?php echo "$insurance" ?>">
	</td>
	</tr>
	<tr>
	<td align="right">
	Address:<span style="color:red;">*</span>
	</td>
	<td>
	   <input  style="border:1px solid #4088b8;" type="text" size="29" name="address" value="<?php echo "$address" ?>">
	</td>
	</tr>	
	<tr>
	<td align="right">
	Tleephone Number:<span style="color:red;">*</span>
	</td>
	<td>
	   <input  style="border:1px solid #4088b8;" type="text" size="29" name="phone" value="<?php echo "$phone" ?>">
	</td>
	</tr>
	<tr>
	<td align="right">
	Email:<span style="color:red;">*</span>
	</td>
	<td>
	   <input  style="border:1px solid #4088b8;" type="text" size="29" name="email" value="<?php echo "$email" ?>">
	</td>
	</tr>
	<tr>
	<td align="right">
	CSCS Registration Number:
	</td>
	<td>
	   <input  style="border:1px solid #4088b8;" type="text" size="29" name="registration" value="<?php echo "$registration" ?>">
	</td>
	</tr>
	<tr>
	<td align="right">
	Special Accommodations:
	</td>
	<td>
	   <textarea  style="border:1px solid #4088b8;" type="textarea" cols="30" rows="5" name="comments">
	   <?php echo "$comments" ?>
	   </textarea>
	</td>
	</tr>
	<tr><td> </td></tr>
	<tr><td> </td>
	<td><input type="submit" name="sub1" value="Payment" />
	<input type="submit" name="sub2" value="More Information"  /></td>
	</tr>
</table>
</form>
</div>

Undefined variable is a notice. It basic is saying that at some point in your code your call a variable that doesn't exist. In most cases this won't effect functionality. In your case you are only creating most of your variables if $_POST['sub1'] is set, meaning the form has been posted. This means the first time the page loads PHP is trying to echo out those values, but they don't have a value. In order to fix it you have two options, either declare the variables at the top of the page...

 

<?php
$title = "";
$name = "";
$insurance = "";
$address = ""];
$phone = "";
$email = "";
$registration = "";
$comments = "";
?>

 

Alternatively before echo'ing out the variable, check it isset...

 

<?php
<input  style="border:1px solid #4088b8;" type="text" size="29" name="title" value="<?php if(isset($title)) { echo "$title"; } ?>">
?>

 

Btw, this post looks awefully familiar...

cags on more problem  :( when its parsed to next page it is not indexing the variables. i added at the top of the next page:

 

<?php 

$title = $_POST['title'];
$name = $_POST['name'];
$insurance = $_POST['insurance'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$email = $_POST['title'];
$registration = $_POST['registration'];
$comments = $_POST['comments'];
?>

 

and try to echo out

echo 'Hello '. $name;

 

and also tried your method to print array of variables its displaying empty...  what i am missing now  ::)

 

Edit: the thing is if i use in form action the next page name its parsing correctly but as i mention earlier i am using two diffrent files to move to.. like if i write like this :

 

<form action="nextpage.php" method="POST">  and if i do self parsing for validation its not moving variables to next page.

If you mean after forwarding to an independ page then it wouldn't have passed the values on. If you are using the header() function to redirect then the $_POST array would be empty. You need to persist the data. The simplest way of doing this would probably be starting a session and saving them in the $_SESSION array.

At the top of both pages you would need to join a session (call session_start()). Then in the first page you would

 

$_SESSION['title'] = $_POST['title'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['insurance'] = $_POST['insurance'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['phone'] = $_POST['phone'];
$_SESSION['email'] = $_POST['title'];
$_SESSION['registration'] = $_POST['registration'];
$_SESSION['comments'] = $_POST['comments'];

 

In the second page you would then use the $_SESSION array to retrieve the values in much the same way you were trying to use the $_POST array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.