Jump to content

Variable issue with a form


luckky300

Recommended Posts

I am stuck on trying to get the form to repopulate after a failed validation.  The real form involves REST calls and such but I trimmed the code back to this issue.  I think I need to declare the variables differently.  I also tried to use the variables in the PHP script to refill the form with the original form entries.  I have tried numerous ways of getting the variables to repopulate but could never get it to work consistently (such as declaring global variables.

 

Here is the form code:

<div>
<a name="error_field"></a>
<?php if($_GET['error']): ?>
        <div style="padding:10px;border:1px solid;color:#C70C0C;"><?php echo $_GET['error'] ?></div>
        <?php endif; ?>
        <div>
        <form method="post" action="process2.php">
        
<fieldset name="Group1">
<legend>Account Registration</legend>
<div id="field"><label id="Firstname">First Name:</label></div>
<div id="input"><input name="Firstname" id="Firstname" type="text" value="<?php $_POST['Firstname']?>"/></div>
<div id="field"><label id="Lastname">Last Name:</label></div>
<div id="input"><input name="Lastname" id="Lastname" type="text" value="<?php  $_POST['Lastname']?>"/></div>
<div id="field"><label id="Username">E-Mail Username):</label></div>
<div id="input"><input name="Username" type="text" value="<?= $_POST['Username']?>"/></div>
<div id="field"><label id="Password">Password:</label></div>
<div id="input"><input name="Password" type="password" /></div>
<div class="center" style="margin-top:15px;margin-bottom:15px">	
<input name="Submitx" type="submit" value="submit" />
<input name="Reset1" type="reset" value="reset" />
</div>								
</fieldset></form>
</div>			
</div>

 

and here is the PHP which I have on a different page

 

<?php
$new_user_username = check_input($_POST['Username'], "Enter your e-mail");
$new_user_password = check_input($_POST['Password'], "Enter your password");
$new_user_first_name = check_input($_POST['Firstname'], "Enter your first name");
$new_user_last_name = check_input($_POST['Lastname'], "Enter your last name");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $new_user_username))
{
    show_error("E-mail address not valid");
} else{ 
header('Location: TY.php');
}
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}
function show_error($myError)
{
header('Location: tp1.php?error=' . $myError);
exit();
}
?>

 

Thanks in advance for any help.

Link to comment
https://forums.phpfreaks.com/topic/230562-variable-issue-with-a-form/
Share on other sites

Archived

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

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