似乎无法让PHP表单显示

时间:2015-05-05 04:10:13

标签: php post

我正在处理我的投资组合网站的第2版,我有一个工作邮件,我在一年前创建了一个指南,转移了它,现在无法让我的页面显示。

以下是我正在使用的代码:

<?php
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
       {
                //send email
            $name = $_REQUEST['name'] ;
            $email = $_REQUEST['email'] ;
            $subject = $_REQUEST["My_Portfolio_Website"] ;
            $message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;

            echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";

        }else{
        //if "email" is not filled out, display the form
            echo <form method="post" action="contact.php" class="connect">
          <div>
            <input id="name" name="name" type="text" required>
            <label for="name">Your Name</label>
          </div>
          <div>
            <input id="email" name="email" type="text" required>
            <label for="email">Your Email</label>
          </div>
          <div>
            <textarea id="message" name="message" required></textarea>
            <label for="message">Your Message</label>
          </div>
          <div class="metro">
            <div class="metro-button" type="submit">Click me</div> 
            </div>
        </form>

        } 
?>

我通过PHP语法检查程序运行它并没有提取任何内容,有没有人有任何想法?

供参考:这是我在版本1上使用的原始代码。我对其进行了格式化,因此它会显示更清晰的电子邮件,这可能会让它变得更加清晰。

<?php
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
       {
                //send email
            $name = $_REQUEST['name'] ;
            $email = $_REQUEST['email'] ;
            $subject = $_REQUEST["My Portfolio Website"] ;
            $message = $_REQUEST['message'].", Name: ".$name.", ".$phone.", Email: ".$email;
            mail("TylerJStelmach@gmail.com", $subject, $message, "From:" . $email);
            echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
        }else{
        //if "email" is not filled out, display the form
            echo "<form method='post' action='index.php'>

                <input type='text' input name='name' id='name' class='contacttext' placeholder=' Your Name' required>
                <input type='text' input name='email' id='email' class='contacttext' placeholder=' Your Email Address' required>
                <textarea input type='text' name='message' id='message' class='contacttext' placeholder=' Your Message' cols='55' rows='5' required></textarea>

                <input type='submit' id='submit' class='submitcontacttext' value='Send Message'>
                </form>";
        }
?>  

3 个答案:

答案 0 :(得分:0)

有一些语法错误。

此外,您的snipplet不包含发送邮件的代码。

更正后的代码如下:

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
     {
                        //send email
                $name = $_REQUEST['name'] ;
                $email = $_REQUEST['email'] ;
                $subject = $_REQUEST["My_Portfolio_Website"] ;
                $message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;

                echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";

        }else{
        //if "email" is not filled out, display the form
                echo '<form method="post" action="contact.php" class="connect">
            <div>
                <input id="name" name="name" type="text" required>
                <label for="name">Your Name</label>
            </div>
            <div>
                <input id="email" name="email" type="text" required>
                <label for="email">Your Email</label>
            </div>
            <div>
                <textarea id="message" name="message" required></textarea>
                <label for="message">Your Message</label>
            </div>
            <div class="metro">
                <div class="metro-button" type="submit">Click me</div> 
                </div>
        </form>';
        } 
?>

答案 1 :(得分:0)

因为所有人都说发送邮件的行在哪里但是因为我看到你的网站没有任何代码的迹象。

正如我猜测的那样,即使if为false,else也应该在我的localhost中运行时运行。

问题必须与您的服务器有关。 将error_reporting(E_ALL);置于顶部,如果存在错误,则会显示错误。

否则尝试写入新文件。

<?php
    error_reporting(E_ALL);
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
       {
                //send email
            $name = $_REQUEST['name'] ;
            $email = $_REQUEST['email'] ;
            $subject = $_REQUEST["My_Portfolio_Website"] ;
            $message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;

            echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";

        }else{
            ?>

            <form method="post" action="contact.php" class="connect">
          <div>
            <input id="name" name="name" type="text" required>
            <label for="name">Your Name</label>
          </div>
          <div>
            <input id="email" name="email" type="text" required>
            <label for="email">Your Email</label>
          </div>
          <div>
            <textarea id="message" name="message" required></textarea>
            <label for="message">Your Message</label>
          </div>
          <div class="metro">
            <div class="metro-button" type="submit">Click me</div> 
            </div>
        </form>
<?php
        } 
?>

答案 2 :(得分:0)

我能够通过重写基于我大约一年前使用的指南的原始代码来解决这个问题,我想出的答案确实有效。

对于所有处理信息的位置以及信息格式的处理方式,我在声明doctype之前都包含了php这个块。

<?php

if($_POST["submit"]) {
    $recipient="myemail@email.com";
    $subject="Client Mail";
    $name=$_POST["name"];
    $email=$_POST["email"];
    $email_from = $_POST['email'];
    $message=$_POST["message"];


    $mailBody="Name: $name\nEmail: $email\n\nMessage: $message";

    mail($recipient, $subject, $mailBody, "From: $sender <$email>");

    $thankYou="<p class='thank-you'>Thank you! Your message has been sent.</p>";
}

?>

从这里开始,我发现它没有提交给自己的页面是因为我的提交按钮既不是button也不是input。我把它设置为div所以在这里和那里做了一些调整我重写了下半部分以包含input作为提交。这让我想到了这个:

<?=$thankYou ?>

当您提交到页面并重新加载时,我会显示少量文本($thankYou)。

 <form method="post" action="contact.php" class="connect">
          <div>
            <input id="name" name="name" type="text" required>
            <label for="name">Your Name</label>
          </div>
          <div>
            <input id="email" name="email" type="text" required>
            <label for="email">Your Email</label>
          </div>
          <div>
            <textarea id="message" name="message" required></textarea>
            <label for="message">Your Message</label>
          </div>
          <div class="metro">
            <input type="submit" name="submit" class="metro-button submit-me">
            </div>
        </form>

所以现在提交时,电子邮件会附带: 'from'由$email设置 “主题”由$subject设置 并以这种方式格式化消息($message):

Name: John Doe
Email: JDoe@fake.com

Message: This is John Doe's message.

我为那可怕的问题表示道歉,那是在深夜,我很沮丧,失去了我的位置,晚上好好休息,并且能够解决它。

以下是 live version ,我鼓励您对其进行测试,它对我来说非常有用,希望此片段可以在将来帮助其他人!