试图修复我的联系表格

时间:2013-11-28 08:03:06

标签: php html wordpress

我一直试图找出代码中缺少的内容。基本上每当我点击提交按钮发送联系表单中的数据时,它基本上会将我返回到主页并在选项卡中显示“找不到页面”。

这是我的代码

<?php 
$error=false;
$sent=false;
if(isset($_POST['submit'])) {
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
    $error = true;
} else {

 $to = "noel@digitalspin.ph";

 $name = trim($_POST['name']);
 $email = trim($_POST['email']);
 $message = trim($_POST['message']);


 $messages ="\r\n Name: $name \r\n Email: $email \r\n Message: $message";
 $headers = "From:" . $name;
 $mailsent = mail($to, $subject, $message, $headers);

 if($mailsent){
     $sent= true;
    }
  }
}
?>

这是我的标记代码

<div class="col10">
<div class="contact_form">
<h2>Inquiries/Comments/Suggestions</h2>
<form id="register-form" action="" method="post">

    <?php if($error == true) { ?>
    <p class="error"> There are some misisng fields.</p>
    <?php } if($sent == true) { ?>
    <p class="sent">Thank you for sending your message</p><?php } ?>

    <div class="contact-form">
    <input type="text" name="name" placeholder="Name">

    <input type="text" name="email" placeholder="Email">

    <textarea name="message" placeholder="Message"></textarea>

    <label>Where did you learn about us?</label>

    <select>
        <option>Word of Mouth</option>
        <option>Billboards</option>
        <option>Roadshows</option>
        <option>Sales agents</option>
        <option>TV ads</option>
        <option>Booths/Exhibits</option>
        <option>Print Ads</option>
        <option>Internet</option>
        <option>Direct Mail</option>
        <option>Flyer</option>
        <option>Referral</option>
        <option>Others</option>
    </select>

    <input type="submit" value="send" name="submit">


</form>
</div>
</div>

</div>

6 个答案:

答案 0 :(得分:1)

action属性中写入负责执行请求的路径,而不是将其留空。将action属性留空会将基本路径(通常是主页)视为提交路径。

<form id="register-form" action="./contact.php" method="post">

(假设./contact.php是执行表单提交请求的负责(和相对)路径。)

答案 1 :(得分:0)

尝试制作你的

 <from method="POST" action="contact.php"> 

而不是

 <form method="POST" action="">

或删除 action =“”

答案 2 :(得分:0)

您必须在表单中指定一个操作,如:

<form id="register-form" action="your_file.php" method="post">

之后您的脚本将被执行,因此您可以输出“Email sended”或重定向到新页面等。

答案 3 :(得分:0)

您可以尝试这样

<?php 
$error=false;
if(isset($_POST['submit'])) {
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
    $error = true;
} else {

 $name = trim($_POST['name']);
 $email = trim($_POST['email']);
 $message = trim($_POST['message']);
 $to = "noel@digitalspin.ph";

 $headers=array(
        'MIME-Version: 1.0' . "\r\n",
        'From: '.$to,
        'Content-Type:text/html',
        'Reply-To: noel@digitalspin.ph'
    );
 $subject = "your subject";

 $messages ="\r\n Name: $name \r\n Email: $email \r\n Message: $message";

 if (!mail($email,$subject,$messages,implode("\r\n",$headers))) {
            mail($to, 'Error sending', 'The following order was not sent:&nbsp;');

        }


}
}
?>


<div class="col10">
<div class="contact_form">
<h2>Inquiries/Comments/Suggestions</h2>
<form id="register-form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

    <?php if($error == true) { ?>
    <p class="error"> There are some misisng fields.</p>
    <?php } if($sent == true) { ?>
    <p class="sent">Thank you for sending your message</p><?php } ?>

    <div class="contact-form">
    <input type="text" name="name" placeholder="Name">

    <input type="text" name="email" placeholder="Email">

    <textarea name="message" placeholder="Message"></textarea>

    <label>Where did you learn about us?</label>

    <select>
        <option>Word of Mouth</option>
        <option>Billboards</option>
        <option>Roadshows</option>
        <option>Sales agents</option>
        <option>TV ads</option>
        <option>Booths/Exhibits</option>
        <option>Print Ads</option>
        <option>Internet</option>
        <option>Direct Mail</option>
        <option>Flyer</option>
        <option>Referral</option>
        <option>Others</option>
    </select>

    <input type="submit" value="send" name="submit">


</form>
</div>
</div>

</div>

答案 4 :(得分:0)

使用<?php echo $_SERVER['PHP_SELF']; ?>

<form id="register-form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

答案 5 :(得分:0)

your problem is the name of the below html field :

<input type="text" name="name" placeholder="Name">

Instead of giving name="name" try giving it name="your_name"

And the from action = "<?php the_permalink(); ?>"

Try the below code, that will work for you:

<div class="col10">
<div class="contact_form">
<h2>Inquiries/Comments/Suggestions</h2>
<form id="register-form" action="<?php the_permalink(); ?>" method="post">

    <?php if($error == true) { ?>
    <p class="error"> There are some misisng fields.</p>
    <?php } if($sent == true) { ?>
    <p class="sent">Thank you for sending your message</p><?php } ?>

    <div class="contact-form">
    <input type="text" name="your_name" placeholder="Name">

    <input type="text" name="email" placeholder="Email">

    <textarea name="message" placeholder="Message"></textarea>

    <label>Where did you learn about us?</label>

    <select>
        <option>Word of Mouth</option>
        <option>Billboards</option>
        <option>Roadshows</option>
        <option>Sales agents</option>
        <option>TV ads</option>
        <option>Booths/Exhibits</option>
        <option>Print Ads</option>
        <option>Internet</option>
        <option>Direct Mail</option>
        <option>Flyer</option>
        <option>Referral</option>
        <option>Others</option>
    </select>

    <input type="submit" value="send" name="submit">


</form>
</div>
</div>

</div>