rbragg Posted October 13, 2006 Share Posted October 13, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/23899-maintaining-field-values-during-php-validation/ Share on other sites More sharing options...
dymon Posted October 13, 2006 Share Posted October 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/23899-maintaining-field-values-during-php-validation/#findComment-108620 Share on other sites More sharing options...
rbragg Posted October 16, 2006 Author Share Posted October 16, 2006 Thanks for your reply. For instance, if I had a form: [code]<form name="bc" method="POST" action="<?php echo $_SESSION['PHP_SELF'] ?>">[/code]and I had a textfield:[code]<input type="text" name="fname" id="fname" size="30">[/code]... 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. Quote Link to comment https://forums.phpfreaks.com/topic/23899-maintaining-field-values-during-php-validation/#findComment-109502 Share on other sites More sharing options...
dymon Posted October 16, 2006 Share Posted October 16, 2006 If I understand ritght you want to fill the correct field:[code]<input type="text" name="fname" id="fname" size="30" value="<?=$_POST['fname']?>">[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23899-maintaining-field-values-during-php-validation/#findComment-109510 Share on other sites More sharing options...
rbragg Posted October 16, 2006 Author Share Posted October 16, 2006 I did try that option and it did not work. However, Reli4nt at another messageboard steered me toward a functioning solution:[code]<?php$sticky_fname = (isset($_POST['fname']))? ' value = "' . $_POST['fname'] . '" ':' '; # maintain user entries echo '<input type="text" ' . $sticky_fname . ' name="fname" />'; # textfield?>[/code]I do thank you for your help! Quote Link to comment https://forums.phpfreaks.com/topic/23899-maintaining-field-values-during-php-validation/#findComment-109519 Share on other sites More sharing options...
rbragg Posted October 17, 2006 Author Share Posted October 17, 2006 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:[b]JS -[/b][code]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 } }[/code][b]cash radiobutton - [/b][code]<?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 ?> [/code][b]speed textfield -[/b][code]<?php $sticky_speed = (isset($_POST['speed']))? ' value = "' . $_POST['speed'] . '" ':' '; # maintain entry echo '<input type="text" ' . $sticky_speed . ' name="speed" />'; # textfield?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23899-maintaining-field-values-during-php-validation/#findComment-110195 Share on other sites More sharing options...
Ninjakreborn Posted October 17, 2006 Share Posted October 17, 2006 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[code]<?phpif ($_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 wantif ($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}[/code]after your php is totally working, go back and put in the javascript, if you know javascript you shouldn't have a problem. Quote Link to comment https://forums.phpfreaks.com/topic/23899-maintaining-field-values-during-php-validation/#findComment-110204 Share on other sites More sharing options...
rbragg Posted October 17, 2006 Author Share Posted October 17, 2006 Yes, I have all of my php validation done and working with an include ... over 200 lines worth. lol.Separately, I [b]had [/b] 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. Quote Link to comment https://forums.phpfreaks.com/topic/23899-maintaining-field-values-during-php-validation/#findComment-110227 Share on other sites More sharing options...
rbragg Posted October 18, 2006 Author Share Posted October 18, 2006 I don't know why but my onClicks just decided to start working! ??? and ;D Quote Link to comment https://forums.phpfreaks.com/topic/23899-maintaining-field-values-during-php-validation/#findComment-110672 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.