变量在提交后不显示

时间:2013-09-09 15:47:56

标签: php

我对php很新,我有一个表格,当填写发送?发送时,在if语句中检查。我的问题是在表单提交后.ucfirst($ firstName)。 '.ucfirst($ lastName)永远不会出现在

中的echod
echo '<h2>Thank you, ' .ucfirst( $firstName ). ' ' .ucfirst( $lastName ). ' for your submission, we will be contacting you shortly if needed.</h2>';

以下是完整代码:

<?php
$firstName   = $_POST[ 'firstName' ];
$lastName       = $_POST[ 'lastName' ];
$email          = $_POST[ 'email' ];
$comments  = $_POST[ 'comments' ];
$errors         = array();

if (isset($_GET['sent']) === true) {

    echo '<h2>Thank you, ' .ucfirst( $firstName ). ' ' .ucfirst( $lastName ). ' for your submission, we will be contacting you shortly if needed.</h2>'; 

} else {                                                                     ////////////////////////////////////// else ////////////////////////////////////////////////////

?>

<form action="" method="post">
    <input type="text" name="firstName"  placeholder="first name" <?php if(isset($_POST['firstName']) === true ){ echo 'value="' .strip_tags($_POST['firstName']). '"' ;} ?>/><br />
    <input type="text" name="lastName"  placeholder="last name" <?php if(isset($_POST['lastName']) === true ){ echo 'value="' .strip_tags($_POST['lastName']). '"' ;} ?>/><br />
    <input type="text" name="email" id="email" placeholder="email address" <?php if(isset($_POST['email']) === true ){ echo 'value="' .strip_tags($_POST['email']). '"' ;} ?>/><br />
    <textarea name="comments" id="comments" cols="30" rows="10" placeholder="comments...."><?php if(isset($_POST['comments']) === true ){  echo strip_tags($_POST['comments'])  ;} ?></textarea><br />
    <input type="submit" name="submit" id="submit" /><br />
    <?php foreach($errors as $error) {
        echo $error;
    } 

    if (isset($_POST['submit'])) {

    if ( empty($_POST['lastName']) && empty($_POST['firstName']) && empty($_POST['email']) && empty($_POST['comments']) ) {
        $errors[] = '&bull; All fields are required for form to be submitted!<br />';
    } else {
        if (ctype_alpha($_POST['firstName']) === false ) {
            $errors[] = '&bull; First name must contain only letters!<br />';
        }
        if (ctype_alpha($_POST['lastName']) === false ) {
            $errors[] = '&bull; Last name must contain only letters!<br />';
        }

        if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
            $errors[] = '&bull; You must enter a valid email address!<br />';
        }
        if (empty($_POST['comments']) === true ) {
            $errors[] = '&bull; Tell us why you would like to contact us!<br />';
        }
    }
    if (empty($errors)) {
        header('Location: testForm.php?sent');
        end();
    }


}                                                                               ///////////////////////////////////// end submit ////////////////////////////////////////////////////
}                                                                               ///////////////////////////////////// end else //////////////////////////////////////////////////// 
?>
</form>

3 个答案:

答案 0 :(得分:1)

如果$ _GET ['sent'] === true;。

,你只能回复那句话

但是,您正在发送POST请求。试试这个:

if (isset($_POST['firstName']) {
//echo code here
}

答案 1 :(得分:0)

对于您想要执行的操作,不需要使用另一个再次执行相同操作的GET请求重定向页面。

只需在页面顶部验证您的数据,如下所示:

<?php
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$errors = array();


if (isset($_POST['submit'])) {
if (empty($_POST['lastName']) && empty($_POST['firstName']) && empty($_POST['email']) && empty($_POST['comments'])) {
    $errors[] = '&bull; All fields are required for form to be submitted!<br />';
} else {
    if (ctype_alpha($_POST['firstName']) === false) {
        $errors[] = '&bull; First name must contain only letters!<br />';
    }
    if (ctype_alpha($_POST['lastName']) === false) {
        $errors[] = '&bull; Last name must contain only letters!<br />';
    }

    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        $errors[] = '&bull; You must enter a valid email address!<br />';
    }
    if (empty($_POST['comments']) === true) {
        $errors[] = '&bull; Tell us why you would like to contact us!<br />';
    }
}
if (count($errors)==0) { // if no errors
    echo '<h2>Thank you, ' . ucfirst($firstName) . ' ' . ucfirst($lastName) . ' for your submission, we will be contacting you shortly if needed.</h2>';
} else { // ese show form with errors

?>

<form action="" method="post">
    <input type="text" name="firstName" placeholder="first name" <?php if (isset($_POST['firstName']) === true) {
        echo 'value="' . strip_tags($_POST['firstName']) . '"';
    } ?>/><br/>
    <input type="text" name="lastName" placeholder="last name" <?php if (isset($_POST['lastName']) === true) {
        echo 'value="' . strip_tags($_POST['lastName']) . '"';
    } ?>/><br/>
    <input type="text" name="email" id="email" placeholder="email address" <?php if (isset($_POST['email']) === true) {
        echo 'value="' . strip_tags($_POST['email']) . '"';
    } ?>/><br/>
    <textarea name="comments" id="comments" cols="30" rows="10"
              placeholder="comments...."><?php if (isset($_POST['comments']) === true) {
            echo strip_tags($_POST['comments']);
        } ?></textarea><br/>
    <input type="submit" name="submit" id="submit"/><br/>
    <?php foreach ($errors as $error) {
        echo $error;
    }
    }
    }
    ?>
</form>

答案 2 :(得分:-1)

您使用header()重定向用户,但在此步骤中,您当然会丢失先前发布的数据(第3-5行),这就是为什么名字和姓氏必须为空。

作为解决方案,请尝试在脚本开头填写错误变量。这样,您可以直接在第7行检查$errors是否为空,而不是使用重定向内容。

如果您想使用 redirect-aber-post-pattern ,则必须将输出的两个必需参数包含在标题重定向中。这样,您的解决方案可能是:

// use this replace against header injection if you got php-version < 4.4.2 or < 5.1.2
// - or update your php-version
$hlastname = str_replace("\n", $_POST['lastName'], ""); 
$hfirstname = str_replace("\n", $_POST['firstName'], "");
header('Location: testForm.php?sent=true&firstName='.$hfirstname.'&lastName='.$hlastname);

// and put this into your fist lines:

$firstname = htmlentities($_REQUEST['firstName']);
$lastname = htmlentities($_REQUEST['lastName']);

此外,您的脚本不安全。你必须逃避你的参数对抗xss攻击!在继续编写PHP应用程序之前,请阅读有关XSS漏洞的更多信息(Google会帮助您!;与我对htmlentities()的使用情况进行比较)。