0

URL: http: //medtransportcenter.com/medical-transportation/los-altos/ 我最近刚刚更新了我的表单,现在包含一个复选框。我试图让 send-form.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("service@medtransportcenter.com","Los Altos, CA Landing Page Contact", $content, $headers);


?>
4

3 回答 3

2

好吧,这是我的朋友,最终的工作版本。

测试和工作,完成:

请求的小册子:

请求的小册子: 您的电子邮件中没有消息,另外
我还在末尾添加了一个回显“谢谢,您的消息已发送......”选项。

要使用的表格:

<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("your_email@example.com","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";
}

之前发生的语法错误是您使用addslashes复选框值,这导致trimhavoc

于 2013-07-24T12:52:15.760 回答
0

您的脚本有几个问题:

  • 该表单没有提交按钮。需要将表单输入发送到服务器。
  • 您没有在脚本中的任何地方验证表单。这就是它产生不必要错误的原因。
  • 在您的代码中,您有许多这些 -$email = addslashes(trim($_POST['email']));但您不会在任何地方接受输入。由于输入字段不存在, Undefined index如果您尝试回显它们,PHP 将抛出错误。

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!

}

希望这可以帮助!

于 2013-07-24T01:35:53.207 回答
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("abc@medtransportcenter.com", "Los Altos, CA Landing Page Contact", $content, $headers);
?>

解释:

$_POST['brochure']仅在选中复选框时才会设置。在您的代码中,您将其用作:

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

如果设置了小册子字段,那么您将获得该值。但是看看你的 else 条件,你正在分配不存在的值。而这个语句$brochure = addslashes(trim($_POST['brochure']));现在正试图修剪不存在的值。检查编辑的代码。希望它有效!我在编辑部分使用了注释。

于 2013-07-24T01:49:58.810 回答