PHP表单提交返回空白

时间:2013-01-15 03:35:42

标签: php jquery html

我是一个新手,所以请耐心等待我:))我设法创建了一个表单,并使用PHP将数据发送到电子邮件地址。但是,一旦我点击提交;屏幕变为空白而不是停留在当前页面上并显示消息。我猜我错过了某种PHP代码?

另外,我想在我的表单上使用JQuery验证器插件,如何在不基本搞砸表单的情况下添加它?

我的HTML:

<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
                <div>
                    <label for="name">Name: </label>
                    <input type="text" name="name" id="name" placeholder="John Smith" required/>
                </div>

                <div>
                    <label for="email">Email: </label>
                    <input type="email" name="email" id="email" placeholder="name@mail.com" required/>
                </div>

                <div>
                    <label for="message">Message: </label>
                    <textarea name="message" id="message" rows="5" cols="30"></textarea>
                </div>

                <div>
                    <input id="submit" type="submit" name="submit" value="submit" />
                </div>
            </form>
            <p id="feedback"><?php echo $feedback; ?></p>
</div>

我的PHP:

<?php

$to = 'example@gmail.com';
$subject = 'Message from The Rocket Factory';

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

$body = <<<EMAIL

Hi, my name is $name.

$message

From $name
My Address is $email


EMAIL;

$header = "From: $email";

if($_POST){
    mail($to, $subject, $body, $header);
    $feedback = 'Thanks for your message';
}

?>

3 个答案:

答案 0 :(得分:1)

您创建的

PHP脚本将返回一个空页面,因为该脚本只是为了发送电子邮件。我认为您需要将PHP脚本和HTML脚本与PHP脚本结合在一起,以获得您想要的内容,并将表单操作编辑为空,如下所示:

<?php

$to = 'example@gmail.com';
$subject = 'Message from The Rocket Factory';

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

$body = <<<EMAIL

Hi, my name is $name.

$message

From $name
My Address is $email


EMAIL;

$header = "From: $email";

if($_POST){
    mail($to, $subject, $body, $header);
    $feedback = 'Thanks for your message';
}

?>

<div>
<form id="form_id" name="form_name" action="" method="post">
                <div>
                    <label for="name">Name: </label>
                    <input type="text" name="name" id="name" placeholder="John Smith" required/>
                </div>

                <div>
                    <label for="email">Email: </label>
                    <input type="email" name="email" id="email" placeholder="name@mail.com" required/>
                </div>

                <div>
                    <label for="message">Message: </label>
                    <textarea name="message" id="message" rows="5" cols="30"></textarea>
                </div>

                <div>
                    <input id="submit" type="submit" name="submit" value="submit" />
                </div>
            </form>
            <p id="feedback"><?php echo $feedback; ?></p>
</div>

答案 1 :(得分:0)

您的表单会将用户带到scripts / index.php。您正在使用HTML表单回显页面上的“$ feedback”变量。使用
来从scripts / index.php重定向         header(“location:filelocation”);         出口();

答案 2 :(得分:0)

您可以通过两种方式实现这一目标: 1.在一个页面中有php和html代码。 2.使用ajax提交表单。

 <div>
    <form id="form_id" name="form_name" action="scripts/index.php" method="post">
            <div>
                <label for="name">Name: </label>
                <input type="text" name="name" id="name" placeholder="John Smith" required/>
            </div>

            <div>
                <label for="email">Email: </label>
                <input type="email" name="email" id="email" placeholder="name@mail.com" required/>
            </div>

            <div>
                <label for="message">Message: </label>
                <textarea name="message" id="message" rows="5" cols="30"></textarea>
            </div>

            <div>
                <input id="submit" type="submit" name="submit" value="submit" />
            </div>
        </form>
 </div>

   <?php

     $to = 'example@gmail.com';
     $subject = 'Message from The Rocket Factory';

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

      $body = <<<EMAIL
         Hi, my name is $name.
           $message
         From $name
        My Address is $email
       EMAIL;

  $header = "From: $email";

if($_POST){
     mail($to, $subject, $body, $header);
    $feedback = 'Thanks for your message';
    echo '<p id="feedback">'.$feedback.'</p>'; <-- Notice this..
 }
?>

您还可以在jquery($ .ajax)或javascript中使用ajax。