1

因此,对于一项作业,我们需要一个联系表格,基本表格是由我的老师提供的,但我的总是返回“请更正以下错误:输入你的名字,你的名字!” 当我点击提交时。我网站上的其他所有内容都运行良好,但这种形式让我把头发拉出来!任何帮助是极大的赞赏。(表格上的fake@fake.com已更改为我的真实地址)

<?php
/* Set e-mail recipient */
$myemail  = "Fake@fake.com";
$subject = "Order Form - B Bakery";
$name = check_input($_POST['name'],"Enter your name you tit!");
$Email    = check_input($_POST['Email'],"Enter your email you tit!");
$Phone   = check_input($_POST['Phone']);
$Message = check_input($_POST['Message'],"Enter your message you tit!");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $name
E-mail: $Email
Phone: $Phone

Message:
$Message

End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thankyou.html');
exit();
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
<html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Order Form</title>

<style type="text/css">
#wrapper #ddffdd {
    position: absolute;
    height: 500px;
    width: 929px;
    left: 385px;
    top: 230px;
    background: #FFF;
}

body {
    background-image: url(Pics/pink-floral-background.jpg);
    background-repeat;
    }

#wrapper {
    width: 1000px;
    margin: 0 auto;
    height: 500px;
}

#menu ul li {
    list-style-type: none;
    display: inline;
    height: auto;
    padding-left: 17px;
    border-right-width: thin;
    border-left-width: thin;
    border-top-style: none;
    border-right-style: solid;
    border-left-style: none;
    border-right-color: #000;
    border-left-color: #000;
    border-top-width: thin;
    border-bottom-width: thin;
    border-bottom-style: none;
    border-top-color: #000;
    border-bottom-color: #000;
    padding-right: 17px;
}

#menu ul {
    position: absolute;
    left: 399px;
    top: 1px;
    width: 737px;
}
#menu {
    height: 27px;
    width: 764px;
    position: absolute;
    left: 29px;
    top: 170px;
}

#HomeText {
    position: absolute;
    left: 305px;
    top: 9px;
    width: 520px;
    alignment: absolute;
    height: 53px;
    font-size: 36px;
    font-family: "Comic Sans MS", cursive;
}
#cakepic {
    position: absolute;
    left: 3px;
    top: 24px;
    width: 200px;
    height: 200px;
}
#HomeText2 {
    position: absolute;
    left: 47px;
    top: 91px;
    width: 192px;
    alignment: absolute;
    font-family: "Comic Sans MS", cursive;
    height: 385px;
}
#HomeText3 {
    position: absolute;
    left: 478px;
    top: 91px;
    width: 192px;
    alignment: absolute;
    font-family: "Comic Sans MS", cursive;
    height: 385px;
}
#Banner{
    height: 90px;
    width: 800px;
    position: absolute;
    top: 75px;
    left: 325px;
}
#Banner #Bannertop {
    position: absolute;
    left: 237px;
    top: -34px;
}
#Banner #Bannerbottom img {
    position: absolute;
    left: 381px;
    top: 57px;
}
#wrapper #Banner div form table tr td {
    text-align: right;
}
#wrapper #Banner div form table {
    text-align: left;
}
#wrapper #ddffdd form table {
    position: absolute;
    left: 11px;
    top: 124px;
    width: 901px;
}
</style>
<script type="text/javascript">
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
</script>
</head>

<body>
<div id="wrapper">
<div class=header></div>
        <div id="menu">
        <ul>
        <li><a href="index.html">Home</a></li>
        <li><a href="FAQ.html">FAQ</a></li>
        <li><a href="Order Form.html">Contact</a></li>
        <li><a href="Latest News.html">Latest News</li>
        <li><a href="Product List.html">Product List</a></li>
        </ul>
        </div>
<p></p>
<div id="Banner">
  <p>

  <div id="Bannertop"> <img src="coollogo_com-201093071.png" width="409" height="83" /></div></p>
<p>
<div id="Bannerbottom"><img src="coollogo_com-200791182.png" width="406" height="34" />
</div>
<div>

</div>
</div>
<div id="ddffdd">
  <div id=HomeText>Contact</div>

  <form action="myform.php" method="get">
    <table width="99%" border="1">
      <tr>
        <td width="33%" onfocus="MM_validateForm('name','','R','Phone','','RisNum','Email','','RisEmail');return document.MM_returnValue"><label for="name">
          <div align="right" onfocus="MM_validateForm('name','','R','Phone','','RisNum','Email','','RisEmail','Message','','R');return document.MM_returnValue">Name</div>
        </label>        </td>
        <td width="67%"><div align="left">
          <input name="name" type="text" id="name" size="36" />
        </div></td>
      </tr>
      <tr>
        <td><label for="Phone">
          <div align="right">Phone Number</div>
        </label>
          <label for="Email">
            <div align="right">Email</div>
          </label></td>
        <td><div align="left">
          <input type="text" name="Phone" id="Phone" />
        </div>
<input type="text" name="Email" id="Email" /></td>
      </tr>
      <tr>
        <td><div align="right">Message</div></td>
        <td><div align="left">
          <textarea name="Message" id="Message" cols="60" rows="5"></textarea>
        </div></td>
      </tr>
      <tr>
        <td><div align="right">
          <input type="reset" name="Reset2" id="Reset2" value="Reset" />
        </div></td>
        <td><div align="left">
          <input name="Reset" type="submit" id="Reset" onclick="MM_validateForm('name','','R','Phone','','RisNum','Email','','RisEmail');return document.MM_returnValue" value="Submit" />
        </div></td>
      </tr>
      <tr>
        <td><div align="right"></div></td>
        <td><div align="right"></div></td>
      </tr>
    </table>
  </form>
</div>

</div>
</body>
</html>

我收到了一封电子邮件:D 但之后它转到了我的虚拟主机 404 而不是我的感谢页面,非常感谢!

它正在工作:DDDDDDDDDD 非常感谢大家,我欠你们所有人一杯啤酒!

4

3 回答 3

1

如果您使用...PHP $_POST,您<form method必须是...

<form method='POST'

如果你使用 PHP 的$_GET,你<form method必须像..

<form method='GET'

所以,在你的代码中:

$name = check_input($_POST['name'],"Enter your name you tit!");
$Email    = check_input($_POST['Email'],"Enter your email you tit!");
$Phone   = check_input($_POST['Phone']);
$Message = check_input($_POST['Message'],"Enter your message you tit!");

相应的 HTML 应该是,

<form action="myform.php" method="POST"

让它工作。

于 2013-06-15T19:20:30.207 回答
0

你有一个check_input

$name = check_input($_POST['name'],"Enter your name you tit!");

但是当仔细观察我看到的功能时

if ($problem && strlen($data) == 0)
{
    show_error($problem);
}

然而,作为$data=$_POST['name'],长度和因此strlen($data) != 0

为什么不直接忽略整个 if 语句呢?

哦:也改成method="post"

于 2013-06-15T19:16:47.607 回答
0

将表单方法更改为 method="POST" 而不是 GET:

<form action="myform.php" method="POST">

现在在处理表单输入的 PHP 代码中,您指的是 POST 数据。现在您使用的方法是 GET,这就是为什么不会在 $_POST 数组中设置值的原因。或者,在您的处理代码中,您可以使用 $_GET 而不是 $_POST。

于 2013-06-15T19:13:57.733 回答