我有有效的一些形式的代码,它的工作良好
我的代码是有效的形式,然后在下一页发送消息这是完整的代码
<?
$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];
if($_POST['submit']){
if(empty($name))
$errorname = ("<span id='error'>no name</span>");
if(empty($email))
$erroremail = ("<span id='error'>no email</span>");
if (empty($msg))
$errormsg = ("<span id='error'>no msg</span>");
else {
echo'<script>window.location = "process.php";</script>';
}
}
?>
<form dir="rtl" action="" method="post">
<div>name<span class="nothing"> </span><input type="text" name="name" id="name" dir="rtl">
<span id="errororg"> <?= $errorname; ?></span></div>
<div >email<span class="nothing"> </span><input type="email" color="#fff" name="email" id="email"><span id="errororg"> <?= $erroremail; ?></span></div>
<span>msg</span>
<div><textarea rows="10" name="msg" id="comment" dir="rtl"></textarea><span id="errororg"><?= $errormsg; ?></span></div>
<div><input type="submit" name="submit" value="send" dir="rtl"></div><br>
</form>
=======
所以它工作正常,有效的消息和一切都很好,但消息到达空!
这是下一页的代码
<?
$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];
date_default_timezone_set('Asia/Baghdad');
$nw_date=date("F j, Y");
$body="name: ".$name."'n msg: ".$msg."'n date: ".$nw_date;
$email="$email";
mail("rezult.smtp@gmail.com", "new msg",$body, "from: <".$email.">");
echo "<span class='eco'>thanks for contact<br></span>";
echo "<span dir='rtl'>- </span>"."<a href='contact.php'>back..</a>";
?>
所以我认为空消息的问题是因为
else {
echo'<script>window.location = "process.php";</script>';
所以我删除了它,把process。php放到了action
中<form dir="rtl" action="" method="post">
它工作了,消息到达了
但是验证不起作用
所以我怎么能使代码和验证执行之前去下一页…
?我知道如何通过javascript来做到这一点
但是我能在这段代码中做到吗?我错过了什么?
非常感谢……不好意思,问题很长实际上。您不需要process.php,所以复制process.php中的代码并将其粘贴到else语句中。使用下面的代码使用$submitted变量。我在代码中做了一些更改,并标记了注释//new code
<?
$submitted = ""; // new code
$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];
if($_POST['submit']){
if(empty($name))
$errorname = ("<span id='error'>no name</span>");
if(empty($email))
$erroremail = ("<span id='error'>no email</span>");
if (empty($msg))
$errormsg = ("<span id='error'>no msg</span>");
else {
$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];
date_default_timezone_set('Asia/Baghdad');
$nw_date=date("F j, Y");
$body="name: ".$name."'n msg: ".$msg."'n date: ".$nw_date;
$email="$email";
mail("rezult.smtp@gmail.com", "new msg",$body, "from: <".$email.">");
echo "<span class='eco'>thanks for contact<br></span>";
echo "<span dir='rtl'>- </span>"."<a href='contact.php'>back..</a>";
$submitted = "yes"; // new code
}
}
if($submitted == ""){ // new code ?>
<form dir="rtl" action="" method="post">
<div>name<span class="nothing"> </span><input type="text" name="name" id="name" dir="rtl">
<span id="errororg"> <?= $errorname; ?></span></div>
<div >email<span class="nothing"> </span><input type="email" color="#fff" name="email" id="email"><span id="errororg"> <?= $erroremail; ?></span></div>
<span>msg</span>
<div><textarea rows="10" name="msg" id="comment" dir="rtl"></textarea><span id="errororg"><?= $errormsg; ?></span></div>
<div><input type="submit" name="submit" value="send" dir="rtl"></div><br>
</form>
<?php } // new code ?>
如果您想在表单成功提交后重定向页面,而没有任何消息设置<form action="" method="post">
,并且在您的代码中不删除javascript调用,请将其更改为:
$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];
$error = array();
if($_POST['submit']){
if(empty($name)) {
$error['name'] = "<span id='error'>no name</span>";
}
if(empty($email)) {
$error['email'] = "<span id='error'>no email</span>";
}
if (empty($msg)) {
$error['msg'] = "<span id='error'>no msg</span>";
}
if (empty($error)) {
echo'<script>window.location = "process.php";</script>';
}
}
这将是你的表单
<form dir="rtl" action="" method="post">
<div>name<span class="nothing"> </span><input type="text" name="name" id="name" dir="rtl">
<span id="errororg"> <?= !empty($error['name']) ? $error['name'] : ''; ?></span></div>
<div >email<span class="nothing"> </span>
<input type="email" color="#fff" name="email" id="email">
<span id="errororg"> <?= !empty($error['email']) ? $error['email'] : ''; ?></span></div>
<span>msg</span>
<div><textarea rows="10" name="msg" id="comment" dir="rtl"></textarea>
<span id="errororg"><?= !empty($error['msg']) ? $error['msg'] : ''; ?></span></div>
<div><input type="submit" name="submit" value="send" dir="rtl"></div><br>
</form>