0

我在一个网站上有两个不同的联系表格,我试图使用相同的 php 文件进行处理。我想在代码的一个部分使用此代码,“1”是第一个表单隐藏字段值,“2”是第二个表单的隐藏字段值。

if ($_POST['process']=='1'){}

无论我做什么,我都无法让这段代码工作。我已经尝试删除上面的代码,并且联系表单提交得很好。我尝试的另一件事是将隐藏值更改为文本字段,以验证是否确实显示了一个值,确实存在。我还将“过程”隐藏字段值作为变量包含在内,并将其发送到电子邮件中,这对我来说似乎很奇怪。该变量的值在电子邮件中出现空白。我在这里做错了什么?这是我的代码:

 if(!$_POST) exit;
    function remove_non_numeric($string) {
    return preg_replace("/[^0-9.]/", "", $string);
    }

    function isEmail($email) {
    return preg_match("/^(?!.{255,})(?!.{65,}@)([!#-'*+\/-9=?^-~-]+)(?>\.(?1))*@(?!.*[^.]        {64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?\.){1,126}[a-z]{2,6}$/iD", $email);
    }
    if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

    if ($_POST['process']=='1'){

$name     = $_POST['name'];
$email    = $_POST['email'];
$phone    = remove_non_numeric($_POST['phone']);
$comments = $_POST['comments'];
$verify   = $_POST['verify'];
$process  = $_POST['process'];

if(trim($name) == '') {
    echo '<div class="error_message">You must enter your name.</div>';
    exit();
} else if(trim($email) == '') {
    echo '<div class="error_message">Please enter a valid email address.</div>';
    exit();
} else if(trim($phone) == '') {
    echo '<div class="error_message">Please enter a valid phone number.</div>';
    exit();
} else if(!isEmail($email)) {
    echo '<div class="error_message">You have entered an invalid e-mail address, please try again.</div>';
    exit();
} else if(trim($comments) == '') {
    echo '<div class="error_message">Please enter your message.</div>';
    exit();
} else if(!isset($verify) || trim($verify) == '') {
    echo '<div class="error_message">Please enter the verification number.</div>';
    exit();
} else if(trim($verify) != '4') {
    echo '<div class="error_message">The verification number you entered is incorrect.</div>';
    exit();
}

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

$address = "myemail@email.com";
$e_subject = 'You\'ve been contacted by ' . $name . '.';
$e_body = "You have been contacted by $name. Their message is below." . PHP_EOL . PHP_EOL;
$e_content = "\"$process\"" . 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'>";
    echo "<h1>Email Sent Successfully.</h1>";
    echo "<p>Thank you <strong>$name</strong>, your message has been sent.</p>";
    echo "</div>";
    echo "</fieldset>";

} else {

    echo 'ERROR!';
}
}

为了简单起见,我已经删除了将处理另一种形式的第二段代码,但希望你能看到我正在尝试做的事情。如果名为“process”的隐藏字段的值为“1”,则处理此代码。如果它的值为 2,我希望它改为处理其他代码。

这是代码的html部分:

<div id="contact">

            <div id="message"></div>

            <form method="post" action="http://s423839726.onlinehome.us/franklinvineyard/contact.php" name="contactform" id="contactform">

            <fieldset>

            <input type="text" name="process" id="process" size="4" value="1" />

            <label for="name">Your Name<span class="required">*</span></label>
            <input name="name" type="text" id="name" size="30" value="" />

            <br />
            <label for="email">Email<span class="required">*</span></label>
            <input name="email" type="text" id="email" size="30" value="" />

            <br />
            <label for="phone">Phone<span class="required">*</span></label>
            <input name="phone" type="tel" id="phone" size="30" value="" />


            <br />
            <label for="comments">Comments<span class="required">*</span></label>
            <textarea name="comments" cols="40" rows="15" id="comments" style="width: 350px;"></textarea>

            <label>Are you human?<span class="required">*</span></label>

            <label class="accesskey" for="verify">&nbsp;&nbsp;&nbsp;3 + 1 =</label>
            <input class="accesskey" name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />

            <input type="submit" class="submit" id="submit" value="submit" />

            </fieldset>

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

还有一些处理表单的jquery......这可能是问题吗?

jQuery(document).ready(function(){

    $('#contactform').submit(function(){

        var action = $(this).attr('action');

        $("#message").slideUp(750,function() {
        $('#message').hide();

          $('#submit')
            .attr('disabled','disabled');
     if($('#process').val()=='1'){
        $.post(action, {
            name: $('#name').val(),
            email: $('#email').val(),
            phone: $('#phone').val(),
            comments: $('#comments').val(),
            verify: $('#verify').val()
        },
            function(data){
                document.getElementById('message').innerHTML = data;
                $('#message').slideDown('slow');
                $('#contactform img.loader').fadeOut('slow',function(){$(this).remove();
                                                                      });
                $('#submit').removeAttr('disabled');
                if(data.match('success') !== null) $('#contactform').slideUp('slow');

            }
        );
    else{
        $.post(action, {
            name: $('#name').val(),
            email: $('#email').val(),
            phone: $('#phone').val(),
            friend: $('#friend').val(),
            search: $('#search').val(),
            signage: $('#signage').val(),
            vineyard: $('#vineyard').val(),
            newspaper: $('#newspaper').val(),
            other: $('#other').val(),
            comments1: $('#comments-survey1').val(),
            comments2: $('#comments-survey2').val(),
            comments3: $('#comments-survey3').val(),
            verify: $('#verify').val()
        },
        function(data){
                document.getElementById('message').innerHTML = data;
                $('#message').slideDown('slow');
                $('#contactform img.loader').fadeOut('slow',function(){$(this).remove();
                                                                      });
                $('#submit').removeAttr('disabled');
                if(data.match('success') !== null) $('#contactform').slideUp('slow');

            }
        );

        });

        return false;
        });
    });

});
4

2 回答 2

1

您在 javascript 中拦截您的提交,而不是将所有表单字段传递给表单处理程序。要更正它,请替换它:

$.post(action, {
            name: $('#name').val(),
            email: $('#email').val(),
            phone: $('#phone').val(),
            comments: $('#comments').val(),
            verify: $('#verify').val()
        },

有了这个:

$.post(action, $('#contactform').serialize(),
于 2013-03-06T13:06:03.407 回答
0

为了简单起见,您可以使用if($_REQUEST['process']==1)任何参数进行测试,POST或者GET可以通过以下方式访问参数REQUEST

于 2013-03-06T12:14:33.800 回答