我对 php 相当陌生,我有一个表格,填写后会发送 ?sent ,在 if 语句中检查它。我的问题是提交表单后 .ucfirst( $firstName )。' ' .ucfirst( $lastName ) 在回显时永远不会出现
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[] = '• All fields are required for form to be submitted!<br />';
} else {
if (ctype_alpha($_POST['firstName']) === false ) {
$errors[] = '• First name must contain only letters!<br />';
}
if (ctype_alpha($_POST['lastName']) === false ) {
$errors[] = '• Last name must contain only letters!<br />';
}
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = '• You must enter a valid email address!<br />';
}
if (empty($_POST['comments']) === true ) {
$errors[] = '• Tell us why you would like to contact us!<br />';
}
}
if (empty($errors)) {
header('Location: testForm.php?sent');
end();
}
} ///////////////////////////////////// end submit ////////////////////////////////////////////////////
} ///////////////////////////////////// end else ////////////////////////////////////////////////////
?>
</form>