Jump to content

maintaining field values during php validation


rbragg

Recommended Posts

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.
Link to comment
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!
Link to comment
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]
Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.