为什么我的联系表单在html中,php无效?

时间:2014-10-09 05:50:06

标签: php send

您好我的联系表格有问题我没有收到任何邮件,我不知道为什么......

这是我的PHP代码有什么问题

 <?php
$name=$_POST ['name'];
$mobileno=$_POST ['mobileno'];
$email=$_POST ['email'];
$message=$_POST ['message'];

$to = 'any mail.com';
$subject = ' Message from your Webite';
$msg = " Your name : $name\n"; 
$msg .= " Your Mobile No. : $mobileno\n"; 
$msg .= " Your email: $email\n" ;
$msg .= " Your Message: $message";

mail ($to, $subject, $msg, 'From:' . $email);

echo ' Thank You <br/>';
echo ' Your name ' . $name . '<br>';
echo ' Your email ' . $email . '<br>';
echo ' Your Mobile No. ' . $mobileno . '<br>';
echo ' Your Message ' . $message . '<br>';

?>

<br /><br />
 Here is the result from $msg: <br /><br />
 <?php 

 echo $msg;

 ?>

我的HTML代码我已多次编辑代码但没有发生任何事情

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

<body>

<form name="form1" method="post" action="justsend.php">

  <p>

    <label for="mobileno">your mobile</label>

    <input type="text" name="mobileno" id="mobileno">

    <label for="name"><br>
      your name</label>
    <input type="text" name="name" id="name">
  </p>
  <p>
    <label for="email">your email</label>
    <input type="text" name="email" id="email">
  </p>
  <p>
    <label for="message">your message</label>
    <textarea name="message" id="message" cols="45" rows="5"></textarea>
  </p>
  <p>
    <label for="send">send msg</label>
    <input type="submit" name="send" id="send" value="Submit">
  </p>
</form>
</body>
</html>

4 个答案:

答案 0 :(得分:1)

$_POST ['name'];应该$_POST['name'];删除帖子值中的所有空格。

这样:

$name=$_POST ['name'];
$mobileno=$_POST ['mobileno'];
$email=$_POST ['email'];
$message=$_POST ['message'];

变为:

$name=$_POST['name'];
$mobileno=$_POST['mobileno'];
$email=$_POST['email'];
$message=$_POST['message'];
和EternalHour说的一样,验证表单是否以某种方式填写可能是一个好主意;在您宣布$name$mobileno等之后立即尝试;

if(!empty($name) && !empty($mobileno) && !empty($email) && !empty($message)){
 // do stuff
}

您使用!empty()而不是isset()的原因是因为如果提交表单,isset仍然会返回true,因为值已设置..它们只是空的。

修改 进一步将HTML更改action="get"改为action="post",以使其正常运行。

答案 1 :(得分:0)

您需要修复$_POST个变量,但我建议您这样做,以便知道您的POST是否成功。

<?php

if (isset($_POST)) {
$name=$_POST['name'];
$mobileno=$_POST['mobileno'];
$email=$_POST['email'];
$message=$_POST['message'];

$to = 'any mail.com';
$subject = ' Message from your Webite';
$msg = " Your name : $name\n"; 
$msg .= " Your Mobile No. : $mobileno\n"; 
$msg .= " Your email: $email\n" ;
$msg .= " Your Message: $message";

mail ($to, $subject, $msg, 'From:' . $email);

echo ' Thank You <br/>';
echo ' Your name ' . $name . '<br>';
echo ' Your email ' . $email . '<br>';
echo ' Your Mobile No. ' . $mobileno . '<br>';
echo ' Your Message ' . $message . '<br>';

?>

<br /><br />
 Here is the result from $msg: <br /><br />
 <?php 

 echo $msg;
} else {
echo "submit failed!";
exit;
}
?>

通过这种方式,您可以确定表单中是否没有正确提交的消息。

答案 2 :(得分:0)

首先,如果这是在本地主机上,它不会工作,因为本地禁用邮件功能,你必须在线运行

此外: 我没有看到你出错的地方,但是我接管了自己的邮件功能,我在自己的网站上将其转换为你的邮件

if(isset($_POST['send'])){  // checks if the data from the submit button with the name send is here
$name=mysqli_real_escape_string($con,$_POST['name']);
$mobileno=mysqli_real_escape_string($con,$_POST['mobileno']);
$email=mysqli_real_escape_string($con,$_POST['email']);
$message=mysqli_real_escape_string($con,$_POST['message']);

// you had spaces between your post variables which wont work. Also if you use mysqli, its better to play safe and make them safe, in case you do also database work. The variable $con is your connection information.

// echo out if your variables working. For example:
// echo $mobileno;
// this way you know for sure if the items are set.    

$message = wordwrap($message, 70); // in case its a long message, this allows 70 characters on one line
$to = 'any mail.com';   // your email
$subject = ' Message from your Webite';
$msg = " Your name : ".$name."\n"; 
$msg .= " Your Mobile No. : ".$mobileno."\n"; 
$msg .= " Your email: ".$email."\n" ;
$msg .= " Your Message:".$message ;

$headers = "MIME-Version: 1.0" . "\r\n"; // this has to do with css and html in your msg
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";  // this has to do with css and html in your msg
$headers .= "From: noreply@yourwebsite.com"; // change this to whatever you want it to come from

if (mail ($to, $subject, $msg, $headers)) {
    echo ' Thank You <br/>';
    echo ' Your name ' . $name . '<br>';
    echo ' Your email ' . $email . '<br>';
    echo ' Your Mobile No. ' . $mobileno . '<br>';
    echo ' Your Message ' . $message . '<br>';

// to make sure if your email is send it will output your message as given above, else an error
}else{
    echo ' Something went wrong ... oopsie daisy'; 
    } // closure of the if mail check
} // closure of the complete if(isset) check

不要忘记包含你的连接信息以及mysqli函数来清理变量,以防你将它添加到数据库中。无论如何,这也是一个很好的做法。我将$ con定义为连接信息,你的可能会有所不同。您必须将其更改为连接变量。

如果您使用mysql_函数,请使用mysql_real_escape_string($_POST['YourPostVariable']);而不使用连接信息。看这里。 http://php.net/manual/en/function.mysql-real-escape-string.php

- 编辑 -

更新它,因为你的消息不会输出你的变量,只输出$ name而不是$ name变量中的名字。

希望这有帮助,祝你好运调试:)

答案 3 :(得分:0)

您好我刚刚修改了您的代码..我收到了电子邮件。请查看以下代码。请在文本框中提供您的电子邮件以接收电子邮件

<?php
if($_POST['email'] != '')
{
    $name=$_POST['name'];
    $mobileno=$_POST['mobileno'];
    $email=$_POST['email'];
    $message=$_POST['message'];
    $to = $email;
    $subject = 'Message from your Webite';
    $msg = " Your name : $name\n"; 
    $msg .= " Your Mobile No. : $mobileno\n"; 
    $msg .= " Your email: $email\n" ;
    $msg .= " Your Message: $message";
    mail ($to, $subject, $msg, 'From:'.$email);
    echo ' Thank You <br/>';
    echo ' Your name ' . $name.'<br>';
    echo ' Your email ' . $email .'<br>';
    echo ' Your Mobile No. ' . $mobileno . '<br>';
    echo ' Your Message ' . $message . '<br>';
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

<body>

<form name="form1" method="post" action="justsend.php">

  <p>
    <label for="mobileno">your mobile</label>

    <input type="text" name="mobileno" id="mobileno">

    <label for="name"><br>
      your name</label>
    <input type="text" name="name" id="name">
  </p>
  <p>
    <label for="email">your email</label>
    <input type="text" name="email" id="email">
  </p>
  <p>
    <label for="message">your message</label>
    <textarea name="message" id="message" cols="45" rows="5"></textarea>
  </p>
  <p>
    <label for="send">send msg</label>
    <input type="submit" name="send" id="send" value="Submit">
  </p>
</form>
</body>
</html>