简体   繁体   English

ajax不向php发送请求进行处理

[英]ajax not sending request to php for processing

I am new to programming and after a zillion hours working on this I am not getting any closer to a solution. 我是编程的新手,经过不计其数的工作,我几乎没有一个解决方案。 Any help would be greatly appreciated. 任何帮助将不胜感激。 I have a form that I am sending to php via ajax(although I don't see /process.php in the address bar after the domain name, or does that even matter?). 我有一个表单,我通过ajax发送到php(尽管在域名后的地址栏中看不到/process.php,或者那有关系吗?)。 The confirmation from php does load on the page but the email with the form values does not get sent. 来自php的确认不会加载到页面上,但不会发送带有表单值的电子邮件。 I work very hard at this, and I really do believe I'll get this stuff, but right now I am in need a little help. 我为此工作非常努力,我确实相信我会得到这些东西,但是现在我需要一点帮助。 A live version can be viewed at http://www.hydrodyneh2o.com . 可以在http://www.hydrodyneh2o.com上查看实时版本。 Thanks in advance. 提前致谢。

   $(document).ready(function(){


//Script for "Submit" button to submit and load 'submission confirmation'

    $('#hydrodyne-form input[type="submit"]').click(function(e){
        e.preventDefault();

        //alert("never!");
        $('#hydrodyne-form .warn').remove();
        var fields = $('#hydrodyne-form').serializeArray();

        $.ajax({
            type : 'POST',
            url : 'processForm.php',
            data : fields,
            dataType : 'json',
            success: function(data) {
                if (data.error) {
                    $.each(data.fields, function(k, v) {
                        $('.' + k).append('<span class="warn">' + v + '</span>');
                        $('.warn').fadeIn(700);
                    });
                } else {
                    $('#form-content').fadeOut(700, function() {
                        $(this).hide();

                        $('#form-content').addClass('processing').html('Processing...').fadeIn(400);
                        $('#form-content').delay(1300).fadeOut(700).hide(function(){
                            $(this).removeClass('processing').html(data.confirm).fadeIn(400);
                        });
                    });
                }
            },
            error: function(data) {
                $('#form-content').hide().html('<p id="error" class="italic inside-text">*Error occurred</p>').fadeIn(700);
                }
        });

            return false;

    });

});

PHP (processForm.php): PHP(processForm.php):

<?php   


    $service = $_POST['service'];
    $service_type = implode(",\r\n• ",$service);

    if ($_POST) {

        $expected_inputs = array('company', 'contact_name', 'phone_number', 'email');
        $validation = array(
            'company' => 'Company Name Required',
            'contact_name'  => 'Contact Name Required',
            'phone_number' => 'Phone Number Required',
            'email' => 'Email Address Required'
        );

        $company = stripslashes($_POST['company']);
        $contact_name = stripslashes($_POST['contact_name']);
        $phone_number = stripslashes($_POST['phone_number']);
        $email = stripslashes($_POST['email']);
        $project_summary = stripslashes($_POST['project_summary']);

        $to = 'brian@bseifert.com';
        $subject = 'You\'ve got a Website Inquiry!';
        $message = "\r\n Contact Name:  " . $contact_name . 
        $message = "\r\n\r\n Company:  " . $company .
        $message = "\r\n\r\n Phone Number:  " . $phone_number .
        $message = "\r\n\r\n Email Address:  " . $email .
        $message = "\r\n\r\n\r\n Type of Anticipated Service: \r\n\r\n " . $service_type .
        $message = "\r\n\r\n\r\n Message: \r\n\r\n" . $project_summary;
        $message = wordwrap($message, 80);


        $errors = array();
        $output = array();

        foreach($expected_inputs as $key) {

            if (array_key_exists($key, $_POST)) {
                if (empty($_POST[$key])) {
                    $errors[$key] = $validation[$key];
                } else {
                    $output[$key] = $_POST[$key];
                }
            } else {
                $errors[$key] = $validation[$key];
            }

        }

        if (!empty($errors)) {
            $array = array('error' => true, 'fields' => $errors);
        } else {

            // PROCESS FORM
            mail($to, $subject, $message) or die('not working!!!');

            $confirm = 
                '<div id="logo" style="margin:80px 0 0 335px;"></div>
                <img class="absolute" style="top:158px; left:265px;" src="IMG/inside-pgs/checkmark.png" alt="" />
                <div class="inside-text thank-you">
                    <p>Your message has been sent.<br/> A representative from Hydrodyne will contact you<br/> as soon as possible.</p>
                </div>';
            $array = array('error' => false, 'confirm' => $confirm);
        }

        echo json_encode($array);

    }

?>

HTML: HTML:

    <div id="form-content">

                    <div id="inside-pg-heading">
                        <img src="IMG/inside-pgs/contact.gif" alt=""/>
                    </div><!-- end inside-pg-heading -->

                    <img class="hydroArrowContact absolute" src="IMG/inside-pgs/hydroArrowContact.png" alt="" />
                    <p class="hydroArrowContactText absolute">ydrodyne thanks you for stopping by, and asks that you use the form below <br>to ask any questions or place a request. We look forward to hearing from you!</p>

                    <div class="absolute" style="width:950px; top:132px; left:35px;">
                        <!--  <div id="response"></div>  -->

                        <form id="hydrodyne-form" action="processForm.php" method="post">
                            <div id="formCol1">
                                <h5 class="form-heading inside-text bold italic"><span style="font-size:14px;">Fields marked with an asterisk (*) are required.</span></h5>
                                <div id="text-inputs" class="inside-text bold italic">
                                    <div id="inputCol1">
                                        <h4 class="company">&#42;&nbsp;Company</h4>
                                        <input type="text" name="company"<br/>
                                        <h4 class="contact_name">&#42;&nbsp;Contact Name</h4>
                                        <input type="text" name="contact_name"<br/>
                                    </div><!-- end inputCol1 -->

                                    <div id="inputCol2">
                                        <h4 class="phone_number">&#42;&nbsp;Phone Number</h4>
                                        <input type="text" name="phone_number"<br/>
                                        <h4 class="email">&#42;&nbsp;Email Address</h4>
                                        <input type="text" name="email"<br/>
                                    </div><!-- end inputCol2 -->
                                </div><!-- end text-inputs -->

                                <div class="clear"></div>

                                <h4 class="form-heading inside-text bold italic">Please check anticipated service(s)</h4>
                                <div class="checkboxes green">
                                    <div id="checkboxCol1">
                                        <input type="checkbox" name="service[]" value="Wastewater Flushing / Laundry">&nbsp;&nbsp;&nbsp;Wastewater Flushing / Laundry<br/>
                                        <input type="checkbox" name="service[]" value="Irrigation">&nbsp;&nbsp;&nbsp;Irrigation<br/>
                                        <input type="checkbox" name="service[]" value="Vehicle Washing">&nbsp;&nbsp;&nbsp;Vehicle Washing<br/>
                                        <input type="checkbox" name="service[]" value="Animal Feeding">&nbsp;&nbsp;&nbsp;Animal Feeding
                                    </div><!-- end checkboxCol1 -->

                                    <div id="checkboxCol2">
                                        <input type="checkbox" name="service[]" value="Commercial / Industrial Cooling">&nbsp;&nbsp;&nbsp;Commercial / Industrial Cooling<br/>
                                        <input type="checkbox" name="service[]" value="Commercial / Industrial Processing">&nbsp;&nbsp;&nbsp;Commercial / Industrial Processing<br/>
                                        <input type="checkbox" name="service[]" value="Fire Suppression">&nbsp;&nbsp;&nbsp;Fire Suppression
                                    </div><!-- end checkboxCol2 -->
                                </div><!-- end checkboxes -->
                            </div><!-- end formCol1 -->

                            <div id="formCol2">
                                <h5 class="form-heading inside-text bold italic">Provide Hydrodyne with more information by typing a brief message in the box below.</h5>
                                <textarea name="project_summary"></textarea>
                                <input type="submit" value="">
                            </div><!-- formCol2 -->

                        </form>

                    </div>


                </div><!-- end form-content -->

According to the pHp documentation: http://php.net/manual/en/function.mail.php#example-3381 you can add a header to the mail(); 根据pHp文档: http : //php.net/manual/en/function.mail.php#example-3381,您可以将标题添加到mail();

$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

// PROCESS FORM
mail($to, $subject, $message, $headers) or die('not working!!!');

Also you should check the following line as I guess there is a invalid character which can create any unwanted pHp Warnings/errors; 另外,您应该检查以下行,因为我猜有一个无效字符,该字符会产生任何不必要的pHp警告/错误;

$service_type = implode(",\r\n• ",$service);

it should be: 它应该是:

$service_type = implode(",\r\n",$service);

Thank you Nouphal.M! 谢谢Nouphal.M! Man, I spent a lot of time searching for a solution when all I needed to do was check my spam. 伙计,当我需要做的只是检查垃圾邮件时,我花了很多时间寻找解决方案。 I really appreciate your time and efforts everyone! 非常感谢您的时间和大家的努力!

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

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