Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts

    Contact form - add checkbox

    Hi!

    I have a contactform, which I would like to add a checkbox to. I have added the part with RED color below, however it dosn't work. In the email it sends to me it dosn't write "no" or "yes" in the "Recieve answear" field". I'm a noob to php, so the error might(?) be pretty easy for you pros to spot:

    Code:
    <?php
    
    //-------------------- Verifying the fields ---
    if(isset($_REQUEST["submit"]))
    {
    $Field_bug = "";
    $Name = $_REQUEST['Name'];
    $Email = $_REQUEST['Email']; 
    $Message  = $_REQUEST['Message'];
    $Website  = $_REQUEST['Website'];
    
    	if(empty($Name))
    	{                 
        	$Field_bug.="<li>Please enter your name</li>";
    	}
    	if(empty($Message))
    	{
        	$Field_bug.="<li>Please enter your message</li>";
    	}
    	if(empty($Email))
    	{                 
        	$Field_bug.="<li>Please enter your Email address</li>";
    	}
    	if(!empty($Email))
    	{
    	$email_ok = eregi("^[_\.0-9a-z-]+@([0-9a-z-]+\.)+[a-z]{2,4}$",$Email);		
    		if(TEST_MAIL)
    		{		
    			list($user, $domaine) = split("@", $Email, 2);
    			$domain_ok = @checkdnsrr($domaine, "MX");
    		}
    		else
    		{
    		    $domain_ok = true;
    		}
    	}
    	if (!$email_ok || !$domain_ok)
    	{
    		$Field_bug.="<li><b>$Email</b> is not a valid Email address</li>";
    
    	}
    	
    	$No_mail = "$Field_bug";
    	
    	if(empty($Field_bug) && 
    	eregi("^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\\.[a-z]{2,4}$",$Email))
    	{
    		$ok = 1;
    	}
    	elseif(!empty($Field_bug) && 
    	eregi("^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\\.[a-z]{2,4}$",$Email))
    	{
    		$ok = false;
    	}
    	else
    	{
    		$Fake_mail = "<li>Please return to verify your Email address</li>";
    		$ok = false;
    	}
    
    //-------------------- Upload file(s) if any ---
    $status = "";
    $new_name = "";
    $uploaded = "";
    $get_files = "";
    $all_names = "";
    $No_upload = "";
    	
    for($i=0;$i<count($_FILES["fileup"]["tmp_name"]);$i++)
    {
    	$name=$_FILES["fileup"]["name"][$i];
    	$temp=$_FILES["fileup"]["tmp_name"][$i];
    	$size=$_FILES["fileup"]["size"][$i];
    	$type=$_FILES["fileup"]["type"][$i];
        $ext = strrchr($name,'.');
    	if (!empty($name) && ($limit_size == "yes") && ($min_size > $size))
    	{
            $No_upload.= "Error: Your file, $name ($size bytes) is to small (min: $min_size bytes).<br/>\n";
    	}
    	if (!empty($name) && ($limit_size == "yes") && ($max_size < $size))
    	{
            $No_upload.= "Error: Your file, $name ($size bytes) is to big (max: $max_size bytes).<br/>\n";
    	}
    	if (!empty($name) && ($limit_ext == "yes") && (!in_array($ext,$extensions)))
    	{
            $No_upload.= "Error: We do not allow this file-type ($ext).<br/>\n";
    	}
    	if (empty($No_upload) && $size > 0)
    	{
    	global $up_dir, $all_names;
    	$new_name = time() . "-" . $name;
    	$new_name = str_replace(" ","_",$new_name);
    	@move_uploaded_file($temp, $up_dir . $new_name);
    	$all_names.= "$new_name ($size bytes)\n";
        }
    }
    
    
    //-------------------- File upload message ---
    if(empty($all_names))
    {
    	$status = "No file sent.<br/><br/>$No_upload";
    	$uploaded = "Not this time...";
    }
    else
    {
    	$status = "Your file(s) was successfully uploaded.<br/><br/>$No_upload";
    	$uploaded = "Yes !!!";
    	$get_files = "File Info:\n$all_names\n";
    }
    
    //-------------------- Building the Email ---
    if($ok=="1")
    {
     $sentat = date("M j G:i:s O");
     if (getenv(HTTP_X_FORWARDED_FOR)){
     $ip=getenv(HTTP_X_FORWARDED_FOR);
     } else { 
     $ip=getenv(REMOTE_ADDR);
     } 
     $host = isset($REMOTE_HOST) ? $REMOTE_HOST : @gethostbyaddr($ip);
            if($host == $ip){
                $host = getenv('REMOTE_ADDR');
            }
     if (!empty($QUERY_STRING)){
     $referer = $baseurl . "?" . $QUERY_STRING;
     } else { 
     $referer = $_SERVER["HTTP_REFERER"];
     }
     $subject = "XflightX Contact";
     $body ="\nSent from: $referer";
     $body.="\n------------------------------------------------------------\n\n";
     $body.="From: $Email\n";
     $body.="Name: $Name\n";
     $body.="Site: $Website\n\n";
     $body.="Message:\n$Message\n\n";
     $body.="Answear: $Answear\n";
     $body.="Received: $sentat GMT\n";
     $body.="Host (IP): $host ($ip)"; 
     $body.="\n------------------------------------------------------------\n\n";
     $body.="File Upload: $uploaded\n\n";
     $body.="$get_files\n";
     $body.="$No_upload";
     $body.="\n------------------------------------------------------------\n\n";
     $body.="$author";
      
     if($online_isp == "1")
     {
    	if(!email("feedback",$adminmail,$subject,$body))
    	{
    	print "An error occurred during mail delivery <br/>";
    	}
     }
     elseif ($online_isp == "2")
     {
        include "mail.inc";
        if(!email($adminmail,$subject,$body))
    	{
        print "An error occurred during mail delivery <br/>";
    	}
     }
     else{
    	if(!mail($adminmail,$subject,$body,"From: $Name<$Email>\r\nReply-To: $Email\r\nReturn-Path: $adminmail"))
    	{
    	print "An error occurred during mail delivery <br/>";
    	}
     }
    
    //-------------------- Create success message ---
    ?>
    
    <br/><br/>
    <table align="center" width="<?php echo $table_width; ?>" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td class="m_head" align="left">Thank you <?php echo $Name; ?>!</td>
      </tr>
      <tr>
    	<td class="m_text"><br/>Your message has been successfully sent to <?php echo $sitename; ?> <br/> 
    	and you should receive an answer shortly, if you have requested it!<br/><br/>
        <?php echo $status; ?><br/><br/>
    	<input align="center" class="email_btn" type="button" value=" Return to form" onClick="javascript:history.go(-1);">
    	</td>
      </tr>
    </table>
    
    <?php
    }
    else
    {
    //-------------------- Create error message ---
    ?>
    
    <br/><br/>
    <table align="center" width="<?php echo $table_width; ?>" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td class="m_head" align="left">OY-10: "Copenhagen Approch, we have a problem abord..</td>
      </tr>
      <tr>
    	<td class="m_text"><br/>
    	<ul type="square">
    	<?php 
    	echo $No_mail;
    	echo $Fake_mail;
    	?>
    	</ul><br/>
    	<?php
    	echo $No_upload;
    	?>
    	<br/><br/>
    	Your message could not be processed properly.<br/> 
    	Please note the error messages above and try again.
        </td>
      </tr>
      <tr>
        <td align="center">
    	<br/><br/><br/><br/><br/><br/>
    	<input class="email_btn" type="button" value=" Return & Fix " onClick="javascript:history.go(-1);">
        </td>
      </tr>
    </table>
    
    <?php
    //-------------------- Building the Email form ---
    } 
    }
    else
    {
      $num = 0;
      $html = '';
      while($num < $UploadNum)
      {
    	$num++;
    	$html.= "  <tr><td align=\"left\"><b>Attach File #$num:</b><br/><input class=\"email_input\" size=\"40\" type=\"file\" name=\"fileup[]\" value=\"\"/></td></tr>\n";
      }
    ?>
    
    <br/>
    <form action="" method="post" enctype="multipart/form-data">
    <table class="email_table" align="center" width="90%" cellspacing="10" cellpadding="0" border="0">
      <tr>
        <td align="left"><b><img src="email/img/req.gif" width="11" height="11" 
    	alt="required" border="0" align="middle"/> Your Name:</b><br/>
        <input class="email_input" type="text" size="40" maxlength="50" name="Name" value=""/></td>
    	<td align="left"><b><img src="email/img/req.gif" width="11" height="11" 
    	alt="required" border="0" align="middle"/> Your Email:</b><br/>
        <input class="email_input" type="text" size="40" maxlength="80" name="Email" value="@"/></td>
      </tr>
      <tr>
        <td align="left"><b>Your Website:</b><br/>
        <input class="email_input" type="text" size="40" maxlength="120" name="Website" value="http://"/></td>
    	<td align="left">
    <table width="100%" cellspacing="0" cellpadding="0" border="0">
    <?php echo $html; ?>
    </table>
        </td>
      </tr>
      <tr>
        <td align="left" colspan="2"><b><img src="email/img/req.gif" width="11" height="11" 
    	alt="required" border="0" align="middle"/> Your Message:</b><br/>
        <textarea class="email_area" name="Message" cols="52" rows="8"></textarea></td>
      </tr>
    <tr>
    <td align="left"><b>I would like to recieve an answear:</b> <input class="email_input" type="checkbox" name="Answear" value="Yes" /></td>
    </tr>
      <tr>
        <td align="center"><input class="email_btn_clear" type="reset" name="reset" value=" Clear "/></td>
    	<td align="center"><input class="email_btn_send" type="submit" name="submit" value=" Send "/></td>
      </tr>
    </table>
    </form>
    <br/><br/>
    <table align="center" width="90%" cellspacing="0" cellpadding="0" border="0">
      <tr>
        <td class="m_text" align="left"><b>When including file(s), the following restrictions apply:</b><br/><br/>
    	File ext.:<?php
    	if (($extensions == "") or ($extensions == " ") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) { 
    	echo "all files allowed..."; 
    	} else { 
    	  $ext_count2 = $ext_count+1;
    	for($counter=0; $counter<$ext_count; $counter++) {
     	echo " ".$extensions[$counter];
    
            }
        }
    	?><br/>
    	File size: between <?php
    	if (($min_size == "") || ($limit_size != "yes")) {
                $min_size = "other size";
            } else {
                $min_size = round($min_size / 1024,2);
            } 
    	if (($max_size == "") || ($limit_size != "yes")) {
                $max_size = "any size";
            } else {
                $max_size = round($max_size / 1024,2);
            }
            
    		echo $min_size . " KB & " . $max_size . " KB";
    	?><br/>
    	No spaces or illegal characters (/,*,\...).</td>
      </tr>	
    </table>
    	  
    <?php
    } 
    ?>

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Hi,
    You were almost there, except you missed a few bits at the start.
    See the bits I've added in blue

    Code:
    <?php
    //-------------------- Verifying the fields ---
    if(isset($_REQUEST["submit"]))
    {
     $Field_bug = "";
     $Name = $_REQUEST['Name'];
     $Email = $_REQUEST['Email']; 
     $Message  = $_REQUEST['Message'];
     $Website  = $_REQUEST['Website'];
     $Answear  = $_REQUEST['Answear'];
     if(empty($Name))
     {                 
         $Field_bug.="<li>Please enter your name</li>";
     }
     if(empty($Message))
     {
         $Field_bug.="<li>Please enter your message</li>";
     }
     if(empty($Email))
     {                 
         $Field_bug.="<li>Please enter your Email address</li>";
     }
     if(!empty($Email))
     {
     $email_ok = eregi("^[_\.0-9a-z-]+@([0-9a-z-]+\.)+[a-z]{2,4}$",$Email);  
      if(TEST_MAIL)
      {  
       list($user, $domaine) = split("@", $Email, 2);
       $domain_ok = @checkdnsrr($domaine, "MX");
      }
      else
      {
          $domain_ok = true;
      }
     }
     if (!$email_ok || !$domain_ok)
     {
      $Field_bug.="<li><b>$Email</b> is not a valid Email address</li>";
     }
     if(empty($Answear)){
      $Answear = "no";
     }
     
     $No_mail = "$Field_bug";
     
     if(empty($Field_bug) && 
     eregi("^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\\.[a-z]{2,4}$",$Email))
     {
      $ok = 1;
     }
     elseif(!empty($Field_bug) && 
     eregi("^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\\.[a-z]{2,4}$",$Email))
     {
      $ok = false;
     }
     else
     {
      $Fake_mail = "<li>Please return to verify your Email address</li>";
      $ok = false;
     }
     //-------------------- Upload file(s) if any ---
     $status = "";
     $new_name = "";
     $uploaded = "";
     $get_files = "";
     $all_names = "";
     $No_upload = "";
      
     for($i=0;$i<count($_FILES["fileup"]["tmp_name"]);$i++)
     {
      $name=$_FILES["fileup"]["name"][$i];
      $temp=$_FILES["fileup"]["tmp_name"][$i];
      $size=$_FILES["fileup"]["size"][$i];
      $type=$_FILES["fileup"]["type"][$i];
      $ext = strrchr($name,'.');
      if (!empty($name) && ($limit_size == "yes") && ($min_size > $size))
      {
       $No_upload.= "Error: Your file, $name ($size bytes) is to small (min: $min_size bytes).<br/>\n";
      }
      if (!empty($name) && ($limit_size == "yes") && ($max_size < $size))
      {
       $No_upload.= "Error: Your file, $name ($size bytes) is to big (max: $max_size bytes).<br/>\n";
      }
      if (!empty($name) && ($limit_ext == "yes") && (!in_array($ext,$extensions)))
      {
       $No_upload.= "Error: We do not allow this file-type ($ext).<br/>\n";
      }
      if (empty($No_upload) && $size > 0)
      {
      global $up_dir, $all_names;
      $new_name = time() . "-" . $name;
      $new_name = str_replace(" ","_",$new_name);
      @move_uploaded_file($temp, $up_dir . $new_name);
      $all_names.= "$new_name ($size bytes)\n";
      }
     }
    
     //-------------------- File upload message ---
     if(empty($all_names))
     {
      $status = "No file sent.<br/><br/>$No_upload";
      $uploaded = "Not this time...";
     }
     else
     {
      $status = "Your file(s) was successfully uploaded.<br/><br/>$No_upload";
      $uploaded = "Yes !!!";
      $get_files = "File Info:\n$all_names\n";
     }
     //-------------------- Building the Email ---
     if($ok=="1")
     {
      $sentat = date("M j G:i:s O");
      if (getenv(HTTP_X_FORWARDED_FOR)){
      $ip=getenv(HTTP_X_FORWARDED_FOR);
      } else { 
      $ip=getenv(REMOTE_ADDR);
      } 
      $host = isset($REMOTE_HOST) ? $REMOTE_HOST : @gethostbyaddr($ip);
       if($host == $ip){
        $host = getenv('REMOTE_ADDR');
       }
      if (!empty($QUERY_STRING)){
      $referer = $baseurl . "?" . $QUERY_STRING;
      } else { 
      $referer = $_SERVER["HTTP_REFERER"];
      }
      $subject = "XflightX Contact";
      $body ="\nSent from: $referer";
      $body.="\n------------------------------------------------------------\n\n";
      $body.="From: $Email\n";
      $body.="Name: $Name\n";
      $body.="Site: $Website\n\n";
      $body.="Message:\n$Message\n\n";
      $body.="Answer: $Answer\n";
      $body.="Received: $sentat GMT\n";
      $body.="Host (IP): $host ($ip)"; 
      $body.="\n------------------------------------------------------------\n\n";
      $body.="File Upload: $uploaded\n\n";
      $body.="$get_files\n";
      $body.="$No_upload";
      $body.="\n------------------------------------------------------------\n\n";
      $body.="$author";
       
      if($online_isp == "1")
      {
      if(!email("feedback",$adminmail,$subject,$body))
      {
      print "An error occurred during mail delivery <br/>";
      }
      }
      elseif ($online_isp == "2")
      {
      include "mail.inc";
      if(!email($adminmail,$subject,$body))
      {
      print "An error occurred during mail delivery <br/>";
      }
      }
      else{
      if(!mail($adminmail,$subject,$body,"From: $Name<$Email>\r\nReply-To: $Email\r\nReturn-Path: $adminmail"))
      {
      print "An error occurred during mail delivery <br/>";
      }
      }
     //-------------------- Create success message ---
     ?>
     <br/><br/>
     <table align="center" width="<?php echo $table_width; ?>" border="0" cellspacing="0" cellpadding="0">
       <tr>
      <td class="m_head" align="left">Thank you <?php echo $Name; ?>!</td>
       </tr>
       <tr>
      <td class="m_text"><br/>Your message has been successfully sent to <?php echo $sitename; ?> <br/> 
      and you should receive an answer shortly, if you have requested it!<br/><br/>
      <?php echo $status; ?><br/><br/>
      <input align="center" class="email_btn" type="button" value=" Return to form" onClick="javascript:history.go(-1);">
      </td>
       </tr>
     </table>
     <?php
     }
     else
     {
     //-------------------- Create error message ---
     ?>
     <br/><br/>
     <table align="center" width="<?php echo $table_width; ?>" border="0" cellspacing="0" cellpadding="0">
       <tr>
      <td class="m_head" align="left">OY-10: "Copenhagen Approch, we have a problem abord..</td>
       </tr>
       <tr>
      <td class="m_text"><br/>
      <ul type="square">
      <?php 
      echo $No_mail;
      echo $Fake_mail;
      ?>
      </ul><br/>
      <?php
      echo $No_upload;
      ?>
      <br/><br/>
      Your message could not be processed properly.<br/> 
      Please note the error messages above and try again.
      </td>
       </tr>
       <tr>
      <td align="center">
      <br/><br/><br/><br/><br/><br/>
      <input class="email_btn" type="button" value=" Return & Fix " onClick="javascript:history.go(-1);">
      </td>
       </tr>
     </table>
     <?php
     //-------------------- Building the Email form ---
     } 
    }
    else
    {
      $num = 0;
      $html = '';
      while($num < $UploadNum)
      {
     $num++;
     $html.= "  <tr><td align=\"left\"><b>Attach File #$num:</b><br/><input class=\"email_input\" size=\"40\" type=\"file\" name=\"fileup[]\" value=\"\"/></td></tr>\n";
      }
    ?>
    <br/>
    <form action="" method="post" enctype="multipart/form-data">
    <table class="email_table" align="center" width="90%" cellspacing="10" cellpadding="0" border="0">
      <tr>
        <td align="left"><b><img src="email/img/req.gif" width="11" height="11" 
     alt="required" border="0" align="middle"/> Your Name:</b><br/>
        <input class="email_input" type="text" size="40" maxlength="50" name="Name" value=""/></td>
     <td align="left"><b><img src="email/img/req.gif" width="11" height="11" 
     alt="required" border="0" align="middle"/> Your Email:</b><br/>
        <input class="email_input" type="text" size="40" maxlength="80" name="Email" value="@"/></td>
      </tr>
      <tr>
        <td align="left"><b>Your Website:</b><br/>
        <input class="email_input" type="text" size="40" maxlength="120" name="Website" value="<A href="http://"/></td">http://"/></td>
     <td align="left">
    <table width="100%" cellspacing="0" cellpadding="0" border="0">
    <?php echo $html; ?>
    </table>
        </td>
      </tr>
      <tr>
        <td align="left" colspan="2"><b><img src="email/img/req.gif" width="11" height="11" 
     alt="required" border="0" align="middle"/> Your Message:</b><br/>
        <textarea class="email_area" name="Message" cols="52" rows="8"></textarea></td>
      </tr>
    <tr>
    <td align="left"><b>I would like to recieve an answear:</b> <input class="email_input" type="checkbox" name="Answear" value="Yes" /></td>
    </tr>
      <tr>
        <td align="center"><input class="email_btn_clear" type="reset" name="reset" value=" Clear "/></td>
     <td align="center"><input class="email_btn_send" type="submit" name="submit" value=" Send "/></td>
      </tr>
    </table>
    </form>
    <br/><br/>
    <table align="center" width="90%" cellspacing="0" cellpadding="0" border="0">
      <tr>
        <td class="m_text" align="left"><b>When including file(s), the following restrictions apply:</b><br/><br/>
     File ext.:<?php
     if (($extensions == "") or ($extensions == " ") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) { 
     echo "all files allowed..."; 
     } else { 
       $ext_count2 = $ext_count+1;
     for($counter=0; $counter<$ext_count; $counter++) {
      echo " ".$extensions[$counter];
            }
        }
     ?><br/>
     File size: between <?php
     if (($min_size == "") || ($limit_size != "yes")) {
                $min_size = "other size";
            } else {
                $min_size = round($min_size / 1024,2);
            } 
     if (($max_size == "") || ($limit_size != "yes")) {
                $max_size = "any size";
            } else {
                $max_size = round($max_size / 1024,2);
            }
            
      echo $min_size . " KB & " . $max_size . " KB";
     ?><br/>
     No spaces or illegal characters (/,*,\...).</td>
      </tr> 
    </table>
       
    <?php
    } 
    ?>
    My first edit reads the "answer" variable from the global request object.
    If a checkbox isn't checked then you don't actually get sent a reference to the checkbox so you need to add in the code to generate the "No" (see my second edit).

    Also, in English, "Answear" is spelt "Answer" and "Recieve" is spelt "Receive", so you might want to edit your scipt accordingly. Hope you don't mind me correcting you!

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts
    Hi sir!

    Thank you, now it works


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •