Jump to content


Photo

maintaining field values during php validation


  • Please log in to reply
8 replies to this topic

#1 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 13 October 2006 - 08:41 PM

When a page containing a form is being posted to itself for php validation, how do I get the accepted field values to maintain themselves on the page even if an error is found elsewhere? Is there a simple way to do this?  ???

Thanks in advance!

#2 dymon

dymon
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 13 October 2006 - 09:05 PM

Hi,

can you provide the code you are useing now? And explain on that code what you want to do, in this way it will be easier to help.
Web is the future ... desktop will die ...

#3 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 16 October 2006 - 01:26 PM

Thanks for your reply. For instance, if I had a form:

<form name="bc" method="POST" action="<?php echo $_SESSION['PHP_SELF'] ?>">

and I had a textfield:

<input type="text" name="fname" id="fname" size="30">

... and I had an error elsewhere, like the last name (lname) was left empty. I want the fname value to repost in the textfield because it was entered correctly by the user.

#4 dymon

dymon
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 16 October 2006 - 01:45 PM

If I understand ritght you want to fill the correct field:

<input type="text" name="fname" id="fname" size="30" value="<?=$_POST['fname']?>">

Web is the future ... desktop will die ...

#5 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 16 October 2006 - 02:05 PM

I did try that option and it did not work. However, Reli4nt at another messageboard steered me toward a functioning solution:

<?php
$sticky_fname = (isset($_POST['fname']))? ' value = "' . $_POST['fname'] . '" ':' '; # maintain user entries 
echo '<input type="text" ' . $sticky_fname . ' name="fname" />';		     # textfield
?>

I do thank you for your help!

#6 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 17 October 2006 - 06:46 PM

Solving this problem has unfortunately led to another. Since my form objects are now within php (server-side) my JavaScript onClick functions can no longer work.  :'(

Here is an example:

JS -

function clear_account()
    {
    	for (var i = 0; i < document.bc.rbBill.length; i++)  //if one of these radiobuttons are chosen
	 	{
        	  document.bc.speed.value = ""; //empty value of speed textfield
		 }
	}

cash radiobutton -

<?php
 $sticky_checked = ( (isset($_POST['rbBill'])) && ($_POST['rbBill'] == 'cash') )?' checked="checked" ':' '; # maintain entry
 echo '<input name="rbBill" type="radio" value="cash" onClick="clear_account();" ' . $sticky_checked . '/>'; # radiobutton 
?>

speed textfield -

<?php
 $sticky_speed = (isset($_POST['speed']))? ' value = "' . $_POST['speed'] . '" ':' '; # maintain entry
  echo '<input type="text" ' . $sticky_speed . ' name="speed" />'; # textfield
?>


#7 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 17 October 2006 - 06:59 PM

Keep it simple.
FIrst get your php down, then get your javascript down.
Normally javascript won't interfere with the php after the php is working properly.
Now you have the form
<?php
if ($_POST['status'] == "yes") {
$errorhandler = "";
if ($_POST['firstname'] == "") {
$errorhandler .= "Firstname was left blank.<br />";
}
if ($_POST['lastname'] == "") {
$errorhandler .= "Lastname was left blank.<br />";
}
if ($_POST['email'] == "") {
$errorhandler .= "The email was left blank.<br />";
}
if ($_POST['email'] != $_POST['verifyemail']) {
$errorhandler .= "The emails don't match.<br />";
}
// then validate email dns, and email with regex if you want

if ($errorhandler != "") {
echo "<span style=\"color:red;\">";
echo $errorhandler;
echo "</span>";
}
if ($errorhandler == "") {
$show = "no";
// do database work and everything else
 }

}
if ($show != "no") {
?>

<form name="register" id="register" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="firstname">First Name</label>
<input name="firstname" id="firstname" type="text" maxlength="120" />
<label for="lastname">Last Name</label>
<input name="lastname" id="lastname" type="text" maxlength="120" />
<label for="email">Email</label>
<input name="email" id="email" type="text" maxlength="120" />
<label for="verifyemail">Verify Email</label>
<input name="verifyemail" id="verifyemail" type="text" maxlength="120" />
<input type="hidden" name="status" value="yes" />
</form>
<?php
}

after your php is totally working, go back and put in the javascript, if you know javascript you shouldn't have a problem.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#8 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 17 October 2006 - 07:32 PM

Yes, I have all of my php validation done and working with an include ... over 200 lines worth. lol.

Separately, I had onClick javascript functions that worked fine when my form objects were done with html. Now that my form objects are done using php (to maintain user entries after validation), those functions do not work ... I think because they are being called within server-side script.

#9 rbragg

rbragg
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts

Posted 18 October 2006 - 02:04 PM

I don't know why but my onClicks just decided to start working!  ??? and  ;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users