【问题标题】:Creating a Form Using HTML & PHP [closed]使用 HTML 和 PHP 创建表单 [关闭]
【发布时间】:2013-05-08 19:36:07
【问题描述】:

我正在尝试使用 HTML、PHP 和引导 twitter 在网站 (http://youngliferaffle.com/) 上创建一个表单。我一直在查看有关如何创建它的几个教程,但我认为我在使用位置时遇到了问题——例如在用户出错或提交答案后重新定位用户。我对 PHP 也很陌生。我非常感谢您的帮助!谢谢!

主要问题:

  • “重定向到网页的次数过多”/可能是由于 cookie 造成的
  • 未收到来自提交的电子邮件
  • 提交后或由于提交错误,用户没有被重定向到正确的页面

这是我的 HTML 表单的一部分:

<form method="POST" action="contact-form-submission.php">
  <fieldset>
    <label><strong>Sign-Up</strong></label>

    <input type="text" class="name" name="cname" id="name" placeholder="Full Name"></input>

    <input type="text" class="phone" name="phone" id="phone" placeholder="Phone Number"></input>

    <input type="text" class="email" name="email" id="email" placeholder="Email Address"></input>
    <div class="form-actions">  
      <input type="submit" name="save" value="Send"> 

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

这是我的 PHP 表单

// check for form submission - if it doesn't exist then send back to contact form  
if (!isset($_POST['save']) || $_POST['save'] != 'contact') { 
  header('Location: contact-form-submission.php'); 
  exit; 
} 

// get the posted data 
$name = $_POST['contact_name']; 
$email_address = $_POST['contact_email']; 
$phone = $_POST['contact_phone']; 

// check that a name was entered 
if (empty($name)) 
  $error = 'You must enter your name.'; 
// check that an email address was entered 
elseif (empty($email_address))  
  $error = 'You must enter your email address.'; 
// check for a valid email address 
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address)) 
  $error = 'You must enter a valid email address.'; 
// check that a phone number was entered 
elseif (empty($phone)) 
  $error = 'You must enter a phone number.'; 

// check if an error was found - if there was, send the user back to the form 
if (isset($error)) { 
  //Am I putting the wrong location?
  header('Location: contact-form-submission.php?e='.urlencode($error)); exit; 
} 

// write the email content 
$email_content = "Name: $name\n"; 
$email_content .= "Email Address: $email_address\n"; 
$email_content .= "Phone:\n\n$phone"; 

// send the email 
mail ("myemail.com", "New Contact Message", $email_content); 

// send the user back to the form
//And This is where I'm having trouble with! the Location part
header('Location: contact-form-submission.phps='.urlencode('Thank you for your message.')); 
exit;

【问题讨论】:

  • 那么你有什么具体问题?
  • 这个脚本什么时候执行?是在 GET contact-form-submission.php、POST contact-form-submission.php 还是其他 URL 之后?
  • 如果您尝试提交表单,即使没有正确输入,它也会将您重定向并说有太多重定向到网页 (youngliferaffle.com/contact-form-submission.php)。此外,即使我填写正确,它也一样,我没有收到电子邮件。
  • 这一行 header('Location: contact-form-submission.php?e='.urlencode($error));出口;正在将页面重定向到同一页面。然后它重定向到自己等等等等......
  • @maeusz 我不知道如何或把它放在哪里 ".$_GET['s']."
    "; // 检查表单错误 elseif (isset($_GET['e'])) echo "
    ".$_GET['e']."
    ";我假设您指的是要放入 html 中?我已经放置了 php 标签,但它仍然会出现在我的 html 页面上

标签: php html forms twitter-bootstrap


【解决方案1】:

最后一行

header('Location: contact-form-submission.phps='.urlencode('Thank you for your message.')); exit; 

看来你的名字是对的。为什么将 php 文件重定向到自身。您应该使用初始表单的 URL,无论名称是什么。这行可能会创建您得到的重定向循环错误。

【讨论】:

    【解决方案2】:

    在 php 代码中的第一件事是它重定向到自身。无论如何都不会设置“保存”变量,因此它会一次又一次地无休止地重定向。它检查是否设置了“保存”,但第一次没有设置,所以它再次重定向到同一页面。但是“保存”变量不会再次设置,因为它只是一个重定向而不是表单提交。所以它一次又一次地发生,所以你得到了太多的重定向错误。

    我通常将处理逻辑和表单保存在同一个 PHP 文件中。这意味着,表单的 action 属性将具有与 value 相同的页面 URL。比如这样。

    simple_form.php

    <?php
        //Assume the form has one field called field1 and a 'save' variable to indicate form submission
        $field1 = "";
    
        //Declare an array to store errors
        $errors = array();
    
        if(isset($_POST['save'])) {
            //Form has been submitted.. do validations etc.
            $field1 = $_POST['field1'];
            if(someValidationCheck($field1) == false) {
                $errors[] = "Field1 is not valid";
            }
    
            //After all field validations.. adding errors to $errors array..
            if(count($errors) == 0) {
                //No errors so write database insert statments etc. here
                //Also put a header("Location:...") redirect here if you want to redirect to a thank you page etc.
            }
        }
    ?>
    <html>
    <body>
    <?php
        //If there were errors, show them here
        if(count($errors) > 0) {
            //loop through $errors array .. print one by one.
        }
    ?>
    <form method="post" action="simple_form.php">
       <input type="text" name="field1" value="<?php echo($field1); ?>" />
       <input type="hidden" name="save" value="save" />
       <input type="submit" />
    </form>
    </body>
    </html>
    

    这样,如果出现错误,用户将在同一页面中看到错误消息,字段也将保留其原始值。它只会在有效提交且没有错误的情况下被重定向。否则它将停留在同一页面中,并显示错误消息。

    【讨论】:

      【解决方案3】:

      在第一行中,您一直返回相同的位置。这会创建一个循环。您需要将它们发送回表单。可能是index.php?

      此外,最后一行,由于此页面仅在发布数据时有效,您需要将用户重定向到此页面之外。也许制作一个新的感谢页面。

      还记得你的 ?在 .php 作为 ?告诉你的网络服务器它不再是一个文件名。否则它将查找名为thankyou.phps 的文件。

      // check for form submission - if it doesn't exist then send back to contact form  
      if (!isset($_POST['save']) || $_POST['save'] != 'contact') { 
      header('Location: index.php'); exit; 
      } 
      
      // get the posted data 
      $name = $_POST['contact_name']; 
      $email_address = $_POST['contact_email']; 
      $phone = $_POST['contact_phone']; 
      
      // check that a name was entered 
      if (empty($name)) 
      $error = 'You must enter your name.'; 
      // check that an email address was entered 
       elseif (empty($email_address))  
      $error = 'You must enter your email address.'; 
      // check for a valid email address 
      elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address)) 
          $error = 'You must enter a valid email address.'; 
      // check that a phone number was entered 
      elseif (empty($phone)) 
      $error = 'You must enter a phone number.'; 
      
      // check if an error was found - if there was, send the user back to the form 
      if (isset($error)) { 
      //Am I putting the wrong location?
      header('Location: contact-form-submission.php?e='.urlencode($error)); exit; 
      } 
      
      // write the email content 
      $email_content = "Name: $name\n"; 
      $email_content .= "Email Address: $email_address\n"; 
      $email_content .= "Phone:\n\n$phone"; 
      
      // send the email 
      mail ("myemail.com", "New Contact Message", $email_content); 
      
      // send the user back to the form
      // remember the ? after your file name!
      header('Location: thankyou.php?s='.urlencode('Thank you for your message.')); exit;  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-11
        • 2022-12-17
        • 1970-01-01
        相关资源
        最近更新 更多