联系PHP不工作

时间:2013-06-27 13:40:31

标签: php html forms contact

我之前从未遇到过这个问题,临时没有使用php,所以我尝试自己编写一个非常简单的代码。发送消息按钮似乎是一个链接,但不提交。你愿意为我检查一下吗?这是HTML

<form method="post" action="contact.php">
<div class="row half">
    <div class="6u">
        <input name="name" placeholder="Name" type="text" class="text" />
    </div>
    <div class="6u">
        <input name="email" placeholder="Email" type="text" class="text" />
    </div>
</div>
<div class="row half">
    <div class="12u">
        <textarea name="message" placeholder="Message"></textarea>
    </div>
</div>
<div class="row half">
    <div class="12u">
    <a href="#" class="button button-icon icon icon-envelope">Send Message</a>
</div>

然后是我的php

<?php
   $name    = $_POST ['name'];
   $email   = $_POST ['email'];
   $message = $_POST ['message'];
   if(!$name || !$company || !$email)
   {
       echo "
<h2>Feedback</h2>
<form method='post' action='contact.php'>
   Name: <br /><input name='name' type='text' value='$name' /><br /><br />
   EMail: <br /><input name='email' type='text' value='$email' /><br /><br />
   Message:<br />
   <textarea name='message' cols='55' rows='8'>$message</textarea><br /><br />
   <input type='submit' value='Send' />
</form>
<p>All fields are required</p>
    ";
       exit;
   }
$send_to = 'sean@seankonig.co.za'; // change to your email
mail($send_to, "Name: $name" , $message, "From: $email");
echo "Thank you for your feedback";
?>

2 个答案:

答案 0 :(得分:3)

你是对的,“发送消息”是一个链接。所以它只是表现得像任何链接,而不是提交表格。

要提交表单,您要将其更改为<input type="submit"><input type="button">,或在javascript中创建click事件处理程序以提交表单。

答案 1 :(得分:1)

您有 if(!$name || !$company || !$email)

我将其替换为:

if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))

检查任何空字段。

注意

您可以在表单中为company添加额外字段,并在PHP表单处理程序中添加empty($_POST['company']) ||

这是经过测试的代码:

<强>形式:

<form method="post" action="contact.php">
<div class="row half">
    <div class="6u">
        <input name="name" placeholder="Name" type="text" class="text" />
    </div>
    <div class="6u">
        <input name="email" placeholder="Email" type="text" class="text" />
    </div>
</div>
<div class="row half">
    <div class="12u">
        <textarea name="message" placeholder="Message"></textarea>
    </div>
</div>
<div class="row half">
    <div class="12u">

<input type="submit" value="Send Message" name="submit_button" class="button button-icon icon icon-envelope">

    </div>
</form>

<强>处理程序:

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

if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))

 {
       echo "
<h2>Feedback</h2>
<form method='post' action='contact.php'>
   Name: <br /><input name='name' type='text' value='$name' /><br /><br />
   EMail: <br /><input name='email' type='text' value='$email' /><br /><br />
   Message:<br />
   <textarea name='message' cols='55' rows='8'>$message</textarea><br /><br />
   <input type='submit' value='Send' />
</form>
<p>All fields are required</p>
    ";
       exit;
   }

else {
$send_to = 'email@example.com'; // change to your email
mail($send_to, "Name: $name" , $message, "From: $email");

echo "Thank you for your feedback";
}

?>