我刚刚开始熟悉PHP,而我现在要做的是提交表单时,您会被重定向到确认页面,在那里您将看到如下消息:
Thank you John Smith for submiting folowing info:
Address: blah blah blah
Mobile phone number: blah blah blah
Landline phone number: blah blah blah
Email: blah blah blah
(where blah blah blah is data that user entered in form)
所以这是我的主要代码:
<?php
if (isset($_POST['submit'])){
header("Location: form.php");
}
?>
<html>
<head>
******
</head>
<body>
<header class="main-header">
***
</header>
<main id="main">
<?php
define('DB_NAME', '***');
define('DB_USER', '***');
define('DB_PASSWORD', '***');
define('DB_HOST', '***');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$forenameErr = $surnameErr = $emailErr = $sendMethodErr = "";
$forename = $surname = $address = $mobile = $landline = $email = $sendMethod = "";
if (isset($_POST['submit'])) {
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$address = $_POST['postalAddress'];
$mobile = $_POST['mobileTelNo'];
$landline = $_POST['landLineTelNo'];
$email = $_POST['email'];
$sendMethod = $_POST['sendMethod'];
}
$sql = "INSERT INTO CT_expressedInterest (forename, surname, postalAddress, mobileTelNo, landLineTelNo, email, sendMethod)
VALUES('$forename', '$surname', '$address', '$mobile', '$landline', '$email', '$sendMethod')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> ">
<fieldset class="formfield"> <!--adds box around content of this element-->
<legend>Your Details</legend> <!--Gives title to this fieldset-->
<p>Fields marked with * are required for form submition.</p>
<label for="forename">First Name:</label>
<input type="text" name="forename" id="forename" placeholder = "Enter your First Name" tabindex="12" required />
<label for="surname">Last Name(s):</label>
<input type="text" name="surname" id="surname" placeholder = "Enter your Last Name" tabindex="13" required />
<label for="postalAddress">Postal Address:</label>
<input type="text" name="postalAddress" id="postalAddress" placeholder = "Your house number, street name and postcode"tabindex="14"/>
<label for="mobileTelNo">Mobile Telephone Number:</label>
<input type="text" name="mobileTelNo" id="mobileTelNo" placeholder = "Enter your mobile number" tabindex="19"/>
<label for="landLineTelNo">Landline Telephone Number:</label>
<input type="text" name="landLineTelNo" id="landLineTelNo" placeholder = "Enter your landline number" tabindex="19"/>
<label for="email">Email:</label>
<input type="text" name="email" id="email" placeholder = "Enter your email address" tabindex="20"/> <br/>
<p> How Would You Like To Recieve Further Information?</p>
<label id="post">Post</label>
<input type="radio" name="sendMethod" value="Post" tabindex="34" placeholder = "please choose one" required >
<label id="eemail">Email</label>
<input type="radio" name="sendMethod" value="Email" tabindex="35">
<label id="sms">SMS Text</label>
<input type="radio" name="sendMethod" value="SMS" tabindex="36">
<p><input type="checkbox" name="checkbox" value="check" id="agree" required /> I have read and agree to the Terms and Conditions and Privacy Policy </p>
<p>
<input type="submit" name="submit" value="Submit!" />
</p>
</fieldset>
</form>
</main>
<footer class="main-footer">
***
</footer>
</body>
</html>
这就是我目前为form.php所做的,但显然它不起作用。 (我不确定我是否还需要连接到此文件中的数据库)
echo "<p>Thank you $forename $surname</p>";
echo "<p>We have recieved following information:</p>";
echo "<p>Address: $address</p>";
echo "<p>Mobile phone number: $mobile</p>";
echo "<p>Landline phone number: $landline</p>";
echo "<p>Email: $email </p>";
任何人都可以帮我完成form.php代码吗? 还有一件事。按下提交按钮后,我的数据库中会添加两条新记录。一个信息已输入表单,一个完全为空。 我将不胜感激任何帮助。谢谢!
P.S。我不允许使用任何jquery或ajax的东西。
答案 0 :(得分:0)
此代码的评论很少...... 首先,如果您使用:
header("Location:form.php");
您实际上将导航重定向到与主代码相同的路径上的“form.php”文件。两种不同的方式:
1 - 不要通过删除:
重定向到“form.php” <?php
if (isset($_POST['submit'])){
header("Location: form.php");
}
?>
只测试您的数据是否已设置并完成工作,不要更改位置。
if ( !empty($_POST['submit'])
&& !empty($_POST['postalAddress'])
&& ...
&& !empty($_POST['sendMethod']) ) {
// Use data
else {
// Show form
}
2 - 您实际上可以使用名为form.php的文件,但直接使用您的html格式:
<form method="post" action="form.php">
...
</form>
所以,只需获取“form.php”的代码,并进行一些更改:
<?php
/* You have to valide all your inputs
* And use empty instead of isset
* it tests for undifined (like isset), null value
* and if it's empty
*/
if ( empty($_POST['submit'])
|| empty($_POST['postalAddress'])
|| ...
|| empty($_POST['sendMethod']) ) {
// There is no (valid) form
header("Location: yourMainCode.php");
else {
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$address = $_POST['postalAddress'];
$mobile = $_POST['mobileTelNo'];
$landline = $_POST['landLineTelNo'];
$email = $_POST['email'];
$sendMethod = $_POST['sendMethod'];
// Database stuff...
// Redirect to a result page or something
header("Location:result.php");
}
?>
希望它有所帮助!