【问题标题】:Set html text box value from php variable从 php 变量设置 html 文本框值
【发布时间】:2014-04-24 01:39:56
【问题描述】:

我正在尝试根据会话数据为 html 注册表单中的文本框填写默认“值”字段。

如果用户在我的注册表单上犯了任何错误,它会将他们发回,我希望尽可能多地填写他们的数据,而不是将他们发送回普通表单以重新开始。

这是我的处理程序脚本检查:

// Let's get the text from the form,.

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$password_check = mysql_real_escape_string($_POST['pwcheck']);
$email = mysql_real_escape_string($_POST['email']);
$spam_check = mysql_real_escape_string($_POST['checkit']); // spam check
$IP = $_SERVER['REMOTE_ADDR']; // log their ip

// set up some session data for mnistakes or spam or errors
$_SESSION['username'] = $username;
$_SESSION['email'] = $email;

// check for empty info

if ($username == NULL || $username == '') { // no username set
    header ("Location: register.php?p=un"); die;
}
if ($password == NULL || $password == '') { // no password set
    header ("Location: register.php?p=pw"); die;
}
if ($password_check == NULL || $password_check == '') { // no password check set
    header ("Location: register.php?p=pwchk"); die;
}
if ($email == NULL || $email == '') { // no email set
    header ("Location: register.php?p=em"); die;
}

// have we had Spam?

if (strlen($spam_check) > 0) { // spam bot alert
    header ("Location: http://www.google.co.uk");
    die ('You\'re naughty.');
}

// does password and check match?

if ($password != $password_check) { // no match
    $_SESSION['username'] = $username;
    $_SESSION['email'] = $email;
    header ("Location: register.php?p=pwnpc&email=".$email.""); die;
}

在我的表单页面上我正在做:

// check for empty fields and session data

if (isset($_SESSION['email'])) { // email session data set
$email_value = 'value = "'.$_SESSION['email'].'"';
} else { $email_value = 'placeholder="Email"'; }

$errors = mysql_real_escape_string($_GET['p']);

switch ($errors) { // something is empty
case "un": // no username set
    $pen_name = 'Pen Name missing!';
break;
case "pw": // no username set
    $password = 'Password missing!';
break;
case "pwchk": // no password check set
    $pw_check = 'Your passwords don\'t match!';
break;
case "em": // no username set
    $email = 'Email missing!';
break;
case "pwnpc": // passwords don't match
    $pw_mismatch = 'Passwords don\'t match!';
break;
}

及其自身的形式

<form action="register_handle.php" method="post">

<label>Choose a Pen Name</label><br /><?php echo $pen_name ?>
<p><input type="text" name="username" placeholder="Pen Name" autofocus required> </p>

<label>Choose a Password</label><br />
<p><input type="password" name="password" placeholder="Password" required> ---> <input type="password" name="pwcheck" placeholder="Password Confirm" required> <?php echo $password.$pw_check.$pw_mismatch ?></p>

<label>Email</label><br />

 <?php echo $email ?>

<p><input type="email" name="email" <?php echo $email_value ?> required></p>

<input id="checkit" type="text" name="checkit" maxlength="50" size="30">

<input type="submit" value=" Sign Me Up " name="submit" class="submit">

</form>

但我似乎无法恢复这些值。我敢肯定这一定很简单,但你能在这里看到问题吗?如您所见,到目前为止,我只在邮箱上尝试过,但无济于事。

【问题讨论】:

    标签: php html session


    【解决方案1】:

    您的&lt;input&gt; 字段中缺少value attribute

    <p><input type="email" name="email" value="<?php echo htmlentities($email_value) ?>" placeholder="Email" required></p>
    

    这会破坏您想要使用的占位符,但无论如何它应该始终存在,因为如果您提供值,它将不会显示。

    【讨论】:

    • 不要忘记 XSS 保护。 +1 他还在定义 $email_value 时设置了 value 属性:s
    • 它看起来像是来自会话并且不是用户提供的。但你是对的,为了完整起见,我应该证明这一点。
    • 只有在最后一行之后没有回显任何内容时才需要分号。 @M.chaudhry
    • @johnConde 我想我使用的是 php 以前的版本,所以在我的情况下它不像那样工作:) +1
    • Ahhh tnaks,我试图在我的变量中设置 value 属性,因为我认为如果两者都存在,它可能会覆盖占位符,但我想如果它是空白的,那么它不会。
    猜你喜欢
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 2017-07-17
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    相关资源
    最近更新 更多