0

URL: http://medtransportcenter.com/medical-transportation/los-altos/ I have just recently updated my form to now include a checkbox. I'm trying to get the send-form.php script to recognize whether or not the checkbox was checked or not when it sends the confirmation email. I did some research and found that the following code will do the trick for checking whether it was checked. But, I inserted it and kept giving me syntax error when testing the form. I included the additional code I have for the php script and checkbox field below. Please help with this.

My whole form 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>

My PHP Script

<?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);


?>
23
  • Please indent your code properly. Commented Jul 24, 2013 at 1:25
  • @Fred It could be worse. Commented Jul 24, 2013 at 1:27
  • Just do that if(isset($_POST['brochure']){ and below that add $brochure_checked = $_POST['brochure'];} else {$brochure_not_checked = $_POST['brochure'];}. Something to that affect. Commented Jul 24, 2013 at 1:29
  • What syntax error it is displaying? Commented Jul 24, 2013 at 1:33
  • This is the syntax error: Parse error: syntax error, unexpected T_IF, expecting '{' Commented Jul 24, 2013 at 1:39

3 Answers 3

2

Well here it is my friend, the final and working version.

Tested and working, complete with a:

Requested Brochure: YES
or
Requested Brochure: NO message in your E-mail,
plus I also added an echo'ed "Thank you, your message has been sent..." option at the end.

The form to use:

<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>

The PHP to use:

<?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.";

?>

The way I set it up is I used isset from the POSTed value, assigned $brochure as a variable that, if it is set, the variable equals "YES", and if it is not set, then the same variable equals "NO".

(The code below was added to the PHP, while some other code was removed)

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

The syntax errors that were happening earlier, is that you used addslashes and trim for the checkbox value, which caused havoc.

0
0

There are several issues with your script:

  • The form does not have a submit button. It's required to send the form inputs to the server.
  • You're not validating the form anywhere in your script. That's the reason why it's generating unnecessary errors.
  • In your code, you have many of these - $email = addslashes(trim($_POST['email'])); but you're not accepting the inputs anywhere. Since the input fields doesn't exist, PHP will throw Undefined index errors if you try to echo them.

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!

}

Hope this helps!

9
  • I'm confused. The form does have a submit button. When I remove that code I just entered and put input into the fields and click submit, I do receive the email with the information that was included. Commented Jul 24, 2013 at 1:44
  • @MTC: I don't see it in your question? Have you tried this soltution? Did it work? If not, what's the error? Commented Jul 24, 2013 at 1:47
  • Here it is:<div class="btn-box"><input class="button1" type="submit" onsubmit="return checkBox()" value="Send" tabindex="10" /></div> Commented Jul 24, 2013 at 1:49
  • This is what generates the error: function checkBox() if(isset($_POST['brochure']){ $brochure_checked = $_POST['brochure'];} else { $brochure_not_checked = $_POST['brochure']; } Commented Jul 24, 2013 at 1:49
  • @MTC onsubmit="return checkBox()" is Javascript. My suggestion is PHP, and doesn't belong inside a Javascript-based function. Re-read my comments in your original question, and post your FULL CODE including your FORM. You're making me look like a fool. Commented Jul 24, 2013 at 1:51
0

Your are missing a closing bracket in the first line of your code:

Your Code:

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

Should be:

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

Cheers!

[EDIT] Your Form

<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>

send-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);
?>

Explanation:

What happen is the $_POST['brochure'] is set only when the checkbox is checked. In your code you are using it as:

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

If the brochure field is set then you will get the value. But look at your else condition, you are assigning the value that doesn't exists. And this statement $brochure = addslashes(trim($_POST['brochure'])); is now trying to trim the value that doesn't exists. Check the edited code. Hope it works! I have used comments in the edited part.

3
  • Why -1? It answers the question about the syntax error he has mentioned. Commented Jul 24, 2013 at 2:00
  • Thank you. This does solve the syntax error, I'm dumb don't know how I missed that. Commented Jul 24, 2013 at 2:02
  • But It still doesn't show the input when form is submitted that the checkbox was checked Commented Jul 24, 2013 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.