使用HTML& amp;创建表单PHP

时间:2013-05-08 19:36:07

标签: php html forms twitter-bootstrap

我正在尝试使用HTML,PHP和bootstrap twitter在网站(http://youngliferaffle.com/)上创建表单。我一直在浏览有关如何创建它的几个教程,但我认为我在使用Locations时遇到了麻烦 - 例如,如果用户出错或者在提交答案后重新定位用户。我也是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;

3 个答案:

答案 0 :(得分:1)

最后一行

header('Location: contact-form-submission.phps='.urlencode('Thank you for your message.')); exit; 
好像你有正确的名字。为什么要将php文件重定向到自己。您应该使用初始表单的URL,无论名称是什么。可能这行会产生你得到的重定向循环错误。

答案 1 :(得分:1)

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

我通常将处理逻辑和表单保存在同一个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>

这样,如果有错误,用户将在同一页面中看到错误消息,这些字段也将保留其原始值。只有在没有错误的有效提交时才会重定向。否则它将保持在同一页面,显示错误消息。

答案 2 :(得分:0)

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

此外,最后一行,因为此页面仅在发布数据时有效,您需要将用户重定向到此页面。也许做一个新的谢谢你的页面。

还记得吗?之后.php为?告诉您的Web服务器它不再是文件名。否则,它将查找名为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;