Jump to content

Error when checkbox is checked.


carosa
Go to solution Solved by Psycho,

Recommended Posts

Not able to find my error, please help me.

I want to send a form copy request of the email when the checkbox is checked "Send a copy to my e-mail address." 

 

 

contact.html
 
<div id="contact form" align="center">
 
<table table="table" width="775" align="center"  bordercolor="#030" bgcolor="#006400">
 
<form name="contactform" method="post" action="enviar.php" align="center">
 
<tr>
<td width="198" valign="top"><h7><label for="first_name" class="OthHdr">First Name *<br /><em>Nombre *</em></label></h7>
</td>
<td width="565" valign="top">
<input  type="text" name="first_name" maxlength="50" size="30" />
   </td>
</tr>
 
<tr>
<td valign="top"><h7><label for="last_name" class="OthHdr">Surname *<br /><em>Apellido</em></label></h7>
</td>
<td valign="top">
<input  type="text" name="last_name" maxlength="50" size="30" />
</td>
</tr>
 
<tr>
<td valign="top">
<h7><label for="'email_from" class="OthHdr"> Email Address *<br /><em>Correo Electrónico</em></label></h7>
</td>
<td valign="top">
<input  type="text" name="email_from" id="email_from" maxlength="80" size="30" />
</td>
</tr>
 
<tr>
<td valign="top"><h7>
<label for="telephone" class="OthHdr"> Telephone Number<br /><em>Teléfono</em></label></h7>
</td>
<td valign="top">
<input  type="text" name="telephone" maxlength="30" size="30" /></td>
</tr>
 
<tr>
<td valign="top">
<h7><label for="issue" class="OthHdr">Subject *<br /><em>Asunto</em></label></h7>
</td>
<td valign="top">
  <textarea  name="issue" maxlength="30" cols="26" rows="1"></textarea>
</tr>
 
<tr>
<td valign="top">
<h7><label for="comments" class="OthHdr">Comments *<br /><em>Mensaje</em></label></h7>
</td>
<td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
 
<tr>
<td height="40" class="OthHdr">
<h7><label for="checkbox" class="OthHdr">Send a copy to my e-mail address<br /><em>Enviar copia a mi correo</em></label></h7>
</td>
<td>
<input type="checkbox" name="checkbox" id= "checkbox" value="1" checked/>
</td>
</tr>
 
<tr>
 <td colspan="2" style="text-align:center">
     <input type="submit" name="checkbox" value="Submit">
</tr>
 
<td colspan="2" class="OthHdr" style="text-align:center">
<h5 align="right"><font face="Arial, Helvetica, sans-serif" size="2" color="#fff">* Required data</font></a></h5></td>
<td style="text-align:center"> </td>
</form>
</table>
  </div>
 
 
enviar.php
 
<div align="left">
 
<?php
 
    //If the submit button is pressed
if(isset($_POST['submit'])) {
 
if (isset($_POST['checkbox']) && $_POST['checkbox'] == '1')
 
    echo '<div style="color:green"><h2 align="center">Your email was sent successfully!</h2></div>'; 
 
 
else echo '<div style="color:red"><h2 align="center">Your email wasn`t sent, please go back.</h2></div>';
 
 
    // Edit the 2 lines below as required
$email_to = "info@mysite.com";
 
$email_subject = "Comment from mysite ";
 
 
 
function died($error) {
 
// Your error code can go here
 
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
 
echo "These errors appear below.<br /><br />";
 
echo $error."<br /><br />";
 
echo "Please go back and fix these errors.<br /><br />";
die();
}
 
    // Validation expected data exists
if(!isset($_POST['first_name']) ||
 
!isset($_POST['last_name']) ||
 
!isset($_POST['email_from']) ||
 
!isset($_POST['telephone']) ||
 
!isset($_POST['issue']) ||
 
!isset($_POST['comments'])) {
 
died('We are sorry, but there appears to be a problem with the comments form you submitted.');       
}
 
$first_name = $_POST['first_name']; // required
 
$last_name = $_POST['last_name']; // required
 
$email_from = $_POST['email_from']; // required
 
$telephone = $_POST['telephone']; // not required
 
$issue = $_POST['issue']; // required
 
$comments = $_POST['comments']; // required 
 
$error_message = "";
 
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 
if(!preg_match($email_exp,$email_from)) {
 
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
 
$string_exp = "/^[A-Za-z .'-]+$/";
 
if(!preg_match($string_exp,$first_name)) {
 
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
 
if(!preg_match($string_exp,$last_name)) {
 
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
 
if(strlen($issue) < 2) {
 
$error_message .= 'The Subject you entered do not appear to be valid.<br />';
}
 
if(strlen($comments) < 2) {
 
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
 
if(strlen($error_message) > 0) {
 
died($error_message);
}
 
$email_message = "Form copy requested from mysite.
Your opinion is very important to us, we appreciate your feedback.
Thank you for contacting us, our support staff will answer your inquiries within 24 hours!\n\n";
 
function clean_string($string) {
 
$bad = array("content-type","bcc:","to:","cc:","href");
 
return str_replace($bad,"",$string);
}
 
 
$email_message .= "First Name: ".clean_string($first_name)."\n";
 
$email_message .= "Last Name: ".clean_string($last_name)."\n";
 
$email_message .= "Email: ".clean_string($email_from)."\n";
 
$email_message .= "Telephone: ".clean_string($telephone)."\n";
 
$email_message .= "Issue: ".clean_string($issue)."\n";
 
$email_message .= "Comments: ".clean_string($comments)."\n";
 
// Create email headers
$headers = 'From: '.$email_from."\r\n".
 
'Reply-To: '.$email_from."\r\n" .
 
'Cc: '.$email_from."\r\n" .
 
'X-Mailer: PHP/' . phpversion();
 
@mail($email_to, $email_subject, $email_message, $headers);
?>
  
<h2 align="center">Our support staff will answer your inquiries within 24 hours!</h2>
<p align="center"> </p>
 
<?php
}
 
?>
 
</div>
 
Link to comment
Share on other sites

  • Solution

You need to give your form fields meaningful names. "checkbox" is not a good name for a form field.

 

One glaring problem is that you have multiple fields with the same name

 

 

<input type="checkbox" name="checkbox" id= "checkbox" value="1" checked/>
<input type="submit" name="checkbox" value="Submit">
Edited by Psycho
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.