Jump to content

PHP contact form checkbox


umdart35

Recommended Posts

Hey I'm a beginner at this and I was hoping the answer might be something simple.  I'll post the code if needed, but it might just be something simple.

 

I have a contact form with a php script to email me when a user enters information.  I have a checkbox for whether or not a user wants to receive our newsletter.

 

When a user fills in his/her information.  Clicks the checkbox to receive our newsletter, then clicks submit they are then taken to a thank you page and I receive an email with their info, etc.

 

When a user fills in his/her information. DOES NOT click the checkbox, then clicks submit, nothing on the screen changes, but I still receive the email with their info.

 

Why is this happening?  What am I missing with this checkbox? How do I get the thank you page to still display if they DO NOT click the checkbox?

 

Any thoughts would be appreciated.

 

 

Link to comment
Share on other sites

<?php

 

$visitorf = $_POST['visitorf'];

$visitorl = $_POST['visitorl'];

$visitorphone = $_POST['visitorphone'];

$visitormail = $_POST['visitormail'];

$visitorzip = $_POST['visitorzip'];

$notes = $_POST['notes'];

$attn = $_POST['attn'];

 

 

if (eregi('http:', $notes)) {

die ("Do NOT try that! ! ");

}

if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))

{

echo "<h2>Please hit back and enter a valid email address</h2>\n";

$badinput = "<h2>Your information was not sent</h2>\n";

echo $badinput;

die ("Thank you");

}

 

if(empty($visitorf) || empty($visitorl) || empty($visitorphone) || empty($visitormail) || empty($visitorzip)) {

echo "<h2>Please fill in all fields marked with a *</h2>\n";

die ("Please hit back");

}

$todayis = date("l, F j, Y, g:i a") ;

 

$attn = $attn ;

$subject = $attn;

 

$notes = stripcslashes($notes);

 

$message = " $todayis [EST] \n

From: $visitorf $visitorl \n

Phone: $visitorphone \n

Email: $visitormail \n

Zip: $visitorzip \n

Model: $attn \n

Message: $notes \n

";

 

$from = "From: $visitormail\r\n";

 

 

mail("mreid@atlantictractor.net", $subject, $message, $from);

 

?>

 

<p align="center">

Information Sent: <?php echo $todayis ?>

<br />

Thank You <?php echo $visitorf ?>

<br /><br />

Some one will contact you shortly about <br />

the 2011 John Deere Gator

Model: <?php echo $attn ?>

<br /><br />

Your Comments/Questions:<br />

<?php $notesout = str_replace("\r", "<br/>", $notes);

echo $notesout; ?>

<br />

Would you like to recieve special offers from Atlantic Tractor? <?php if (isset($_POST['specialoffers'])):

echo "Yes";

else:

echo "No";

endif;

?>

Link to comment
Share on other sites

eregi is depreciated and you should look into using preg_match.

 

What is the variable from the form that's posted to your script?

 

I see first name, last name, phone, mail, zip, notes and attn but nothing about the checkbox?

 

Edit: I'm blind

Would you like to recieve special offers from Atlantic Tractor? <?php if (isset($_POST['specialoffers'])):
echo "Yes";
else:
echo "No";
endif;
?> 

 

Should be:

 

<?php if (isset($_POST['specialoffers'])) {
echo 'yes';
} else {
echo 'no';
} // End If
?>

Link to comment
Share on other sites

I'm must be really stupid that I can't figure this out...I changed the code you sent and no change.  Where is the code that says go to "thank you page info" after clicking submit.  The emails are all coming to me, I just can't get the thank you information to display if the user does not click the checkbox.

Link to comment
Share on other sites

Another question..

 

Your checkbox, does it have a value set?

In otherwords......

<input type="checkbox" name="specialoffers" value="yes" />

 

or does it look more like this....

<input type="checkbox" name="specialoffers" />

 

If its more like the second one then I am going to assume that you're thinking along the lines of a text box with the checkbox. If there is a value then its going to get posted with said value. Which is not the case with checkboxes. With checkboxes if the checkbox is clicked on and only if the checkbox is clicked will the browser use the value as part of the submitted or "$_POST" (if you will) content. Not clicked (checked) tells the browsers not to post it. I guess thats the easiest way I could put it. This also applies to radio inputs as well..

 

Link to comment
Share on other sites

hmm, well, now.. I seem to be stumped. Would you mind either sharing all the HTML/PHP related to this issue with the forum, or just me if you like. Cause now I wanna see what the issue is but I need more detail than whats been provided thus far coding wise.

Link to comment
Share on other sites

Here is the form

 

<form method="post" action="contact.php">

 

<?php

$ipi = getenv("REMOTE_ADDR");

$httprefi = getenv ("HTTP_REFERER");

$httpagenti = getenv ("HTTP_USER_AGENT");

?>

 

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />

<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />

<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />

</span><strong><span class="location">CONTACT US</span></strong><br>

<br>

<span class="FormText">First Name: <br />

<input type="text" name="visitorf" size="35" /><br />

Last Name:<br />

<input type="text" name="visitorl" size="35" /><br />

Phone Number: <br />

<input type="text" name="visitorphone" size="35" /><br />

Email:<br />

<input type="text" name="visitormail" size="35" /><br />

Zip:<br />

<input type="text" name="visitorzip" size="10" /><br /><br />

</span>

<p>

<input name="specialoffers" type="checkbox" value="YES"/>

<span class="FormText">Please click if you would like to receive <br>

special offers from **** via email.<br /> <br />

Which ****** are you interested in?<br />

<select name="attn" size="1">

  <option value=" * ">* </option>

  <option value=" * ">* </option>

  <option value=" * ">* </option>

</select>

<br /><br />

Questions or Comments?

<br />

<textarea name="notes" rows="4" cols="33"></textarea>

<br />

<input type="submit" value="Submit" />

</span>

</form>

Link to comment
Share on other sites

Here is the php

 

<?php

 

error_reporting(E_ALL);

 

$visitorf = mysqli_real_escape_string($visitorf, trim($_POST['visitorf'];

$visitorl = mysqli_real_escape_string($visitorl, trim($_POST['visitorl'];

$visitorphone = mysqli_real_escape_string($visitorphone, trim($_POST['visitorphone'];

$visitormail = mysqli_real_escape_string($visitormail, trim($_POST['visitormail'];

$visitorzip = mysqli_real_escape_string($visitorzip, trim($_POST['visitorzip'];

$notes = mysqli_real_escape_string($visitornotes, trim($_POST['notes'];

$attn = mysqli_real_escape_string($visitorattn, trim($_POST['attn'];

 

 

if (eregi('http:', $notes)) {

die ("Do NOT try that! ! ");

}

if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))

{

echo "<h2>Please hit back and enter a valid email address</h2>\n";

$badinput = "<h2>Your information was not sent</h2>\n";

echo $badinput;

die ("Thank you");

}

 

if(empty($visitorf) || empty($visitorl) || empty($visitorphone) || empty($visitormail) || empty($visitorzip)) {

echo "<h2>Please fill in all fields marked with a *</h2>\n";

die ("Please hit back");

}

 

$todayis = date("l, F j, Y, g:i a") ;

 

$attn = $attn ;

$subject = $attn;

 

$notes = stripcslashes($notes);

 

$message = " $todayis [EST] \n

From: $visitorf $visitorl \n

Phone: $visitorphone \n

Email: $visitormail \n

Zip: $visitorzip \n

Model: $attn \n

Message: $notes \n

";

 

$from = "From: $visitormail\r\n";

 

 

mail("*****", $subject, $message, $from);

 

?>

 

<p align="center">

Information Sent: <?php echo $todayis ?>

<br />

Thank You <?php echo $visitorf ?>

<br /><br />

Some one will contact you shortly about <br />

****

Model: <?php echo $attn ?>

<br /><br />

Your Comments/Questions:<br />

<?php $notesout = str_replace("\r", "<br/>", $notes);

echo $notesout; ?>

<br />

Would you like to recieve special offers from **? <?php if (isset($_POST['specialoffers'])) {

echo 'yes';

} else {

echo 'no';

} // End If

?>

 

Link to comment
Share on other sites

Thanks for the help...I'm a beginner to all this and am getting very frustrated at what should be a simple task.

 

That thank you information at the bottom of the php page displays on a new page when the user clicks the checkbox.  The user is not redirected (page does not change) when the checkbox is left unclicked.

Link to comment
Share on other sites

I tested it locally for you, everything worked fine here for me. Except escaping the variables needs to be done differently but your not using a database to store the information so I don't know how to do it otherwise.

 

I bypassed it from sending an e-mail and it works correctly, try that on your end and see what you get.

 

edit: old values

 

$visitorf = $_POST['visitorf'];
$visitorl = $_POST['visitorl'];
$visitorphone = $_POST['visitorphone'];
$visitormail = $_POST['visitormail'];
$visitorzip = $_POST['visitorzip'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.