简体   繁体   English

表单发送成功后的PHP JS警报消息

[英]PHP js alert message when form was sent ok

I'm trying to show an alert box saying the message was sent OK, but for some reason I don't see the message. 我试图显示一个警告消息框,指出消息已发送正常,但由于某种原因我看不到该消息。 I do get the one saying to check the captcha. 我确实有话要检查验证码。 What am I doing wrong? 我究竟做错了什么? All the code is in my index.php file. 所有的代码都在我的index.php文件中。 All the if's at the beginning where attempts to avoid resubmitting the form. 所有的if都是在开始时尝试避免重新提交表单。 Also, how can I style the alert box? 另外,如何设置警报框的样式? I've tried using Bootbox. 我试过使用Bootbox。 Js but without luck. Js,但没有运气。

<?php
$message['message'] = "";
if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['submit'])) {


    if (isset($_POST['g-recaptcha-response']) && ($_POST["g-recaptcha-response"] != '')) {

        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $privatekey = "--secretKey--";

        $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
        $data = json_decode($response);

        if (isset($data->success) AND $data->success==true) {

            $to = "info@someone.com.ar";
            $email = $_POST['email'];
            $name = $_POST['name'];
            $subject = $_POST['asunto'];
            $subject2 = 'Copia de: '.$_POST['asunto'];
            $message = $name . " " . "escribio lo siguiente:" . "\n\n" . $_POST['message'];
            $message2 = "Le enviamos una copia de su mensaje " . $name . "\n\n" . $_POST['message'];
            $headers = "From:" . $email;
            $headers2 = "From:" . $to;
            mail($to,$subject,$message,$headers);
            mail($email,$subject2,$message2,$headers2);
            //echo "La consulta se envió correctamente. Gracias! " . $name . ", lo contactaremos a la brevedad.";
            echo '<script type="text/javascript">alert("El mensaje se envió correctamente. Muchas gracias!");</script>';
            //Back to the web
            header('Location: index.php');

        }

        } else {
            echo '<script type="text/javascript">alert("Por favor tildar el captcha!");</script>';
            //$_SESSION['form'] = filter_input_array(INPUT_POST);

    }
}

?>

Form: 形成:

<div class="col-md-4 to-animate">
    <h3 class="section-title">Escribinos</h3>
    <form class="contact-form" action="" method="post" id="form">
        <div class="form-group">
            <label for="name" class="sr-only">Nombre</label>
            <input type="name" class="form-control" name="name" id="name" placeholder="Nombre y Apellido" required title="Por favor ingrese su nombre y apellido" value="<?php echo (isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''); ?>" />
        </div>
        <div class="form-group">
            <label for="asunto" class="sr-only">Asunto</label>
            <input type="asunto" class="form-control" name="asunto" id="asunto" placeholder="Asunto" required title="Por favor ingrese el asunto" value="<?php echo (isset($_POST['asunto']) ? htmlspecialchars($_POST['asunto']) : ''); ?>" />
        </div>
        <div class="form-group">
            <label for="email" class="sr-only">Email</label>
            <input type="email" class="form-control" name="email" id="email" placeholder="Email" required title="Por favor ingrese un mail" value="<?php echo (isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''); ?>" />
        </div>
        <div class="form-group">
            <label for="message" class="sr-only">Mensaje</label>
            <textarea class="form-control" id="message" name="message" rows="7" placeholder="Mensaje" required title="Por favor ingrese un mensaje mayor a 10 caractéres."><?php echo (isset($_POST['message']) ? htmlspecialchars($_POST['message']) : ''); ?></textarea>
        </div>
        <div class="form-group">
            <input type="submit" name="submit" id="btn-submit" class="btn btn-send-message btn-md" value="ENVIAR">
        </div>
        <div class="g-recaptcha" data-sitekey="--Sitekey--" data-callback="callback"></div>
    </form>
</div>

不了解其余代码或是否存在问题-但您不应该在'if'陈述中使用“ AND ”-应该是:

if (isset($data->success) && $data->success==true) {...

After removing the 移除后

header('Location: index.php');

The message appears. 出现信息。 Don't ask me why. 不要问我为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM