Dreamweaver PHP联系表单返回名称错误

时间:2013-06-15 18:58:32

标签: php

因此,对于作业,我们需要一个联系表格,基本表格由我的老师提供,但我的回复表格“请更正以下错误: 输入你的名字你的名字!“当我点击提交。我网站上的其他所有内容都工作正常,但这个表格让我脱掉了头发!非常感谢任何帮助。(表格上的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非常感谢大家,我欠你们所有啤酒!

3 个答案:

答案 0 :(得分: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"

让它发挥作用。

答案 1 :(得分:0)

将form方法更改为method =“POST”而不是GET:

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

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

答案 2 :(得分: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,因此method="post"

为什么不把整个if语句留下来?

哦:还要把它改成{{1}}