Html表单问题

时间:2014-05-03 16:25:36

标签: php html

这是我想在我的网页上使用的代码。我显然忽略了这一点。首先,我不是PHP程序员,所以我通过一些教程制作这些代码,并在互联网上找到一些代码。


                        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name=”viewport” content=”width=device-width, initial-scale=1, maximum-scale=1″>
            <title>Untitled Document</title>
             <link href="style.css" rel="stylesheet" type="text/css" />

             <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
             <script src="jquery/js/bjqs-1.3.min.js"></script>
            </head>

            <body>

              <div id="content">
                <div id="content_left">
              <form class="form" name="htmlform" method="post" action="html_form_send.php" target="_blank">
            <table width="600px">
            <td width="271"></tr>
            <tr>
             <td valign="top">
              <label for="first_name">Ime ili naziv tvrtke</label>
             </td>
             <td width="317" valign="top">
              <input  type="text" name="first_name" maxlength="50" size="40">
             </td>
            </tr>

            <tr>
             <td valign="top">
              <label for="last_name">Prezime </label>
             </td>
             <td valign="top">
              <input  type="text" name="last_name" maxlength="50" size="40">
             </td>
            </tr>
            <tr>
             <td valign="top">
              <label for="email">Email </label>
             </td>
             <td valign="top">
              <input  type="text" name="email" maxlength="80" size="40">
             </td>

            </tr>
            <tr>
             <td valign="top">
              <label for="telephone">Telefon ili Mobitel</label></td>
             <td valign="top">
              <input  type="text" name="telephone" maxlength="30" size="40">
             </td>
            </tr>
            <tr>
             <td valign="top">
              <label for="comments">Poruka </label>
             </td>
             <td valign="top">
              <textarea  name="comments" maxlength="1000" cols="50" rows="6"></textarea>
             </td>

            </tr>
            <tr>
             <td colspan="2" style="text-align:center">
              <input type="submit" value="Pošalji">   
             </td>
            </tr>
            </table>
            </form>


                </div>



            </body>
            </html>

和我的php

        <html>
        <head>
        <title>Application Submitted</title>

        <?php
        header('Content-Type: text/html; charset=utf-8');
        if(isset($_POST['email'])) {

            // CHANGE THE TWO LINES BELOW
            $email_to = "mbulic@live.com";

            $email_subject = "website html form submissions";


            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']) ||
                !isset($_POST['telephone']) ||
                !isset($_POST['comments'])) {
                died('We are sorry, but there appears to be a problem with the form you submitted.');       
            }

            $first_name = $_POST['first_name']; // required
            $last_name = $_POST['last_name']; // required
            $email_from = $_POST['email']; // required
            $telephone = $_POST['telephone']; // not 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($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 .= "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 .= "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);  
        ?>

        <!-- place your own success html below -->

        Vaš zahtjev je zaprimljen. Bit ćete kontaktiranu u roku od 24h.

        <?php
        }
        die();
        ?>

        </head>
        <body>
        </body>
        </html>

这是我得到的错误:

![enter image description here][1]

'"; echo $error."

"; echo "Please go back and fix these errors.

"; exit; } // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) { error('We are sorry, but there appears to be a problem with the form you submitted.'); exit; } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; // not 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.
'; } $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.
'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.
'; } 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 .= "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 .= "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); ?> Vaš zahtjev je zaprimljen. Bit ćete kontaktiranu u roku od 24h.' sry this is the code

1 个答案:

答案 0 :(得分:2)

尝试删除header('Content-Type: text/html; charset=utf-8');,然后确保所有PHP所在的文件是PHP文件。 (yourfile.php)

试试这个:

<?php
if(isset($_POST['email'])) {

// CHANGE THE TWO LINES BELOW
$email_to = "mbulic@live.com";

$email_subject = "website html form submissions";


function error($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 />";
exit;
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
error('We are sorry, but there appears to be a problem with the form you submitted.');
exit;     
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not 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($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 .= "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 .= "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);  
?>

<!-- place your own success html below -->

Vaš zahtjev je zaprimljen. Bit ćete kontaktiranu u roku od 24h.

<?php
}
?>

我测试了这段代码,它对我来说很好。