带有复选框和验证的PHP表单无效

时间:2015-08-31 21:46:40

标签: php forms email

我是新来的,我根本不懂php,但我需要做一个带复选框和验证的联系表格,这是我的代码(是一个frankenstein代码)。 我需要我的表单发送一封电子邮件,其中包含避免垃圾邮件和丢失信息的信息,邮件到达后的所有详细信息都在标题中,如邮件回复,主题和所有内容。

如果你能发一个正确的代码就会很精彩。 提前谢谢

<form name="htmlform" method="post" action="app.php">
  <label for="name">*Nombre Completo</label>
  <input  type="text" name="name" maxlength="150" size="50">
  <label for="company">*Empresa</label>
  <input  type="text" name="company" maxlength="150" size="50">
  <label for="telephone">Teléfono</label>
  <input  type="text" name="telephone" maxlength="150" size="50">
  <label for="email">*Email</label>
  <input  type="text" name="email" maxlength="180" size="50">
  <label for="services">*Servicios de Interes</label>

<input type="checkbox" name="services[]" value="apps" />apps<br />

<input type="checkbox" name="services[]" value="Marketing Movil" />Marketing Móvil<br />

<input type="checkbox" name="services[]" value="video juegos" />video Juegos<br />

  <label for="comments">*Comentarios</label>

  <textarea  name="comments" maxlength="1000" cols="40" rows="6"></textarea>
  <div class="hide">Leave this empty:<input name="url" /></div>

  <center><input class="button large" type="submit" value="Submit Form"></center>

</form>





<?php
// if the url field is empty
if(isset($_POST['url']) && $_POST['url'] == '')
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "my@mail.com";
    $email_subject = "Contact from web.com";



    function died($error) {
        // your error code can go here
        echo "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['full_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['services']) ||
        !isset($_POST['comments']))
         {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $full_name = $_POST['full_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // required
    $services = $_POST['services']; // 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,$full_name)) {
    $error_message .= 'The First Name you entered does 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 details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Full Name: ".clean_string($full_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Services: ".clean_string($services)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";



// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  

 //  hacker defense
function clean($var){//request string cleaner
    if(get_magic_quotes_gpc()) $var=stripslashes($var); //clean
    $var=mysql_real_escape_string($var); //clean
    return strip_tags($var, '<b><a>');//returning clean var
}

function hackerDefense(){//thanks to Allen Sanford
        // begin hacker defense       
        foreach ($_POST as &$postvalue){    //checking posts
            $postvalue = clean($postvalue);//cleaning the value
        }
} // end hacker defense


?>

<!-- include your own success html here -->
<center>
<img src="images/logo.png" />
  <br>
  <br>
  Thank you for contacting us.  We will be in touch.<br>
<br>
 <a href="index.html">HOME</a></center>
<?php
}
?>

0 个答案:

没有答案