2013-03-06 14 views
0

我在一個網站上有兩種不同的聯繫表單,我試圖使用相同的php文件進行處理。我想在代碼的一部分上使用此代碼,其中「1」是第一個窗體隱藏字段值,「2」是第二個窗體的隱藏字段值。隱形字段無法使用的PHP聯繫表格

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 = "[email protected]"; 
$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; 
     }); 
    }); 

}); 
+1

顯示你的HTML部分也 – 2013-03-06 12:10:45

+0

加入'回聲$ _ POST [「過程」];'你的代碼的不同部分,並有一看看值是否在某處變化。 – oktopus 2013-03-06 12:11:46

+0

請發送您的HTML與表格 – michi 2013-03-06 12:11:57

回答

1

您攔截訪問您提交的JavaScript,而不是通過你的所有表單域到表單處理程序。要糾正它,更換這樣的:

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

與此:

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

這是一個巨大的幫助。不夠感謝你!在這個問題上,我一直把頭撞在牆上太久了。 – hyphen 2013-03-06 16:27:41

0

爲了簡單起見,你可以測試使用if($_REQUEST['process']==1)任何POSTGET參數可以通過REQUEST

+0

他的HTML說'

2013-03-06 12:17:10

+0

謝謝。我編輯了我的答案 – 2013-03-06 12:22:28

+0

我嘗試使用$ _REQUEST獲得相同的結果。 – hyphen 2013-03-06 12:25:10