PHP表单在预订过程中出错

时间:2015-07-10 02:38:57

标签: javascript php html email

我有一个客户希望使用预订页面。该过程只是将联系信息通过电子邮件发送给我自己。我一直是.NET开发人员,从未使用过PHP。我做了很多研究,并试图创建一个booking.php页面。由于代码与现有的联系页面(工作正常)非常相似,我从contact.php复制了代码,将其保存到booking.php,并根据需要调整了代码。我已经发布了以下代码。我收到的错误消息是输入有效的电子邮件地址。由于电子邮件的代码(html / php)与联系页面相同,而我实际上使用的是有效的电子邮件,因此我对如何调试非常困惑:/。如果有人有任何建议我会非常感激。

如下所示,我只是尝试为新字段创建变量,而不是将它们包含在我的电子邮件正文中。在我解决这个问题之后,我正计划继续这样做。

<?php

            if(!$_POST) exit;

            // Email address verification, do not edit.
            function isEmail($email) { 
                return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));     
            }

            if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

            $name     = $_POST['name'];
            $email    = $_POST['email'];
            $phone   = $_POST['phone'];
            $address   = $_POST['address'];
            $city   = $_POST['city'];
            $state   = $_POST['state'];
            $zip   = $_POST['zip'];
            $duration   = $_POST['duration'];
            $people   = $_POST['people'];

            if(trim($name) == '') {
                echo '<div class="error_message box-red box">Attention! You must enter your name.</div>';
                exit();
            } 


              else if(trim($email) == '') {
                echo '<div class="error_message box-red box">Attention! Please enter a valid email address.</div>';
                exit();
            }


            else if(trim($phone) == '') {
                echo '<div class="error_message box-red box">Attention! Please enter a valid phone number.</div>';
                exit();
            }   
              else if(trim($address) == '') {
                echo '<div class="error_message box-red box">Attention! Please enter a valid address.</div>';
                exit();
            }
              else if(trim($city) == '') {
                echo '<div class="error_message box-red box">Attention! Please enter a valid city.</div>';
                exit();
            }

              else if(trim($state) == '') {
                echo '<div class="error_message box-red box">Attention! Please enter a valid state.</div>';
                exit();
            }

              else if(trim($zip) == '') {
                echo '<div class="error_message box-red box">Attention! Please enter a valid zip.</div>';
                exit();
            }

              else if(trim($duration) == '') {
                echo '<div class="error_message box-red box">Attention! Please enter a valid trip duration.</div>';
                exit();
            }
              else if(trim($people) == '') {
                echo '<div class="error_message box-red box">Attention! Please enter a valid number of people.</div>';
                exit();
            }
             else if(!is_numeric($phone)) {
                echo '<div class="error_message box-red box">Attention! Phone number can only contain digits.</div>';
                exit();
            } else if(!isEmail($email)) {
                echo '<div class="error_message box-red box">Attention! You have enter an invalid e-mail address, try again.</div>';
                exit();
            }


            if(trim($comments) == '') {
                echo '<div class="error_message box-red box">Attention! Please enter your message.</div>';
                exit(); 
            }

            if(get_magic_quotes_gpc()) {
                $comments = stripslashes($comments);
            }


            // Configuration option.
            // Enter the email address that you want to emails to be sent to.
            // Example $address = "joe.doe@yourdomain.com";

            $address = "eric@ericbelldesigns.com";

            // Configuration option.
            // i.e. The standard subject will appear as, "You've been contacted by John Doe."

            // Example, $e_subject = '$name . ' has contacted you via Your Website.';

            $e_subject = 'Yacht Charter Booked by ' . $name . '.';


            // Configuration option.
            // You can change this if you feel that you need to.
            // Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.

            $e_body = "You have been contacted by $name, their additional message is as follows." . PHP_EOL . PHP_EOL;
            $e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
            $e_reply = "You can contact $name via email, $email or via phone $phone";

            $msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

            $headers = "From: $email" . PHP_EOL;
            $headers .= "Reply-To: $email" . PHP_EOL;
            $headers .= "MIME-Version: 1.0" . PHP_EOL;
            $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
            $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

            if(mail($address, $e_subject, $msg, $headers)) {

                // Email has sent successfully, echo a success page.

                echo "<fieldset>";          
                echo "<div id='success_page' class='box-green box'>";
                echo "<p><b>Email Sent Successfully.</b></p>";
                echo "<p>Thank you $name, your message has been submitted to us.</p>";
                echo "</div>";
                echo "</fieldset>";
            } 
            else {
                echo 'ERROR!';
            }

HTML

<form role="form" method="post" action="booking.php" id="bookingform">
    <div id="message"></div>
    <div class="one-half">
        <label for="name">Name</label>
            <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
    </div>

    <div class="one-half">
         <label for="people">Number of people</label>
         <select id="people">
             <option value="" selected>Please Select</option>
             <option value="6">Up to 6</option>
             <option value="12">Up to 12</option>
         </select>
    </div>

    <div class="one-half">
         <label for="phone">Phone</label>
         <input type="text" class="form-control" id="phone" name="phone" placeholder="Phone Number" value="">
    </div>

    <div class="one-half">
        <label for="email">E-mail address</label>
        <input type="email" id="email"/>
    </div>

    <div class="one-half">
        <label for="address">Address</label>
        <input type="text" id="address" name="address" />
    </div>

    <div class="one-half">
        <label for="zip">ZIP code</label>
        <input type="text" id="zip" name="zip" />
    </div>

    <div class="one-half">
        <label for="city">City</label>
        <input type="text" id="city" name="city" />
    </div>

    <div class="one-half">
         <label for="state">State</label>
         <input type="text" id="state" name="state" />
    </div>
    <div class="one-half">
         <label for="duration">Duration</label>
         <select id="duration" name="duration">
              <option value="">Please Select</option>
              <option value="half" selected>1/2 Day</option>
              <option value="3">3-Day (Catalina Special)</option>
          </select>
    </div>
    <div class="one-half">
         <label for="payment">Payment type</label>
         <select id="payment" name="payment">
              <option value="">Please Select</option>
              <option value="cash" selected>Cash</option>
              <option value="check">Check</option>
              <option value="paypal">PayPal</option>
         </select>
    </div>

    <input id="submit" name="submit" type="submit" value="Complete booking process" class="button medium full gold" />
</form>

**添加JAVASCRIPT             //预订表格

        $('#bookingform').submit(function(){
            var action = $(this).attr('action');
            $("#message").show(500,function() {
            $('#message').hide();
            $('#submit')
                .after('<img src="images/contact-ajax-loader.gif" class="loader" />')
                .attr('disabled','disabled');

            $.post(action, { 
                name: $('#name').val(),
                email: $('#email').val(),
                phone: $('#phone').val(),
                address: $('#address').val(),
                city: $('#city').val(),
                state: $('#state').val(),
                zip: $('#zip').val(),
                date: $('#date').val(),
                duration: $('#duration').val(),
                people: $('#people').val()
            },
            function(data){
                document.getElementById('message').innerHTML = data;
                $('#message').slideDown('slow');
                $('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
                $('#submit').removeAttr('disabled'); 
            });

            });
            return false; 
        });

        // CONTACT FORM
        $('#contactform').submit(function(){
            var action = $(this).attr('action');
            $("#message").show(500,function() {
            $('#message').hide();
            $('#submit')
                .after('<img src="images/contact-ajax-loader.gif" class="loader" />')
                .attr('disabled','disabled');

            $.post(action, { 
                name: $('#name').val(),
                email: $('#email').val(),
                phone: $('#phone').val(),
                comments: $('#comments').val()
            },
            function(data){
                document.getElementById('message').innerHTML = data;
                $('#message').slideDown('slow');
                $('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
                $('#submit').removeAttr('disabled'); 
            });

            });
            return false; 
        });

1 个答案:

答案 0 :(得分:1)

为了让php访问发布的表单数据,输入需要有name属性 -

     <select id="people" name="people">
         <option value="" selected>Please Select</option>
         <option value="6">Up to 6</option>
         <option value="12">Up to 12</option>
     </select>
    ...
    <label for="email">E-mail address</label>
    <input type="email" id="email" name="email" />
    ...

由于contact.php上的表单在没有name属性的情况下正常运行,因此指向通过javascript提交的表单,其中javascript代码使用name属性发布值。