如何创建单页PHP表单提交
我正在尝试创建一个联系人表单,以便人们可以在网站上留言。本质上,我希望它是一个单页进程,在同一页上显示成功或错误消息。然而,我是一名PHP初学者,所以我不确定从目前的困境中该往哪里走 到目前为止,我已经得到了要提交的表单,但是返回时总是会丢失字段,即使没有字段丢失。此外,页面将重新加载并返回到页面顶部。提交表单后,有没有办法让表单在提交表单后不返回页面顶部 这是HTML表单,本质上是一个分为不同幻灯片的单页网站。提交表单后,我希望页面停留在幻灯片4上,表单和成功/错误消息将显示在幻灯片4上如何创建单页PHP表单提交,php,html,forms,Php,Html,Forms,我正在尝试创建一个联系人表单,以便人们可以在网站上留言。本质上,我希望它是一个单页进程,在同一页上显示成功或错误消息。然而,我是一名PHP初学者,所以我不确定从目前的困境中该往哪里走 到目前为止,我已经得到了要提交的表单,但是返回时总是会丢失字段,即使没有字段丢失。此外,页面将重新加载并返回到页面顶部。提交表单后,有没有办法让表单在提交表单后不返回页面顶部 这是HTML表单,本质上是一个分为不同幻灯片的单页网站。提交表单后,我希望页面停留在幻灯片4上,表单和成功/错误消息将显示在幻灯片4上
<div id="slide4">
<div class="slidetitle">Contact Me</div>
<div class="content">
<form action="index.php" method="POST" id="contactform" target="_self">
<input type="hidden" name="postid" value="<?php $postid?>">
<table>
<tr><td><b>First Name:</b></td><td><input type="text" name="firstName" size="45" value="<?php $firstName?>"></td></tr>
<tr><td><b>Last Name:</b></td><td><input type="text" name="lastName" size="45" value="<?php $lastName?>"></td></tr>
<tr><td><b>Email:</b></td><td><input type="email" name="email" size="45" value="<?$email?>"></td></tr>
<tr><td><b>Phone:</b></td><td><input type="tel" name="phone" size="45" value="<?$phone?>"></td></tr>
<tr><td><b>Content:</b></td><td><textarea name="content" form="contactform" rows="10" cols="100"><?php echo $content; if (empty($content)){ echo "Enter text here...";} ?></textarea></td></tr>
<tr><td colspan=2 style="text-align: center"><input type=submit name="submit1" value="Leave me a message"></td></tr>
</table>
</form>
<div id="contactoutput">
<?php $output ?>
</div>
</div>
</div>
联系我
关闭表单标记怎么样:
联系我
关闭表单标记怎么样:
联系我
我只是想把你的代码做点什么。但有几件事你确实需要看一下:
您的html中缺少一些结束标记,例如,您缺少一个
&
标记李>
您可以将所有php代码放在页面顶部。把你的逻辑和你的观点分开是最好的做法
正确的php开始和结束标记如下所示:
不是
如果要连接ul以输出错误,则必须按照以下步骤进行:
你所得到的误差的基础是由上面提到的点引起的。祝你好运,如果你还有任何问题,请询问。只是想让你的代码有点与众不同。但有几件事你确实需要看一下:
您的html中缺少一些结束标记,例如,您缺少一个
&
标记李>
您可以将所有php代码放在页面顶部。把你的逻辑和你的观点分开是最好的做法
正确的php开始和结束标记如下所示:
不是
如果要连接ul以输出错误,则必须按照以下步骤进行:
你所得到的误差的基础是由上面提到的点引起的。祝您好运,如果您还有任何问题,请询问。谢谢您的回复Axel!我真不敢相信这只是一个简单的错误。我非常担心我的PHP,因为它只是一个缺少的结束标记!谢谢你抽出时间来帮助我!谢谢你的回复阿克塞尔!我真不敢相信这只是一个简单的错误。我非常担心我的PHP,因为它只是一个缺少的结束标记!谢谢你抽出时间来帮助我@佬司,非常感谢你的提示!我将实现您提供的指针。如果我有任何问题,我一定会问你。在实施了你建议的更改后,页面现在正在重新加载并返回到页面顶部,并且没有显示成功/错误消息。您知道如何将其保留在幻灯片4上,并在幻灯片4的表单下方显示成功/错误消息吗?表单现在正在提交到我的数据库!这是正确的一步:)。更新了代码。有一些问题被复制并粘贴到代码段中,所以希望没有出现任何问题。尽管出于某种原因,我已按要求设置了所有字段,但如果缺少一些字段,它仍会提交。@Lars非常感谢您提供的提示!我将实现您提供的指针。如果我有任何问题,我一定会问你。在实施了你建议的更改后,页面现在正在重新加载并返回到页面顶部,并且没有显示成功/错误消息。您知道如何将其保留在幻灯片4上,并在幻灯片4的表单下方显示成功/错误消息吗?表单现在正在提交到我的数据库!这是正确的一步:)。更新了代码。有一些问题被复制并粘贴到代码片段中,所以希望没有出现任何问题。此外,尽管出于某种原因,我已经按照要求设置了所有字段,但如果缺少一些字段,它仍然会提交。
<?php
$firstName = "";
$lastName= "";
$email = "";
$phone = "";
$content = "";
$errMsg = "";
?>
<?php $errMsg ?>
<?php
if (isset($_POST['submit1'])) {
// ==========================
//validate user input
// set up the required array
$required = array("firstName","lastName","email","phone","content"); // note that, in this array, the spelling of each item should match the form field names
// set up the expected array
$expected = array("firstName","lastName", "email","phone","content","postid"); // again, the spelling of each item should match the form field names
$missing = array();
foreach ($expected as $field){
// isset($_POST[$field]): Note that if there is no user selection for radio buttons, checkboxes, selection list, then $_POST[$field] will not be set
// Under what conditions the script needs to record a field as missing -- $field is required and one of the following two (1) $_POST[$field] is not set or (2) $_POST[$field] is empty
//echo "$field: in_array(): ".in_array($field, $required)." empty(_POST[$field]): ".empty($_POST[$field])."<br>";
if (in_array($field, $required) && (!isset($_POST[$field]) || empty($_POST[$field]))) {
array_push ($missing, $field);
} else {
// Passed the required field test, set up a variable to carry the user input.
// Note the variable set up here uses a variable name as the $field value. In this example, the first $field in the foreach loop is "title" and a $title variable will be set up with the value of "" or $_POST["title"]
if (!isset($_POST[$field])) {
//$_POST[$field] is not set, set the value as ""
${$field} = "";
} else {
${$field} = $_POST[$field];
}
}
}
//print_r ($missing); // for debugging purpose
/* add more data validation here */
// ex. $price should be a number
/* proceed only if there is no required fields missing and all other data validation rules are satisfied */
$stmt = $conn->stmt_init();
$sql = "Insert Into msg_table16 (firstName, lastName, email, content, phone, postid) values (?, ?, ?, ?, ?, ?)";
if($stmt->prepare($sql)){
// Note: user input could be an array, the code to deal with array values should be added before the bind_param statement.
$stmt->bind_param('ssssii',$firstName, $lastName,$email,$content,$phone,$postid);
$stmt_prepared = 1; // set up a variable to signal that the query statement is successfully prepared.
}
if ($stmt_prepared == 1){
if ($stmt->execute()){
$output = "<p>The following information has been saved in the database:<br><br>";
foreach($_POST as $key=>$value){
$output .= "<b>$key</b>: $value <br>"; //$key (form field names) may not be very indicative (ex. lname for the last name field), you can set up the form field names in a way that can be easily processed to produce more understandable message. (ex. use field names like "Last_Name", then before printing the field name, replace the underscore with a space. See php solution 4.4)
}
} else {
//$stmt->execute() failed.
$output = "<p>Database operation failed. Please contact the webmaster.";
}
} else {
// statement is not successfully prepared (issues with the query).
$output = "<p>Database query failed. Please contact the webmaster.";
}
} else {
// $missing is not empty
$output = "<p>The following required fields are missing in your form submission. Please check your form again and fill them out. <br>Thank you.<br>\n<ul>";
foreach($missing as $m){
$output .= "<li>$m";
}
$output .= "</ul>";
}
?>