Jump to content

[SOLVED] new to php, need help with forms


meomike2000

Recommended Posts

A am new to php and have been trying to create a form to collect information. I can get the form to post and can even validate the input, the problem comes when there is a failure in validation, how do you get the form to reload and display the error that needs to be fixed. All I seem to be able to do is let the script die and display the error.

 

Here is a sample form that I have created to test this on:

 

<html>

<head></head>

<body>

<?php

//check if form has been submitted

if (!$_POST['submit'])

{

//if not display form

?>

<form method="POST" action="<?=$_SERVER['PHP_SELF']?>">

Select from test1:

<select name="state">

<option value="test1.1">test1.1</option>

<option value="test1.2">test1.2</option>

<option value="test1.3">test1.3</option>

</select><br />

<br />

Select from test2:

<select name="city">

<option value="test2.1">test2.1</option>

<option value="test2.2">test2.2</option>

<option value="test2.3">test2.3</option>

</select><br />

<br />

Select from test3:

<select name="cat">

<option value="test3.1">test3.1</option>

<option value="test3.2">test3.2</option>

<option value="test3.3">test3.3</option>

</select><br />

Please enter a number between 1 and 10:

<input type="text" name="num" size="2">

<br />

<input type="submit" name="submit" value="Select">

</form>

<?php

}

else

{

//form has been submitted

//items selected, check that number was entered,

$num = (!isset($_POST['num']) || trim($_POST['num']) == " " || !is_numeric($_POST['num']))

? die ('ERROR: Please enter a number between 1 and 10') : trim($_POST['num']);

//check number range

if ($num < 1 || $num >10)

{

die ('ERROR: The number must be between 1 and 10');

}

//if so, display them

$state = $_POST['state'];

$city = $_POST['city'];

$cat = $_POST['cat'];

echo 'Here is your selections: <br />';

echo "<i>$state</i><br />";

echo "<i>$city</i><br />";

echo "<i>$cat</i><br />";

}

?>

 

</body>

</html>

 

any help with this would be greatly appreciated......

mike

Link to comment
https://forums.phpfreaks.com/topic/142014-solved-new-to-php-need-help-with-forms/
Share on other sites

Save the submitted values in a cookie (or session variable), then use a redirect to redirect the user back to the original page. Populate the form using the values that are saved in the cookie.

 

Note: DONT do this with password fields - the password becomes visible in the HTML and can be a security risk.

Ok so you already have a script which validates etc.

 

Ideally you have one script: user.php (or something),

 

This will be layed out something like:

 

<?php

// Check wether to show form or validate and log in user.

if(isset($_POST['form_data'])){
   
   // Check if there has been any errors etc, if there has: display the form with the errors.
}else{
   // Display the form.
}

?>

 

Now you could just have the html code for the form doubled in the script (so you have two copies), this is bad practice and can be easily sorted with a simple function.

 

<?php

Function DisplayForm($errors=null){

$error_string = $errors;	// You could use $error_strring = implode("<br />",$errors); if you want to save performance by using an array instead.

echo('
	'.$error_string.'<Br />
	  <form method="POST" action="'.$_SERVER['PHP_SELF'].'">
	  Select from test1:
		 <select name="state">
			<option value="test1.1">test1.1</option>
			<option value="test1.2">test1.2</option>
			<option value="test1.3">test1.3</option>
		 </select><br />
		 <br />
	  Select from test2:
		 <select name="city">
			<option value="test2.1">test2.1</option>
			<option value="test2.2">test2.2</option>
			<option value="test2.3">test2.3</option>
		 </select><br />
		 <br />
	  Select from test3:
		 <select name="cat">
			<option value="test3.1">test3.1</option>
			<option value="test3.2">test3.2</option>
			<option value="test3.3">test3.3</option>
		 </select><br />
	  Please enter a number between 1 and 10:
		 <input type="text" name="num" size="2">
		 <br />         
	  <input type="submit" name="submit" value="Select">
	  </form>
');
}

?>

 

To put this into perspective, you would modify your original code like so:

 

<html>
<head></head>
<body>
<?php

Function DisplayForm($errors=null){

$error_string = $errors;	// You could use $error_strring = implode("<br />",$errors); if you want to save performance by using an array instead.

echo('
	'.$error_string.'<Br />
	  <form method="POST" action="'.$_SERVER['PHP_SELF'].'">
	  Select from test1:
		 <select name="state">
			<option value="test1.1">test1.1</option>
			<option value="test1.2">test1.2</option>
			<option value="test1.3">test1.3</option>
		 </select><br />
		 <br />
	  Select from test2:
		 <select name="city">
			<option value="test2.1">test2.1</option>
			<option value="test2.2">test2.2</option>
			<option value="test2.3">test2.3</option>
		 </select><br />
		 <br />
	  Select from test3:
		 <select name="cat">
			<option value="test3.1">test3.1</option>
			<option value="test3.2">test3.2</option>
			<option value="test3.3">test3.3</option>
		 </select><br />
	  Please enter a number between 1 and 10:
		 <input type="text" name="num" size="2">
		 <br />         
	  <input type="submit" name="submit" value="Select">
	  </form>
');
}

//check if form has been submitted
if (!$_POST['submit'])
{
   //if not display form
   DisplayForm();
}
else
{
   //form has been submitted
   
   $error = null; // Declare it so we dont get those notices 'undeclared variable' blah blah
   
//check number range
if (isset($_POST['num']) && is_numeric($_POST['num']) && $_POST['num'] < 1 || $_POST['num'] >10)
{
	$error .= 'ERROR: The number must be between 1 and 10';
}

// If there was an error, show it.
if($error != null){
	DisplayForm($error);
}else{
	// Otherwise carry on?
	$state = $_POST['state'];
	$city = $_POST['city'];
	$cat = $_POST['cat'];
		echo 'Here is your selections: <br />';
		echo "<i>$state</i><br />";
		echo "<i>$city</i><br />";
		echo "<i>$cat</i><br />";
}
}
?>

</body>
</html>

 

I hope this helps,

i only had to change the validation part of your script to:

 

//check number range

if (!isset($_POST['num']) || !is_numeric($_POST['num']))

{

$error .='ERROR: Please enter a number between 1 and 10';

}

elseif ($_POST['num'] < 1 || $_POST['num'] > 10)

{

$error .='ERROR: The number must be between 1 and 10';

}

//if there was an error, show it.

 

 

from:

 

//check number range

  if (isset($_POST['num']) && is_numeric($_POST['num']) && $_POST['num'] < 1 || $_POST['num'] >10)

  {

      $error .= 'ERROR: The number must be between 1 and 10';

  }

 

 

if i used the validation that you show here i could still leave the number input blank it would pass validation......

 

but still thanks for the help...

 

now what if i wanted to make a selection in the first box affect my choices in the second box.

 

say i wanted the first box to let me choose a state, well then my second box would be

to choose a city within that state.

 

how would something like that look.....

 

can somebody please help with that.....

 

thanks mike

 

I have  read a book called "how to do everything with php&mysql" by Vakram Vaswani.

 

very good book, explains most of the basics and gives good examples, that is how i learned how to write the script above and how to use validation to check the input.

 

i will look at the site that you suggest, but i do how a good understanding of the basics.

 

thanks for the info and the link.

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.