0

我正在尝试使用 HTML、PHP 和 bootstrap 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;
4

3 回答 3

1

最后一行

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

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

于 2013-05-08T19:45:44.103 回答
1

那么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>

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

于 2013-05-08T20:00:13.343 回答
0

在第一行中,您一直返回相同的位置。这会创建一个循环。您需要将它们发送回表单。可能是 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;  
于 2013-05-08T19:42:26.570 回答