如何在动作之前执行php有效代码?

时间:2014-03-23 03:34:23

标签: php validation

我有一些有效的代码表格,而且工作正常

我的代码jop是有效形式,然后在下一页发送消息 这是完整的代码

    <?
                    $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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><input type="text" name="name" id="name" dir="rtl"> 


            <span id="errororg">&nbsp;<?= $errorname; ?></span></div>
                            <div >email<span class="nothing">&nbsp;&nbsp;</span><input  type="email" color="#fff" name="email" id="email"><span id="errororg">&nbsp;&nbsp;<?= $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放入动作

<form dir="rtl" action="" method="post">

它起作用了,消息很顺利

但验证不起作用

所以我可以在进入下一页之前如何制作代码和验证。?

我知道如何通过javascript

执行此操作

但我可以在此代码中执行此操作吗?我错过了什么?

非常感谢..并且抱歉很长的问题

2 个答案:

答案 0 :(得分:0)

实际上。您不需要process.php,因此请复制process.php中的代码并将其粘贴到else语句中。使用此代码 使用$ submitted变量。我在代码中做了一些更改,并用注释//新代码

标记了它
<?


    $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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><input type="text" name="name" id="name" dir="rtl"> 


            <span id="errororg">&nbsp;<?= $errorname; ?></span></div>
                            <div >email<span class="nothing">&nbsp;&nbsp;</span><input  type="email" color="#fff" name="email" id="email"><span id="errororg">&nbsp;&nbsp;<?= $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 ?>

答案 1 :(得分:0)

如果您想在成功提交表单后重定向页面而没有设置任何邮件<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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><input type="text" name="name" id="name" dir="rtl"> 
   <span id="errororg">&nbsp;<?= !empty($error['name']) ? $error['name'] : ''; ?></span></div>
   <div >email<span class="nothing">&nbsp;&nbsp;</span>
       <input  type="email" color="#fff" name="email" id="email">
       <span id="errororg">&nbsp;&nbsp;<?= !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>