Jump to content

Archived

This topic is now archived and is closed to further replies.

rbragg

maintaining field values during php validation

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]<?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
}[/code]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I don't know why but my onClicks just decided to start working!  ??? and  ;D

Share this post


Link to post
Share on other sites

×

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.