Jump to content

keep user input in form after a submit


moran1409

Recommended Posts

hello all, i have a php form with multiple submits (one calculates price, one checks date and one submits the form and the data to mysql database)

when i hit calculate price, for example, the function works fine but all the fields are cleared and i would like for all the input to remain as the user sent.

here is part of the code (the form):

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

<fieldset>
<legend ><span><a>video</span></a></legend>
<ol>
<li>
<label for="hover_camera">hover camera</label>
<input id="hover_camera" type="radio" name="hover_camera" value="yes" /><b>yes</b>
<input id="hover_camera" type="radio" name="hover_camera" value="no"  /><b>no</b>
</li>
<li>
<label for="video_photographers">video photographers</label>
<input id="video_photographers" type="text" name="video_photographers" maxlength="1" size="1"/>
</li>
<li>
<label for="video_edit">video edit</label>
<input id="video_edit" type="radio" name="video_edit" value="short" /><b>short</b>
<input id="video_edit" type="radio" name="video_edit" value="long" /><b>long</b>
</li>
<li>
<label for="video_clip">video clip</label>
     <select name="video_clip">
         <option value="no">no</option>
         <option value="regular">regular</option>
	 <option value="staged">staged</option>
      </select>
</li>
</ol>
</fieldset><br />

<fieldset align="right">  
    <legend><span><a>stills</span></a></legend> 
<ol>
<li>
<label for="stills">stills</label>
<input id="stills" type="text" name="stills" maxlength="1" size="1"  />
</li>
<li>
<label for="increase">increase amount</label>
<input id="increase" type="text" name="increase" maxlength="4" size="1" />
</li>
<li>
<label for="magnets">magnets</label>
<input id="magnets" type="text" name="magnets" maxlength="4" size="1" />
</ol>
</fieldset><br />

<fieldset align="right">  
    <legend><span><a>albums</span></a></legend> 
<ol>
<li>
<label for="digital_album">digital album</label>
<input id="digital_album" type="radio" name="digital_album" value="yes" /><b>yes</b>
<input id="digital_album" type="radio" name="digital_album" value="no" /><b>no</b>
</li>
<li>
<label for="photo_album">photo album</label>
<input id="photo_album" type="radio" name="photo_album" value="yes" /><b>yes</b>
<input id="photo_album" type="radio" name="photo_album" value="no" /><b>no</b>
</li>
<li>
<label for="small_digital_album">small digital album</label>
<input id="small_digital_album" type="radio" name="small_digital_album" value="yes" /><b>yes</b>
<input id="small_digital_album" type="radio" name="small_digital_album" value="no" /><b>no</b>	
</li>
</ol>
</fieldset><br />

<fieldset align="right">
<ol>
<li>
<label for="comments">comments</label>																																																																																																																				
<textarea id="comments" name="comments"></textarea><br /><br />
</li>
<li>
<label for="price">price</label>
 <td><input type="submit" id = "price" name="price" value="calculate price" /></td>
</li>
</ol>
</fieldset><br />


<fieldset align="right">  
    <legend><span><a>choose date</span></a></legend> 
<ol>
<li>



      <input type="submit" id="isavailable" name="isavailable" value="isavailable"  />
  </li>
  </ol>
  </fieldset>
  
  	<fieldset class="submit">
<ol>
<li>
<input class="submit" type="submit" id="submit" name="submit" value="done!" />    <input class="submit" type="reset" id="reset" name="reset" value="reset" />
</li>
</ol>
</fieldset>  
  
</form>

 

thanks for the help

Link to comment
Share on other sites

It's called making a form 'sticky'. You basically check to see if the form field has a corresponding value present in the $_POST array, and if so, echo it as the fields value/use it as the indicator for checked/selected, depending on the field type. Here's an example I keep handy of a form with text field validation, that is sticky so the user can make any necessary corrections. Look it over and see how it works.

 

<?php
if( $_POST['submitted'] == 'yes' ) { //check for hidden field value to indicate form has been submitted
$errors = array(); // initialize an array to hold validation errors
$_POST = array_map('trim', $_POST); // trim all $_POST array values

if( !empty($_POST['name']) ) { // validate the name field
	if( !ctype_alpha($_POST['name']) ) {
		$errors[] = 'Name must be alphabetic characters only.'; // if name has non alpha chars, store error
	}
	if( strlen($_POST['name']) < 3 || strlen($_POST['name'] > 20) ) {
		$errors[] = 'Name must be from 3 to 20 characters.'; // if name has too many/few chars, store error
	}
} else {
	$errors[] = 'Name is a required field.'; // if name is empty, store error
}

if( !empty($_POST['number']) ) { // same validations as in name, above.
	if( !ctype_digit($_POST['number']) ) {
		$errors[] = 'Number must be numeric.';
	}
	if( strlen($_POST['number']) < 5 || strlen($_POST['number']) > 20 )  {
		$errors[] = 'Number must be from 5 to 20 digits. It is currently ' . strlen($_POST['number']) . ' digits';
	}
} else {
	$errors[] = 'Number is a required field.';
}

if( !empty($errors) ) {  // if the $errors array is not empty, display the errors to allow the user to correct them and resubmit the form
	echo "<font color=\"red\">The following errors were detected";
	echo implode("<br>\n", $errors)";
	echo '</font>';
}
}
?>
<form method="post">
Name (3-20 letters): <input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>"><br>
Number (5-10 numbers): <input type="text" name="number" value="<?php echo isset($_POST['number']) ? $_POST['number'] : ''; ?>"><br>
<input type="hidden" name="submitted" value="yes">
<input type="submit" name="submit" value="Submit">
</form>

Link to comment
Share on other sites

Pikachu's response is spot on.  However, as a budding developer myself, I feel compelled to inform you on other tools.  Sometimes even though your favorite tool is capable of pounding a nail into a board, it's better practice to just grab a hammer!

 

Though sticky forms will work, take a peek into AJAX.  The content is more dynamic, and allows the client's experience to be much for fruitful and pleasant.  If you're not afraid to expand your coding horizon, take a look [=http://www.ibm.com/developerworks/library/x-ajaxxml9/]here[/url], and you can see all the nifty ways that this little tool can help you develop your site.

 

Good luck!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.