PHP联系表单隐藏字段不起作用

时间:2013-03-06 12:07:04

标签: php forms conditional-statements

我在网站上有两个不同的联系表单,我正在尝试使用相同的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;
        });
    });

});

2 个答案:

答案 0 :(得分:1)

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

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

用这个:

$.post(action, $('#contactform').serialize(),

答案 1 :(得分:0)

为了简单起见,您可以使用if($_REQUEST['process']==1)进行测试,因为任何POSTGET参数都可以通过REQUEST访问