2013-07-24 63 views
0

URL:http://medtransportcenter.com/medical-transportation/los-altos/ 我剛剛更新了我的表單,現在包括一個複選框。我試圖讓send-form.php腳本在發送確認電子郵件時識別複選框是否被選中。我做了一些研究,發現下面的代碼會檢查它是否被檢查。但是,我插入它並在測試表單時不斷給我提供語法錯誤。我在下面的php腳本和複選框字段中包含了附加代碼。請幫助。PHP發送腳本 - 複選框驗證

我的整個HTML表單:

<div class="form-box"> 
<!-- Form --> 

<form id="proForm" action="send-form.php" method="post" > 

<h2 style="text-align:center;">REQUEST INFO</h2> 
     <h3 style="text-align:center;">FREE EXPERT ADVICE</h3> 
<div class="form-content"> 
<!-- Form --> 

<table cellpadding="0" cellspacing="0" border="0"> 

<!--tr> 
<td class="field-full" colspan="2"> 
    <div class="label"><label for="www"><b>Your website URL</b></label></div> 
    <input id="www" class="required defaultInvalidUrl url" maxlength="40" name="www" type="text" tabindex="2" value="http://" /> 
</td> 
</tr--> 
<tr> 
<th class="label" align="right"><label for="nameis">Your name</label></td>:<td class="field"> 
    <input id="nameis" class="required defaultInvalidEmail nameis" maxlength="40" name="nameis" type="text" tabindex="2" value="Required" 
     onfocus="if (this.value == 'Required') {this.value = '';}" 
     onblur="if (this.value == '') {this.value = 'Required';}" size="40" /> 
</td> 
</tr>         
<tr> 
<th class="label" align="right"><label for="email">E-mail</label></td>:<td class="field"> 
    <input id="email" class="required defaultInvalidEmail email" maxlength="40" name="email" type="text" tabindex="3" value="Required" 
     onfocus="if (this.value == 'Required') {this.value = '';}" 
     onblur="if (this.value == '') {this.value = 'Required';}" size="40" /> 
</td> 
</tr> 

<tr> 
<th class="label" align="right"><label for="phone">Best Phone:</label></td> 
<td class="field"> 
    <input id="phone" class="required defaultInvalidEmail phone" maxlength="40" name="phone" type="text" tabindex="4" value="Required" size="30" 
     onfocus="if (this.value == 'Required') {this.value = '';}" 
     onblur="if (this.value == '') {this.value = 'Required';}" size="40" /> 
</td> 
</tr> 

<tr> 
<th class="label" align="right"><label for="bestime"> 
Best time to reach you:</label></td> 
<td class="field"> 
    <input maxlength="40" size="30" name="bestime" type="text" tabindex="5" value="" /> 
</td> 
</tr> 

<tr> 
<th class="label" align="right"><label for="phone"> 
Origin:</label></td> 
<td class="field"> 
    <input maxlength="40" size="30" name="origin" type="text" tabindex="5" value="" /> 
</td> 
</tr> 

<tr> 
<th class="label" align="right"><label for="phone"> 
Destination:</label></td> 
<td class="field"> 
    <input maxlength="40" size="30" name="destination" type="text" tabindex="5" value="" /> 
</td> 
</tr> 


<tr> 
<th class="label" align="right"><label for="msg"> 
Message:</label></td> 
<td class="field"> 
    <textarea rows="3" cols="31" name="msg" tabindex="6"></textarea> 
</td> 
</tr> 

<tr> 
<th class="label"><label for="brochure">Request Brochures</label></td> 
<td class="field"> 
    <input type="checkbox" name="brochure[]" value="Yes" /> 
</td> 
</tr>  

<!--tr> 
<td class="field-full" colspan="2"> 
    <div class="label"><label for="msg"><b>Other</b></label></div> 
    <textarea rows="3" cols="30" name="msg" tabindex="9"></textarea> 
</td> 
</tr--> 

</table> 

<div class="terms">By clicking 'Send',<br/> you accept our <a onclick="showTerms()">Privacy Policy</a>. <br /><span style="font-size:14px;">Phone: 800-282-6878.</span></div> 

<div class="btn-box"><input class="button1" type="submit" value="Send" tabindex="10" /></div> 


<!-- END Form --> 
</form> 
</div> 

我的PHP腳本

<?php 

//$www = '';  
$email = ''; 
$nameis = ''; 
$phone = ''; 
$bestime = ''; 
//$address = ''; 
$origin = ''; 
$destination = ''; 
$msg = ''; 
$brochure = ''; 

function getIp() 
{if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ 
    $ip_address=$_SERVER['HTTP_X_FORWARDED_FOR']; 
} 

if (!isset($ip_address)){ 
     if (isset($_SERVER['REMOTE_ADDR'])) 
     $ip_address=$_SERVER['REMOTE_ADDR']; 
} 
return $ip_address; 
} 

if(isset($_POST['brochure'])){ 
$brochure_checked = $_POST['brochure'];} 
else { 
$brochure_not_checked = $_POST['brochure']; 
} 


//taking info about date, IP and user agent 

$timestamp = date("Y-m-d H:i:s"); 
$ip = getIp(); 
$host = gethostbyaddr($ip); 
$user_agent = $_SERVER["HTTP_USER_AGENT"]; 


//taking the data from form 

//$www = addslashes(trim($_POST['www'])); 
$email = addslashes(trim($_POST['email'])); 
$nameis = addslashes(trim($_POST['nameis'])); 
$phone = addslashes(trim($_POST['phone'])); 
$bestime = addslashes(trim($_POST['bestime'])); 
//$address = addslashes(trim($_POST['address'])); 
$origin = addslashes(trim($_POST['origin'])); 
$destination = addslashes(trim($_POST['destination'])); 
$msg = addslashes(trim($_POST['msg'])); 
$brochure = addslashes(trim($_POST['brochure'])); 




//preparing mail 

$headers = "MIME-Version: 1.0\n"; 
$headers .= "Content-type: text/html; charset=utf-8\n"; 
$headers .= "Content-Transfer-Encoding: quoted-printable\n"; 
$headers .= "From: $email\n"; 

$content = 'Name: '.$nameis.'<br>'. 
'E-mail: '.$email.'<br>'. 
'Phone: '.$phone.'<br>'. 
'Message: '.$msg.'<br>'. 
'Best Time to Call: '.$bestime.'<br>'. 
'Origin: '.$origin.'<br>'. 
'Destination: '.$destination.'<br>'. 
'Requested Brochure: '.$brochure.'<br>'. 
'Time: '.$timestamp.'<br>'. 
'IP: '.$host.'<br>'. 
'User agent: '.$user_agent; 


//sending mail 

mail("[email protected]","Los Altos, CA Landing Page Contact", $content, $headers); 


?> 
+0

請正確縮進您的代碼。 –

+0

@Fred它可能會更糟糕。 – hexafraction

+0

只要做到這一點'if(isset($ _ POST ['brochure']){'及以下添加'$ brochure_checked = $ _POST ['brochure'];} else {$ brochure_not_checked = $ _POST ['brochure'];} '。Some thing to that affect。 –

回答

2

那麼這裏是我的朋友,最後的工作版本。

測試和工作,完成一個:

要求手冊:

要求宣傳冊:在您的電子郵件NO消息,
再加上我還增加了在最後回顯「謝謝,您的消息已發送...」選項。

的形式使用:

<div class="form-box"> 
<!-- Form --> 

<form id="proForm" action="send-form.php" method="post" > 

<h2 style="text-align:center;">REQUEST INFO</h2> 
     <h3 style="text-align:center;">FREE EXPERT ADVICE</h3> 
<div class="form-content"> 
<!-- Form --> 

<table cellpadding="0" cellspacing="0" border="0"> 

<!--tr> 
<td class="field-full" colspan="2"> 
    <div class="label"><label for="www"><b>Your website URL</b></label></div> 
    <input id="www" class="required defaultInvalidUrl url" maxlength="40" name="www" type="text" tabindex="2" value="http://" /> 
</td> 
</tr--> 
<tr> 
<th class="label" align="right"><label for="nameis">Your name</label></td>:<td class="field"> 
    <input id="nameis" class="required defaultInvalidEmail nameis" maxlength="40" name="nameis" type="text" tabindex="2" value="Required" 
     onfocus="if (this.value == 'Required') {this.value = '';}" 
     onblur="if (this.value == '') {this.value = 'Required';}" size="40" /> 
</td> 
</tr>         
<tr> 
<th class="label" align="right"><label for="email">E-mail</label></td>:<td class="field"> 
    <input id="email" class="required defaultInvalidEmail email" maxlength="40" name="email" type="text" tabindex="3" value="Required" 
     onfocus="if (this.value == 'Required') {this.value = '';}" 
     onblur="if (this.value == '') {this.value = 'Required';}" size="40" /> 
</td> 
</tr> 

<tr> 
<th class="label" align="right"><label for="phone">Best Phone:</label></td> 
<td class="field"> 
    <input id="phone" class="required defaultInvalidEmail phone" maxlength="40" name="phone" type="text" tabindex="4" value="Required" size="30" 
     onfocus="if (this.value == 'Required') {this.value = '';}" 
     onblur="if (this.value == '') {this.value = 'Required';}" size="40" /> 
</td> 
</tr> 

<tr> 
<th class="label" align="right"><label for="bestime"> 
Best time to reach you:</label></td> 
<td class="field"> 
    <input maxlength="40" size="30" name="bestime" type="text" tabindex="5" value="" /> 
</td> 
</tr> 

<tr> 
<th class="label" align="right"><label for="phone"> 
Origin:</label></td> 
<td class="field"> 
    <input maxlength="40" size="30" name="origin" type="text" tabindex="5" value="" /> 
</td> 
</tr> 

<tr> 
<th class="label" align="right"><label for="phone"> 
Destination:</label></td> 
<td class="field"> 
    <input maxlength="40" size="30" name="destination" type="text" tabindex="5" value="" /> 
</td> 
</tr> 


<tr> 
<th class="label" align="right"><label for="msg"> 
Message:</label></td> 
<td class="field"> 
    <textarea rows="3" cols="31" name="msg" tabindex="6"></textarea> 
</td> 
</tr> 

<tr> 
<th class="label"><label for="brochure">Request Brochures</label></td> 
<td class="field"> 
    <input type="checkbox" name="brochure"/> 
</td> 
</tr>  

<!--tr> 
<td class="field-full" colspan="2"> 
    <div class="label"><label for="msg"><b>Other</b></label></div> 
    <textarea rows="3" cols="30" name="msg" tabindex="9"></textarea> 
</td> 
</tr--> 

</table> 

<div class="terms">By clicking 'Send',<br/> you accept our <a onclick="showTerms()">Privacy Policy</a>. <br /><span style="font-size:14px;">Phone: 800-282-6878.</span></div> 

<div class="btn-box"><input class="button1" type="submit" value="Send" tabindex="10" /></div> 


<!-- END Form --> 
</form> 
</div> 

的PHP的使用方法:

<?php 

//$www = '';  
$email = ''; 
$nameis = ''; 
$phone = ''; 
$bestime = ''; 
//$address = ''; 
$origin = ''; 
$destination = ''; 
$msg = ''; 
$brochure = ''; 

function getIp() 
{ 
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ 
    $ip_address=$_SERVER['HTTP_X_FORWARDED_FOR']; 
} 

if (!isset($ip_address)){ 
     if (isset($_SERVER['REMOTE_ADDR'])) 
     $ip_address=$_SERVER['REMOTE_ADDR']; 
} 
return $ip_address; 
} 

if(isset($_POST['brochure'])){ 
$brochure = "YES"; 
} 
else { 
$brochure = "NO"; 
} 


//taking info about date, IP and user agent 

$timestamp = date("Y-m-d H:i:s"); 
$ip = getIp(); 
$host = gethostbyaddr($ip); 
$user_agent = $_SERVER["HTTP_USER_AGENT"]; 


//taking the data from form 

//$www = addslashes(trim($_POST['www'])); 
$email = addslashes(trim($_POST['email'])); 
$nameis = addslashes(trim($_POST['nameis'])); 
$phone = addslashes(trim($_POST['phone'])); 
$bestime = addslashes(trim($_POST['bestime'])); 
//$address = addslashes(trim($_POST['address'])); 
$origin = addslashes(trim($_POST['origin'])); 
$destination = addslashes(trim($_POST['destination'])); 
$msg = addslashes(trim($_POST['msg'])); 


//preparing mail 

$headers = "MIME-Version: 1.0\n"; 
$headers .= "Content-type: text/html; charset=utf-8\n"; 
$headers .= "Content-Transfer-Encoding: quoted-printable\n"; 
$headers .= "From: $email\n"; 

$content = 'Name: '.$nameis.'<br>'. 
'E-mail: '.$email.'<br>'. 
'Phone: '.$phone.'<br>'. 
'Message: '.$msg.'<br>'. 
'Best Time to Call: '.$bestime.'<br>'. 
'Origin: '.$origin.'<br>'. 
'Destination: '.$destination.'<br>'. 
'Requested Brochure: '.$brochure.'<br>'. 
'Time: '.$timestamp.'<br>'. 
'IP: '.$host.'<br>'. 
'User agent: '.$user_agent; 


//sending mail 

mail("[email protected]","Los Altos, CA Landing Page Contact", $content, $headers); 

echo "Thank you, your message has been sent. We will contact you as soon as possible."; 

?> 

的方式予設置它是我用issetPOSTed值,分配$brochure如一個變量,如果它被設置,變量等於「YES」,並且如果它沒有被設置,那麼相同的變量等於「NO」。

(下面的代碼添加到PHP,而除去一些其他的代碼)

if(isset($_POST['brochure'])){ 
$brochure = "YES"; 
} 
else { 
$brochure = "NO"; 
} 

即是前面發生的語法錯誤,是你用addslashestrim複選框值,其中造成嚴重破壞

+0

再次感謝Fred! :) – MTC

+0

@MTC你很受歡迎,這是我的榮幸。 :) –

0

有幾個問題與你的腳本:

  • 形式沒有一個提交按鈕。它需要將表單輸入發送到服務器。
  • 您不驗證腳本中任何位置的表單。這就是它產生不必要錯誤的原因。
  • 在你的代碼中,你有很多 - $email = addslashes(trim($_POST['email']));但你不接受任何地方的輸入。由於輸入字段不存在,如果您嘗試回顯它們,PHP將會拋出Undefined index錯誤。

HTML:

<form id="proForm" action="send-form.php" method="post" > 
<tr> 
<th class="label"><label for="brochure">Request Brochures</label></td> 
<td class="field"> 
Email: <input type="text" name="email"> 
Phone: <input type="text" name="phone"> 
Bestime: <input type="text" name="bestime"> 
Origin: <input type="text" name="origin"> 
Destination: <input type="text" name="destination"> 
Message: <textarea name="msg">Input message</textarea> 
<input type="checkbox" name="brochure" value="Yes" /> 
<input type="submit" name="submitButton"/> 
</form> 

PHP:

if(isset($_POST['submitButton'])){ 

//the form was submited, process the inputs below! 

} 

希望這有助於!

+0

我很困惑。表單確實有一個提交按鈕。當我刪除代碼時,我剛剛輸入並將輸入放入字段中,然後單擊「提交」,我確實收到包含該信息的電子郵件。 – MTC

+0

@MTC:我在你的問題中沒有看到它?你有沒有嘗試過這種解決方法?它有用嗎?如果不是,那麼錯誤是什麼? –

+0

這裏是:'

' – MTC

0

你缺少在代碼的第一行右括號:

代碼:

if(isset($_POST['brochure']){ 
// Requested Brochures 
} 

應該是:

if(isset($_POST['brochure'])){ //Closing bracket for if added 
// Requested Brochures 
} 

乾杯!

[編輯] 表單

<div class="form-box"> 
    <!-- Form --> 

    <form id="proForm" action="send-form.php" method="post" > 

     <h2 style="text-align:center;">REQUEST INFO</h2> 
     <h3 style="text-align:center;">FREE EXPERT ADVICE</h3> 
     <div class="form-content"> 
      <!-- Form --> 

      <table cellpadding="0" cellspacing="0" border="0"> 

       <!--tr> 
       <td class="field-full" colspan="2"> 
       <div class="label"><label for="www"><b>Your website URL</b></label></div> 
       <input id="www" class="required defaultInvalidUrl url" maxlength="40" name="www" type="text" tabindex="2" value="http://" /> 
       </td> 
       </tr--> 
       <tr> 
        <th class="label" align="right"><label for="nameis">Your name</label></td>:<td class="field"> 
        <input id="nameis" class="required defaultInvalidEmail nameis" maxlength="40" name="nameis" type="text" tabindex="2" value="Required" 
        onfocus="if (this.value == 'Required') {this.value = '';}" 
        onblur="if (this.value == '') {this.value = 'Required';}" size="40" /> 
        </td> 
       </tr> 
       <tr> 
        <th class="label" align="right"><label for="email">E-mail</label></td>:<td class="field"> 
        <input id="email" class="required defaultInvalidEmail email" maxlength="40" name="email" type="text" tabindex="3" value="Required" 
        onfocus="if (this.value == 'Required') {this.value = '';}" 
        onblur="if (this.value == '') {this.value = 'Required';}" size="40" /> 
        </td> 
       </tr> 

       <tr> 
        <th class="label" align="right"><label for="phone">Best Phone:</label></td><td class="field"> 
        <input id="phone" class="required defaultInvalidEmail phone" maxlength="40" name="phone" type="text" tabindex="4" value="Required" size="30" 
        onfocus="if (this.value == 'Required') {this.value = '';}" 
        onblur="if (this.value == '') {this.value = 'Required';}" size="40" /> 
        </td> 
       </tr> 

       <tr> 
        <th class="label" align="right"><label for="bestime"> Best time to reach you:</label></td><td class="field"> 
        <input maxlength="40" size="30" name="bestime" type="text" tabindex="5" value="" /> 
        </td> 
       </tr> 

       <tr> 
        <th class="label" align="right"><label for="phone"> Origin:</label></td><td class="field"> 
        <input maxlength="40" size="30" name="origin" type="text" tabindex="5" value="" /> 
        </td> 
       </tr> 

       <tr> 
        <th class="label" align="right"><label for="phone"> Destination:</label></td><td class="field"> 
        <input maxlength="40" size="30" name="destination" type="text" tabindex="5" value="" /> 
        </td> 
       </tr> 

       <tr> 
        <th class="label" align="right"><label for="msg"> Message:</label></td><td class="field">     <textarea rows="3" cols="31" name="msg" tabindex="6"></textarea></td> 
       </tr> 

       <tr> 
        <th class="label"><label for="brochure">Request Brochures</label></td><td class="field"> 
        <input type="checkbox" name="brochure[]" value="Yes" /> 
        </td> 
       </tr> 

       <!--tr> 
       <td class="field-full" colspan="2"> 
       <div class="label"><label for="msg"><b>Other</b></label></div> 
       <textarea rows="3" cols="30" name="msg" tabindex="9"></textarea> 
       </td> 
       </tr--> 

      </table> 

      <div class="terms"> 
       By clicking 'Send', 
       <br/> 
       you accept our <a onclick="showTerms()">Privacy Policy</a>. 
       <br /> 
       <span style="font-size:14px;">Phone: 800-282-6878.</span> 
      </div> 

      <div class="btn-box"> 
       <input class="button1" type="submit" value="Send" tabindex="10" /> 
      </div> 

      <!-- END Form --> 
    </form> 
</div> 

發送-form.php的

<?php 

//$www = ''; 
$email = ''; 
$nameis = ''; 
$phone = ''; 
$bestime = ''; 
//$address = ''; 
$origin = ''; 
$destination = ''; 
$msg = ''; 
$brochure = ''; 
echo "<pre>"; 
print_r($_POST); 
echo "</pre>"; 

function getIp() { 
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
     $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; 
    } 

    if (!isset($ip_address)) { 
     if (isset($_SERVER['REMOTE_ADDR'])) 
      $ip_address = $_SERVER['REMOTE_ADDR']; 
    } 
    return $ip_address; 
} 

if (isset($_POST['brochure'])) { 
    $brochure_checked = $_POST['brochure']; 
} else { 
    //$brochure_checked = "no"; //no could be any value you want because the check box is not checked 
    // or 
    //if you want to use it in post super global you can set it as 
    $_POST['brochure'] = array('no'); // the value is returned in array if the brochure is set so i am using array here 
} 

//taking info about date, IP and user agent 

$timestamp = date("Y-m-d H:i:s"); 
$ip = getIp(); 
$host = gethostbyaddr($ip); 
$user_agent = $_SERVER["HTTP_USER_AGENT"]; 

//taking the data from form 

//$www = addslashes(trim($_POST['www'])); 
$email = addslashes(trim($_POST['email'])); 
$nameis = addslashes(trim($_POST['nameis'])); 
$phone = addslashes(trim($_POST['phone'])); 
$bestime = addslashes(trim($_POST['bestime'])); 
//$address = addslashes(trim($_POST['address'])); 
$origin = addslashes(trim($_POST['origin'])); 
$destination = addslashes(trim($_POST['destination'])); 
$msg = addslashes(trim($_POST['msg'])); 
$brochure = addslashes(trim($_POST['brochure'][0])); //now you can use this value here 
var_dump($brochure); 

//preparing mail 

$headers = "MIME-Version: 1.0\n"; 
$headers .= "Content-type: text/html; charset=utf-8\n"; 
$headers .= "Content-Transfer-Encoding: quoted-printable\n"; 
$headers .= "From: $email\n"; 

$content = 'Name: ' . $nameis . '<br>' . 'E-mail: ' . $email . '<br>' . 'Phone: ' . $phone . '<br>' . 'Message: ' . $msg . '<br>' . 'Best Time to Call: ' . $bestime . '<br>' . 'Origin: ' . $origin . '<br>' . 'Destination: ' . $destination . '<br>' . 'Requested Brochure: ' . $brochure . '<br>' . 'Time: ' . $timestamp . '<br>' . 'IP: ' . $host . '<br>' . 'User agent: ' . $user_agent; 

//sending mail 

mail("[email protected]", "Los Altos, CA Landing Page Contact", $content, $headers); 
?> 

說明:

是當複選框被選中的$_POST['brochure']只設定發生什麼。在你的代碼使用它作爲:

if(isset($_POST['brochure'])){ 
$brochure_checked = $_POST['brochure'];} 
else { 
$brochure_not_checked = $_POST['brochure']; 
} 

如果小冊子字段設置,那麼你將獲得價值。但看看你的其他條件,你正在分配不存在的值。並且此聲明$brochure = addslashes(trim($_POST['brochure']));正在嘗試修剪不存在的值。檢查編輯的代碼。希望它有效!我在編輯部分使用了評論。

+0

爲什麼-1?它回答了他提到的有關語法錯誤的問題。 – Konsole

+0

謝謝。這確實解決了語法錯誤,我很笨,不知道我是怎麼錯過的。 – MTC

+0

但是當提交表單時,複選框被選中時,它仍然不顯示輸入 – MTC