【发布时间】:2013-12-24 09:12:52
【问题描述】:
(我找到了但还是不明白){HTML form PHP post to self to validate or submit to new page}
如果这个问题在另一个地方得到更好的解释,我很抱歉,但我已经被困了几个小时,已经搜索过,然后就放弃了。我正在阅读有关如何使用 PHP 验证、清理和处理表单的 W3c 网站教程。一切都很顺利(至少我认为确实如此),直到是时候对这些数据做点什么了。我现在将代码展示给您,并在代码之后进一步说明我的立场和问题:
<form method="POST" name="signup" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label for="first name"></label><input id="first name" name="first_name" placeholder="First Name" type="text" value="<?php echo $firstname;?>" /> <span class="error">* <?php echo $firstnameErr;?></span>
<label for="last_name"></label><input id="last name" name="last_name" placeholder="Last Name" type="text" value="<?php echo $lastname;?>" />
<span class="error">* <?php echo $lastnameErr;?></span>
<br><br>
<label for="email"></label><input id="email" name="email" placeholder="Email" type="text" value="<?php echo $email;?>" />
<span class="error">* <?php echo $emailErr;?></span>
<br /><br />
<label for="password"></label><input id="password" name="password" placeholder="Create Password" type="password" />
<span class="error">* <?php echo $passwordErr;?></span>
<br /><br />
<label for="male"><strong>Male</strong></label>
<input id="male" value="male" <?php if (isset($gender) && $gender=="male") echo "checked";?> name="gender" type="radio" />
<label for="female"><strong>Female</strong></label> <input id="female" value="female"
<?php if (isset($gender) && $gender=="female") echo "checked";?> name="gender" type="radio" />
<span class="error">* <?php echo $genderErr;?></span>
<br /><br />
<label for="submit">"I Agree To <a href="#">Terms And Conditions"</a></label> <input id="submit" value="Submit" type="submit" name="submit"/><br /><br />
<p><span class="error">* required field.</span></p>
<hr>
我对很多事情感到困惑。我应该保持“表单操作”原样,还是应该将其更改为“welcome.php”。如果我将其更改为“welcome.php”,我是否仍然包含“htmlspecialchars”?我要正在使用 MSQLI。我已经能够连接到我的数据库,但是如何将用户数据转换为服务器的可行信息?我是否继续使用我在这个 HTML 表单中创建的变量?我知道我需要将某种变量放入查询字符串中然后确保我也退出它比我能处理一些不好的点。感谢您的帮助,祝您节日快乐。
下面是我的“welcome.php”。它实际上被称为不同的东西,但目前它是“welcome.php”。再次感谢。
<?php
$hostname="social89.db";
$username="social89";
$password="P!!";
$dbname="social89";
$db_conx = mysqli_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
if(mysqli_connect_errno()){
echo mysqli_connect_error();
exit();
}
$select = mysqli_select_db($db_conx,$dbname);
$firstname= $_POST["first_name"];
$lastname= $_POST["last_name"];
$email= $_POST["email"];
$password= $_POST["password"];
$gender= $_POST["gender"];
mysqli_query($db_conx,"INSERT INTO users (firstname, lastname, email, password, gender)
VALUES ('$firstname', '$lastname', '$email', '$password', '$gender')");
mysqli_close($db_conx);
header("Location: ERASETHISprofile.php")
?>
【问题讨论】:
标签: php html forms validation