Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据echo使用Javascript重定向PHP表单_Javascript_Php - Fatal编程技术网

如何根据echo使用Javascript重定向PHP表单

如何根据echo使用Javascript重定向PHP表单,javascript,php,Javascript,Php,我正在寻找一种在提交后根据回声重定向的方法 我使用了以下代码: <?php $to = "mymail@gmail.com"; $name = Trim(stripslashes($_POST['name'])); $email = Trim(stripslashes($_POST['email'])); $message = Trim(stripslashes($_POST['message'])); $subject = "New Client Call"; $body = 'nam

我正在寻找一种在提交后根据回声重定向的方法 我使用了以下代码:

<?php
$to = "mymail@gmail.com";
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$subject = "New Client Call";

$body = 'name: ' .$name."\r\n";
$body = 'email: ' .$email."\r\n";
$body = 'message: ' .$message."\r\n";

$go = mail($to, $subject, $body, "From:<$email>");
if ($go) {
    echo "Success";
}

else {
    echo "error";
}
?>

<form action="index9.html" method="post" name="redirect" ></form>
<script> document.forms['redirect'].submit()</script>
但我现在有两个问题:

我总是获得成功。甚至客户端发送空的详细信息。 我想用Javascript if/else重定向到一个错误页面,但我现在不知道该怎么做。 顺便说一句,我是这个领域的新手,因此:
我将感谢您的建议/帮助,并表示感谢。

您不需要javascript来实现这一点,您可以使用纯php。 如果表单中的字段为空,则可以使用错误检查,如下所示

validate.php

if(isset($_POST['submit'])){
if (empty($_POST["email"])) {
    $emailErr = "Email is required";
        } else {
            $email = test_input($_POST["email"]);
            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                 $emailErr = "Invalid email format"; 
            }
     }
if (empty($_POST["name"])) {
            $nameErr = "Name is required";
        } else {
            $name = test_input($_POST["name"]);
}

if (isset( $nameErr) ||  isset($emailErr){
     // you have a error
    }

else {
    $to = "mymail@gmail.com";
    $name = Trim(stripslashes($_POST['name']));
    $email = Trim(stripslashes($_POST['email']));
    $message = Trim(stripslashes($_POST['message']));
    $subject = "New Client Call";
    ....
这将检查电子邮件字段是否为空,如果为空,则提示需要发送错误电子邮件

然后在html中添加错误

电邮: *
你似乎走在正确的道路上,在将参数放入邮件程序之前,我想说你可以做更多的健全性检查,以确保输入所有值,这样你就避免尝试发送你知道会失败的电子邮件,因此你会提前失败并通知用户,您还可能会看到邮件函数返回的错误消息,如果无法使用erorr_get_last方法轻松纠正任何其他问题,则返回该消息

回显错误\u获取\u最后一个['message']

注意:php邮件不会验证给定的标题,因此您必须100%确保它是干净的,并且用户不可能插入自己的标题,因此请确保您也对其进行了清理

然后,要进行重定向,您可以直接用javascript部分替换echo success和echo error调用

因此,脚本最终将如下所示:

<?php

    $to = "mymail@gmail.com";
    $name = Trim(stripslashes($_POST['name']));
    $email = Trim(stripslashes($_POST['email']));
    $message = Trim(stripslashes($_POST['message']));
    $subject = "New Client Call";

    $missingFieldCount = 0;
    if(!isset($_POST['name'])) {
        echo "Error: Name requried";
        $missingFieldCount++;
    }
    if(!isset($_POST['email'])) {
        echo "Error: Email required";
        $missingFieldCount++;
    }
    if(!isset($_POST['message'])) {
        echo "Error: message required";
        $missingFieldCount++;
    }

    if($missingFieldcount > 0) {
        echo "Please fill in the missing $missingFieldcount fields, then submit again";
    } else {
        $body = 'name: ' .$name."\r\n";
        $body .= 'email: ' .$email."\r\n";          // Using .= to append instead of replace
        $body .= 'message: ' .$message."\r\n";

        $headers = "From:<$email>"; //Make sure $email is actually just an email address and not injected headers

        $success = mail($to, $subject, $body, $headears);

        if ($success) {
            $redirectPage = "successPage.html"
        } else {
            $redirectPage = "errorPage.html"
            echo "An error occured:";

            //Don't show this to the user, but log it to file instead
            //this is here only for demonstration
            echo error_get_last()['message'];
        }

        echo "<form action="$redirectPage" method="post" name="redirect" ></form>";
        echo "<script> document.forms['redirect'].submit()</script>";
    }
}
?>
<?php
$name = Trim(stripslashes($_POST['name'])); 
$email = Trim(stripslashes($_POST['email'])); 
$message = Trim(stripslashes($_POST['message'])); 
$to = "mymail@gmail.com";
$subject = " New Client order ";
$errors = 0;


$body ="";
$body .="Name: ";
$body .=$name;
$body .="\n";
$body .="Email: ";
$body .=$email;
$body .="\n";
$body .="Message: ";
$body .=$message;
$body .="\n";



if(isset($_POST['submit'])) { // Checking null values in message.

    $name = $_POST["name"]; // Sender Name
    $email = $_POST["email"]; // Sender's email ID
    $message = $_POST["message"]; // Sender's Message


    if (empty($_POST["name"])){
        $nameError = "Name is required";
        $errors = 1;
    }
    if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
    {
        $emailError = "Email is not valid";
        $errors = 1;
    }

    if (empty($_POST["message"])){
        $messageError = "Message is required";
        $errors = 1;
    }

}   
if($errors == 0){
    $successMessage = mail($to, $subject, $body, "From: <$email>");
}
    else {
         print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    }

if ($successMessage){
   print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}


?>
谢谢你们的回答。 最后,我从本期中获得的所有信息中管理了一个完美的PHP表单。无需Javascript重定向到两个不同的页面: 1.谢谢你,佩奇。 2.错误页

代码是这样的:

<?php

    $to = "mymail@gmail.com";
    $name = Trim(stripslashes($_POST['name']));
    $email = Trim(stripslashes($_POST['email']));
    $message = Trim(stripslashes($_POST['message']));
    $subject = "New Client Call";

    $missingFieldCount = 0;
    if(!isset($_POST['name'])) {
        echo "Error: Name requried";
        $missingFieldCount++;
    }
    if(!isset($_POST['email'])) {
        echo "Error: Email required";
        $missingFieldCount++;
    }
    if(!isset($_POST['message'])) {
        echo "Error: message required";
        $missingFieldCount++;
    }

    if($missingFieldcount > 0) {
        echo "Please fill in the missing $missingFieldcount fields, then submit again";
    } else {
        $body = 'name: ' .$name."\r\n";
        $body .= 'email: ' .$email."\r\n";          // Using .= to append instead of replace
        $body .= 'message: ' .$message."\r\n";

        $headers = "From:<$email>"; //Make sure $email is actually just an email address and not injected headers

        $success = mail($to, $subject, $body, $headears);

        if ($success) {
            $redirectPage = "successPage.html"
        } else {
            $redirectPage = "errorPage.html"
            echo "An error occured:";

            //Don't show this to the user, but log it to file instead
            //this is here only for demonstration
            echo error_get_last()['message'];
        }

        echo "<form action="$redirectPage" method="post" name="redirect" ></form>";
        echo "<script> document.forms['redirect'].submit()</script>";
    }
}
?>
<?php
$name = Trim(stripslashes($_POST['name'])); 
$email = Trim(stripslashes($_POST['email'])); 
$message = Trim(stripslashes($_POST['message'])); 
$to = "mymail@gmail.com";
$subject = " New Client order ";
$errors = 0;


$body ="";
$body .="Name: ";
$body .=$name;
$body .="\n";
$body .="Email: ";
$body .=$email;
$body .="\n";
$body .="Message: ";
$body .=$message;
$body .="\n";



if(isset($_POST['submit'])) { // Checking null values in message.

    $name = $_POST["name"]; // Sender Name
    $email = $_POST["email"]; // Sender's email ID
    $message = $_POST["message"]; // Sender's Message


    if (empty($_POST["name"])){
        $nameError = "Name is required";
        $errors = 1;
    }
    if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
    {
        $emailError = "Email is not valid";
        $errors = 1;
    }

    if (empty($_POST["message"])){
        $messageError = "Message is required";
        $errors = 1;
    }

}   
if($errors == 0){
    $successMessage = mail($to, $subject, $body, "From: <$email>");
}
    else {
         print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
    }

if ($successMessage){
   print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}


?>

进行一些错误检查,例如,如果'name'为空,则对所需的字段进行检查:例如,如果为空$name{echo'name为必需';},或者您可以重定向到您想要的任何页面,而不是消息。