Retaining field values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Retaining field values

Post by emilcarlo »

Good Morning,

Code: Select all

if (empty($location)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}
The following code checks if the field is empty, and then reloads the page add_client.php it is. I have multiple similar code since I have many fields (23 fields I am correct, therefore 23 similar statements o.O). My problem is, if one field is found empty, all fields are cleared out, requiring the user to re-input everything else. Is there possible way for me to retain field values should the user missed to input one of the fields? I am new to the industry, and I am willing to learn much about php programming. All your inputs and suggestions are well appreciated. Thank you very much :)
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Retaining field values

Post by JakeJ »

First of all, it seems strange that you're combining javascript and php validation together. I'm not quite sure why.

Assuming you're posting a form to a different php page, the answer to your question is to use sessions. There's a ton of good tutorials on the internet and it's pretty easy to implement.

I use sessions when validating my forms since not all validation can be done on the client side, I just did it all server side though in retrospect that was a mistake.

In any case, when you require server side validation, use sessions, it's much easier.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: Retaining field values

Post by SidewinderX »

Is there something wrong with just setting the value of the form element to the value of the request?

Code: Select all

<input type="text" name="first" value="<?php echo $_POST['first']; ?>" />
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Retaining field values

Post by JakeJ »

I guess I'm not really clear on what your work flow is here.

If you're posting a form and then using those values to fill in fields on another form, than what you just proposed works just fine (almost, but I'll get to that in a moment).

If you're posting to another page and returning to the original form, what you've proposed doesn't work at all since the code you're posting won't be returned back to the page.

Please fill me in on your work flow so I can answer your question better.

As for using $_POST['first'] as the value for your form, it works, but it's not a good practice. I always sanitize my POST variables first before allowing them to be inserted in to another form or for that matter, a database. Use mysql_real_escape_string() for starters along with preg_match() or even preg_replace() and regular expressions.
emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Re: Retaining field values

Post by emilcarlo »

Hi Jake,

Thank you for your time in reading my post. The idea actually was taken from the sample php page that was given to me by a friend. Here's where the idea came:

Code: Select all

<?php
include("dbconnection.php");

if(isset($_POST["btnSubmit"]))
{

	$id = $_POST["id"];
	$date = $_POST["date"];
	$status = $_POST["status"];
	$territory = $_POST["territory"];
	$job_title = $_POST["job_title"];
	$area_of_work = $_POST["area_of_work"];
	$employer = $_POST["employer"];
	$department = $_POST["department"];
	$location = $_POST["location"];
	$date_posted = $_POST["date_posted"];
	$closing_date = $_POST["closing_date"];
	$gender = $_POST["gender"];
	$first_name = $_POST["first_name"];
	$last_name = $_POST["last_name"];
	$telephone = $_POST["telephone_number"];
	$title = $_POST["title"];
	$address_1 = $_POST["address_1"];
	$address_2 = $_POST["address_2"];
	$address_3 = $_POST["address_3"];
	$city = $_POST["city"];
	$country = $_POST["country"];
	$postal_code = $_POST["postal_code"];
	$website = $_POST["website"];
	$email_address = $_POST["email_address"];
	$comment_by_cg = $_POST["cg_comment"];
	$date_emailed = $_POST["date_emailed"];
	$mailing_comments= $_POST["mailing_comments"];
	$telesales_comments= $_POST["telesales_comments"];
	$time= $_POST["time"];
	

	$query = "INSERT INTO records (date, status, territory, job_title, area_of_work, employer, department, location, date_posted, closing_date, gender, first_name, last_name, telephone_number, title, address_1, address_2, address_3, city, country, postal_code, website, email_address, cg_comment, date_emailed, mailing_comments, telesales_comments, time) VALUES ('".$date."','".$status."','".$territory."', '".$job_title."', '".$area_of_work."', '".$employer."', '".$department."', '".$location."', '".$date_posted."', '".$closing_date."', '".$gender."', '".$first_name."', '".$last_name."', '".$telephone_number."', '".$title."', '".$address_1."', '".$address_2."', '".$address_3."', '".$city."', '".$country."', '".$postal_code."', '".$website."', '".$email_address."', '".$cg_comment."', '".$date_emailed."', '".$mailing_comments."', '".$telesales_comments."', '".$time."')";
	
	mysql_query($query) or die(mysql_error());
	
	echo "<script>
		alert('You have successfully added a record');
		window.location = 'add_client.php';
	</script>";
}
?>
Due to the page requirements, I needed to modify his code (I do apologize if I am not doing it very good, I am a real noob in the industry). What happens is, I need to reload the page should the user left a field blank. However, all my attempts leave me from just displaying a message and clicking the back button to return from the page. So when I try to browse through his code, I modified it by applying the if statement. It is indeed very long, but for a newbie like me, that's the solution I found since my friend didn't want me to be spoon fed.

Any suggestions? The whole code is here:

Code: Select all

<link href="add_client.css" rel="stylesheet" type="text/css">

<script type="text/javascript">

function alpha(e) {
     var k;
     document.all ? k = e.keyCode : k = e.which;
     return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32);
}

function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

function dtval(d,e) {
var pK = e ? e.which : window.event.keyCode;
if (pK == 8) {d.value = substr(0,d.value.length-1); return;}
var dt = d.value;
var da = dt.split('/');
for (var a = 0; a < da.length; a++) {if (da[a] != +da[a]) da[a] = da[a].substr(0,da[a].length-1);}
if (da[0] > 31) {da[1] = da[0].substr(da[0].length-1,1);da[0] = '0'+da[0].substr(0,da[0].length-1);}
if (da[1] > 12) {da[2] = da[1].substr(da[1].length-1,1);da[1] = '0'+da[1].substr(0,da[1].length-1);}
if (da[2] > 9999) da[1] = da[2].substr(0,da[2].length-1);
dt = da.join('/');
if (dt.length == 2 || dt.length == 5) dt += '/';
d.value = dt;
}

</script>

<?php
include("dbconnection.php");

if(isset($_POST["btnSubmit"]))
{

	$id = $_POST["id"];
	$date = $_POST["date"];
	$status = $_POST["status"];
	$territory = $_POST["territory"];
	$job_title = $_POST["job_title"];
	$area_of_work = $_POST["area_of_work"];
	$employer = $_POST["employer"];
	$department = $_POST["department"];
	$location = $_POST["location"];
	$date_posted = $_POST["date_posted"];
	$closing_date = $_POST["closing_date"];
	$gender = $_POST["gender"];
	$first_name = $_POST["first_name"];
	$last_name = $_POST["last_name"];
	$telephone = $_POST["telephone_number"];
	$title = $_POST["title"];
	$address_1 = $_POST["address_1"];
	$address_2 = $_POST["address_2"];
	$address_3 = $_POST["address_3"];
	$city = $_POST["city"];
	$country = $_POST["country"];
	$postal_code = $_POST["postal_code"];
	$website = $_POST["website"];
	$email_address = $_POST["email_address"];
	$comment_by_cg = $_POST["cg_comment"];
	$date_emailed = $_POST["date_emailed"];
	$mailing_comments= $_POST["mailing_comments"];
	$telesales_comments= $_POST["telesales_comments"];
	$time= $_POST["time"];
	
if (empty($location)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";

}

if (empty($area_of_work)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($job_title)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($employer)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($department)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($date_posted)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($closing_date)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($first_name)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($last_name)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($title)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($address_1)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($address_2)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

if (empty($address_3)) {
	echo "<script>
		alert('Please fill out necessary fields');
		window.location = 'add_client.php';
	</script>";
}

else
		
	$query = "INSERT INTO records (date, status, territory, job_title, area_of_work, employer, department, location, date_posted, closing_date, gender, first_name, last_name, telephone_number, title, address_1, address_2, address_3, city, country, postal_code, website, email_address, cg_comment, date_emailed, mailing_comments, telesales_comments, time) VALUES ('".$date."','".$status."','".$territory."', '".$job_title."', '".$area_of_work."', '".$employer."', '".$department."', '".$location."', '".$date_posted."', '".$closing_date."', '".$gender."', '".$first_name."', '".$last_name."', '".$telephone_number."', '".$title."', '".$address_1."', '".$address_2."', '".$address_3."', '".$city."', '".$country."', '".$postal_code."', '".$website."', '".$email_address."', '".$cg_comment."', '".$date_emailed."', '".$mailing_comments."', '".$telesales_comments."', '".$time."')";
	
	mysql_query($query) or die(mysql_error());
	
	echo "<script>
		alert('You have successfully added a record');
		window.location = 'add_client.php';
	</script>";
}


?>

<table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><table width="760" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="199" align="center" valign="top"><a href="login.html"><img src="image.gif" alt="" width="152" height="58" border="0" /></a></td>
        <td width="176" align="right" valign="bottom"><a href="main.php"><img src="Home.jpg" width="104" height="20" border="0"/></a></td>
        <td width="130" align="right" valign="bottom"><a href="view_client.php"><img src="View.jpg" width="104" height="20" border="0"/></a></td>
        <td width="146" align="right" valign="bottom"><img src="Add.jpg" width="104" height="20" border="0"/></td>
        <td width="109" align="right" valign="bottom">&nbsp;</td>
        </tr>

    </table></td>
  </tr>
  <tr>
    <td><table width="760" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="200" height="3"  bgcolor="#1B1C78"><img src="images/topspacerblue.gif" alt="" width="1" height="3" /></td>
        <td width="560"  bgcolor="#0076CC"><img src="images/topspacerlblue.gif" alt="" width="1" height="3" /></td>

      </tr>
    </table></td>

  </tr>
  <tr>
    <td height="526" align="center" valign="top" bgcolor="#F3FAFE"><p>&nbsp;</p>
      <form name="form" method="post" action="add_client.php">
      <table width="679" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="160" height="26" align="left" valign="middle" scope="col">Date and Time</td>
        <td width="160" height="26" align="center" valign="middle" scope="col">
		<?php
        $date = date("d/m/y");
		print date("d/m/y |", time()) 
		?>
		<?php
 		print date("h:i a", time()) 
		?></td>
        <td width="39" height="26" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="26" colspan="2" align="left" valign="middle" scope="col">Status</td>
        <td height="26" colspan="2" align="center" valign="middle" scope="col">NEW RECORD</td>
      </tr>
      <tr>
        <td width="160" align="left" valign="middle" scope="col">Territory</td>
        <td width="160" height="27" align="center" valign="middle" scope="col"><select name="territory" width="230" style="width: 143px" id="territory">
      <option selected="selected" disabled="disabled">Select Territory</option>
      <option>Territory 1</option>
      <option>Territory 2</option>
      <option>Territory 3</option>
      <option>Territory 4</option>
      <option>Territory 5</option>
      <option>Territory 6</option>
      <option>Territory 7</option>
      <option>Territory 8</option>
      <option>Territory 9</option>
      <option>Territory 10</option>
    </select></td>
        <td width="39" height="27" align="left" valign="top" scope="col">&nbsp;</td>
        <td colspan="2" align="left" valign="middle" scope="col">Location </td>
        <td height="27" colspan="2" align="center" valign="middle" scope="col"><input name="location" type="text" id="location" onkeypress="return alpha(event)"/></td>
      </tr>
      <tr>
        <td width="160" align="left" valign="middle" scope="col">Area of Work</td>
        <td width="160" height="26" align="center" valign="middle" scope="col"><input name="area_of_work" type="text" id="area_of_work" onkeypress="return alpha(event)"/></td>
        <td width="39" height="26" align="left" valign="top" scope="col">&nbsp;</td>
        <td colspan="2" align="left" valign="middle" scope="col">Job Title</td>
        <td height="26" colspan="2" align="center" valign="middle" scope="col"><input name="job_title" type="text" id="job_title" onkeypress="return alpha(event)"/></td>
      </tr>
      <tr>
        <td width="160" align="left" valign="middle" scope="col">Employer</td>
        <td width="160" height="27" align="center" valign="middle" scope="col"><input name="employer" type="text" id="employer" onkeypress="return alpha(event)"/></td>
        <td width="39" height="27" align="left" valign="top" scope="col">&nbsp;</td>
        <td colspan="2" align="left" valign="middle" scope="col">Department</td>
        <td height="27" colspan="2" align="center" valign="middle" scope="col"><input name="department" type="text" id="department" onkeypress="return alpha(event)"/></td>
      </tr>
      <tr>
        <td width="160" align="left" valign="middle" scope="col">Date Posted<br />
          (dd/mm/yyyy)</td>
        <td width="160" height="26" align="center" valign="middle" scope="col"><input name="date_posted" type="text" id="date_posted" onkeyup="dtval(this,event)" maxlength="10"/></td>
        <td width="39" height="26" align="left" valign="top" scope="col">&nbsp;</td>
        <td colspan="2" align="left" valign="middle" scope="col">Closing Date<br />
          (dd/mm/yyyy)</td>
        <td height="26" colspan="2" align="center" valign="middle" scope="col"><input name="closing_date" type="text" id="closing_date" onkeyup="dtval(this,event)" maxlength="10"/></td>
      </tr>
      <tr>
        <td width="160" height="27" align="left" valign="middle" scope="col">&nbsp;</td>
        <td width="160" height="27" align="center" valign="middle" scope="col">&nbsp;</td>
        <td width="39" height="27" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="left" valign="middle" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="center" valign="middle" scope="col">&nbsp;</td>
      </tr>
      <tr>
        <td width="160" height="26" align="left" valign="middle" scope="col">First Name</td>
        <td width="160" height="26" align="center" valign="middle" scope="col"><input name="first_name" type="text" id="first_name" onkeypress="return alpha(event)"/></td>
        <td width="39" height="26" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="26" colspan="2" align="left" valign="middle" scope="col">Last Name</td>
        <td height="26" colspan="2" align="center" valign="middle" scope="col"><input name="last_name" type="text" id="last_name" onkeypress="return alpha(event)"/></td>
      </tr>
      <tr>
        <td width="160" height="27" align="left" valign="middle" scope="col">Title</td>
        <td width="160" height="27" align="center" valign="middle" scope="col"><input name="title" type="text" id="title" onkeypress="return alpha(event)"/></td>
        <td width="39" height="27" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="left" valign="middle" scope="col">Telephone Number</td>
        <td height="27" colspan="2" align="center" valign="middle" scope="col"><input name="telephone_number" type="text" id="telephone_number" onKeyPress="return numbersonly(this, event)"/></td>
      </tr>
      <tr>
        <td width="160" height="27" align="left" valign="middle" scope="col">Address 1</td>
        <td width="160" height="27" align="center" valign="middle" scope="col"><input name="address_1" type="text" id="address_1" /></td>
        <td width="39" height="27" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="left" valign="middle" scope="col">Address 2</td>
        <td height="26" colspan="2" align="center" valign="middle" scope="col"><input name="address_2" type="text" id="address_2" /></td>
      </tr>
      <tr>
        <td width="160" height="26" align="left" valign="middle" scope="col">Address 3</td>
        <td width="160" height="26" align="center" valign="middle" scope="col"><input name="address_3" type="text" id="address_3" /></td>
        <td width="39" height="26" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="26" colspan="2" align="left" valign="middle" scope="col">City</td>
        <td height="27" colspan="2" align="center" valign="middle" scope="col"><input name="city" type="text" id="city" onkeypress="return alpha(event)"/></td>
      </tr>
      <tr>
        <td width="160" height="27" align="left" valign="middle" scope="col">Country</td>
        <td width="160" height="27" align="center" valign="middle" scope="col"><input name="country" type="text" id="country" onkeypress="return alpha(event)"/></td>
        <td width="39" height="27" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="left" valign="middle" scope="col">Postal Code</td>
        <td height="26" colspan="2" align="center" valign="middle" scope="col"><input name="postal_code" type="text" id="postal_code" onKeyPress="return numbersonly(this, event)"/></td>
      </tr>
      <tr>
        <td width="160" align="left" valign="middle" scope="col">&nbsp;</td>
        <td width="160" height="26" align="center" valign="middle" scope="col">&nbsp;</td>
        <td width="39" height="26" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="26" colspan="2" align="left" valign="middle" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="center" valign="middle" scope="col">&nbsp;</td>
      </tr>
      <tr>
        <td width="160" align="left" valign="middle" scope="col">Website</td>
        <td width="160" height="27" align="center" valign="middle" scope="col"><input name="website" type="text" id="website" /></td>
        <td width="39" height="27" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="left" valign="middle" scope="col">Email Address</td>
        <td height="26" colspan="2" align="center" valign="middle" scope="col"><input name="email_address" type="text" id="email_address" /></td>
      </tr>
      <tr>
        <td width="160" align="left" valign="middle" scope="col">&nbsp;</td>
        <td width="160" height="27" align="center" valign="middle" scope="col">&nbsp;</td>
        <td width="39" height="27" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="left" valign="middle" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="center" valign="middle" scope="col">&nbsp;</td>
      </tr>
      <tr>
        <td width="160" align="left" valign="middle" scope="col">CG Comment</td>
        <td width="160" height="26" align="center" valign="middle" scope="col"><input name="cg_comment" type="text" id="cg_comment" /></td>
        <td width="39" height="26" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="26" colspan="2" align="left" valign="middle" scope="col">Date Emailed<br />
          (dd/mm/yyyy)</td>
        <td height="26" colspan="2" align="center" valign="middle" scope="col"><input name="date_emailed" type="text" id="date_emailed" onkeyup="dtval(this,event)" maxlength="10"/></td>
      </tr>
      <tr>
        <td width="160" align="left" valign="middle" scope="col">Mailing Comment</td>
        <td width="160" height="27" align="center" valign="middle" scope="col"><input name="mailing_comment" type="text" id="mailing_comment" /></td>
        <td width="39" height="27" align="left" valign="top" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="center" valign="middle" scope="col">&nbsp;</td>
        <td height="27" colspan="2" align="center" valign="middle" scope="col">&nbsp;</td>
      </tr>
      <tr>
        <td align="left" valign="middle" scope="col">Telesales Comment</td>
        <td height="27" align="center" valign="middle" scope="col"><input name="telesales_comment" type="text" id="telesales_comment" /></td>
        <td height="27" align="left" valign="middle" scope="col">&nbsp;</td>
        <td width="49" height="27" align="center" valign="middle" scope="col"><a href="add_client.php"><img src="Delete.png" width="27" height="27" border="0"/></a></td>
        <td width="111" align="left" valign="middle" scope="col">Clear Fields</td>
        <td width="53" height="27" align="center" valign="middle" scope="col">
        <input type="image" src="Add.png" height="27" width="27" name="btnSubmit" id="btnSubmit" value="Add Record"/></td>
        <td width="107" align="left" valign="middle" scope="col">Add Record</td>
      </tr>
      <tr>
        <td height="27" colspan="7" align="left" valign="middle" scope="col"><input name="status" type="text" width="10" id="status" style="visibility:hidden" value="New Record"/>
          <input name="date" type="text" width="10" id="date" style="visibility:hidden" value="<?php $date = date("d/m/y");
		print date("d/m/y |", time()) 
		?>"/>
          <input name="time" type="text" width="10" id="time" style="visibility:hidden" value="<?php print date("h:i a", time())?>"/></td>
        </tr>
      </table>
    </form></p></td>

  </tr>
  <tr>
    <td height="38"><table width="760" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="200" height="35" align="center"  bgcolor="#1B1C78" class=white><img src="images/topspacerblue.gif" alt="" width="1" height="3" />&nbsp;<a href="disclaimer.html"><font color="#FFFFFF">Legal Disclaimer</font></a> </td>

        <td width="560" align="center"  bgcolor="#0076CC"  class=white><img src="images/topspacerlblue.gif" alt="" width="1" height="3" />&nbsp;Copyright &copy; 2006 - 2010 Limited. All rights reserved.
</td>
      </tr>

    </table></td>
  </tr>
</table>
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Retaining field values

Post by JakeJ »

There's two ways to handle that. Server side with PHP or client side with javascript. In your case, since you're just testing for a blank field, you should do it on the client side in javascript.

Go look up some tutorials on form validation with javascript. You'll learn a lot more that way than from me trying to give you an explanation here. But it's really not that hard.

The concept is, when you hit the submit button, javascript fires a function that looks for blank fields, issues a warning and then cancels the submit.

If you have trouble getting the form validation worked out after getting through a tutorial, post the code in a new thread (in the javscript section) and we'll be happy to help.
emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Re: Retaining field values

Post by emilcarlo »

I actually have a post.. but yeah, I wrongly posted it at PhP forum xD but here's the link viewtopic.php?f=1&t=119772

Thanks Jake.
PradeepKr
Forum Newbie
Posts: 14
Joined: Wed Aug 11, 2010 8:29 am

Re: Retaining field values

Post by PradeepKr »

Though client side validation is good to have but is never a substitute for server side validation.
Both should be there in my views. If not, then server side is must. So, I think this thread being under PHP should be acceptable.

Coming to the solution,
You must be having the <form name="" action=""....> on the same page for sure since you want to redirect back to it after submitting(if user input is not ok).
Put the captured value(from post/get) in the "value" attribute of the input fields under this form. How?

Code: Select all

<input name="someting" value="<?php if( isset($_POST["someting"])) echo $_POST["someting"]; ?>" >
Do this for all inputs and you'll see that the user gets his/her entered values even after redirection.
Post Reply