0

我以前从未遇到过这种情况,临时没有 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";
?>
4

2 回答 2

3

你是对的,“发送消息”是一个链接。所以它就像任何链接一样,而不是提交表单。

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

于 2013-06-27T13:43:45.590 回答
1

你有过 if(!$name || !$company || !$email)

我将其替换为:

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

这将检查任何空字段。

注意

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

这是经过测试的代码:

形式:

<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";
}

?>
于 2013-06-27T14:23:37.377 回答