我很难弄清楚我的PHP表单在process.php上处理的原因,但没有返回带有相应$ message的表单页面。我错过了一行代码吗?我自己写了这个,这是第一次。
这是我的HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Contact Form - jQuery</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<h3>Contact Us</h3>
<?php echo $contact_message; ?>
<form id="myForm" method="post" action="process.php">
<input name="name" type="text" value="<?php echo $_POST[name]; ?>" placeholder="Name" required/>
<br>
<input name="email" type="email" value="<?php echo $_POST[email]; ?>" placeholder="you@yourmail.com" required/>
<br>
<textarea name="message" class="message" placeholder="We can answer your questions." required>
<?php echo $_POST[message]; ?>
</textarea>
<br>
<button type="submit" name="submit" class="btn send">
<img src="img/send.png">
</button>
<br>
<?php echo $contact_success_message; ?>
</form>
<!--close contact form-->
</body>
</html>
这是我的process.php
<?php
//checks for valid email
function is_valid_email($email) {
$result = true;
$pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';
if(!preg_match($pattern, $email)) {
$result = false;
}
return $result;
}
//when send is pressed, validate fields
if(isset($_POST['submit'])) {
$valid = true;
$contact_message = '';
if ( $_POST['name'] == "" ) {
$contact_message .= "You forgot to tell us your name. ";
$valid = false;
}
if ( !is_valid_email($_POST['email']) ) {
$contact_message .= "A valid email is required, don't worry we don't share it with anyone. ";
$valid = false;
}
if ( $_POST['message'] == "" ) {
$contact_message .= "What did you want to ask us? ";
$valid = false;
}
//if everything checks out, send the message!
if ( $valid == true ) {
$safe_email = str_replace("\r\n","",$_POST[email]);
$mail = "From: $_POST[name]\n";
$mail .= "Email: $_POST[email]\n";
$mail .= "$_POST[message]\n";
mail('ME@MYEMAIL.COM','New Contact from RN+',$mail,"From: $safe_email\r\n");
$contact_success_message = 'Brilliant I say! We will be in contact with you shortly.';
//clear form when submission is successful
unset($_POST);
}
}
?>
我可以发誓说我以前用过这个,但这次它没有回到联系页面。
答案 0 :(得分:2)
看起来你打算使用这样的代码:
form.php的
<?php include 'process.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Contact Form - jQuery</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<h3>Contact Us</h3>
<?php echo $contact_message; ?>
<form id="myForm" method="post" action="process.php">
<input name="name" type="text" value="<?php echo $_POST[name]; ?>" placeholder="Name" required/><br>
<input name="email" type="email" value="<?php echo $_POST[email]; ?>" placeholder="you@yourmail.com" required/><br>
<textarea name="message" class="message" placeholder="We can answer your questions." required><?php echo $_POST[message]; ?></textarea><br>
<button type="submit" name="submit" class="btn send"><img src="img/se
__
formnd.png"></button><br>
<?php echo $contact_success_message; ?>
</form><!--close contact form-->
</body>
</html>
表单处理器将设置您在HTML代码中输出的相应变量。由于process.php检查方法是否为POST,因此您不必在表单页面中执行此操作。
答案 1 :(得分:0)
如果您希望process.php重定向回您的表单,您需要添加一个PHP标头代码,如:header('Location: http://www.example.com/form.php');
如果要将任何数据传回原始页面,请将其作为GET变量包含在URL中:header('Location: http://www.example.com/form.php?message='.$messagetext);
然后,您可以使用GET在表单页面上检索此内容:{{1} }
重定向后不要忘记echo $contact_success_message = $_GET['message'];
!
答案 2 :(得分:0)
如果您没有任何理由排除单个PHP页面,可以将两个(表单和进程)合并到一个php页面中。
<?php
//checks for valid email function
...
if(isset($_POST['submit'])) {
...
}
?>
...your HTML goes here...
如果没有提交数据,这将只显示表单,如果表单已提交,将“重新加载”页面,执行操作并显示相应的消息。将表单操作更改为action=""
,以便文件发布到自身。
答案 3 :(得分:-1)
我建议使用jQuery验证。它更容易,并且可以帮助您回复消息时遇到的任何问题。
http://docs.jquery.com/Plugins/Validation
像这样......
$("#myForm").validate({
rules: {
name: { required: true; }
},
messages: {
name: "You forgot to tell us your name.",
}
});
您可以为电子邮件字段和整个表单执行此操作。你可以在网上找到很多例子。
只需包含您的其他字段即可。如果您这样做,验证是客户端,表格将处理,然后您转发到感谢您联系我们页面。