Jump to content

repopulate after failed server side validation


simpli

Recommended Posts

Hi,

I have a form that submits to itself for validation. I have the js validation that works fine but in case js is absent I have the server side validation set up. How could I ensure that after validation the form is showed with the fields it had so  the user doesn't have to start over? I have radio buttons, input texts and select boxes in the mix.

Thanks for pointing toward a solution.

J-R

echo
'<input value="'.$_REQUEST['username'].'" name="username" />'.
'<select name="dropdown">'.
    '<option value="1" '.(($_REQUEST['dropdown'] == '1')?'selected="selected"':FALSE).'>1</option>'.
    '<option value="2" '.(($_REQUEST['dropdown'] == '1')?'selected="selected"':FALSE).'>2</option>'.
'</select>'.
'<input type="radio" name="rdo" '.(($_REQUEST['rdo'] == TRUE)?'checked="checked"':FALSE).'/>';

Another similar way that shows you where to put the validation etc.

 

Post the information to the same page. Put an if statement at the top to check if the form is submitted and validate it in there. Then underneath have the form with if statements to echo the values of the forms if they were set.

 

Quick e.g. with a few different types of inputs in the form:

 

<?php

  if ($_POST['send']=="Send")
  {
     //validate here
  }

?>

<form action="" method="post">

  <input type="text" name="something" value="<?php if(isset($_POST['something'])){echo $_POST['something'];} ?>" />

  <input type="radio" name="radiobutton" value="yes" <?php if($_POST['radiobutton']=="yes"){echo ' checked="checked" ';}?>/>
  <input type="radio" name="radiobutton" value="no" <?php if($_POST['radiobutton']=="no"){echo ' checked="checked" ';}?>/>

  <select name="test">

  <option value="1"<?php if($_POST['test']=="1"){echo ' selected="selected" ';} ?>>1</option>
  <option value="2"<?php if($_POST['test']=="2"){echo ' selected="selected" ';} ?>>2</option>

  </select>

</form>

 

(Obviously you don't have to open and close the <?php ?> like I have - I did it to make it easier to understand)

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.