[英]Form result in Same page
我已经访问了以下链接
现在我有2个示例文件test2.php的内容
<!DOCTYPE html>
<html>
<head>
<title> Form </title>
</head>
<body>
<?php include_once("test.php"); ?>
</body>
</html>
test.php的内容
<?php
if (isset($_POST['submit'])) {
$arr=array();
foreach ($_POST as $key => $value) {
$arr[$key]=$value;
}
print_r($arr);
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Form </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<link rel="stylesheet" href="css/main.css" />
</head>
<body>
<div id="content" id="login_form">
<form method="post" id="f1" name="f1" action="test.php">
<table>
<tr>
<td>Name :</td><td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Register Number :</td><td><input type="text" name="regno"/></td>
</tr>
<tr>
<td>Date of Birth :</td><td><input type="text" name="dob"/></td>
</tr>
<tr>
<td><input type="submit" id="b1" name="submit" value="Submit" style="font-size:16px;"/></td>
</tr>
</table>
</form>
</div>
<script>
$('#f1').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('post'),
url: $(this).attr('test.php'),
success: function(response) {
$('#content').html(response);
}
});
return false;
});
</script>
</body>
</html>
test2.php显示表格。 I want to display the values entered in the form and the form
单击提交后I want to display the values entered in the form and the form
,但我只会看到表单。 是因为$ _POST为空,还是因为我在页面本身的结构中犯了错误?
编辑:我希望将表单提交的结果加载到test2.php页面中。
进行两项更改
action="test.php"
更改为action=""
,也可以删除它,不需要它。 return false;
return true;
在js中, $('#f1').submit(function()
(何时使用return true / false的解释,当前您请求的是一个jquery函数调用的submit操作,如果从javascript / jquery中返回false,则意味着当前请求即commit不会发生。的确,将发生提交。如果在验证某些内容且用户未添加正确的输出的情况下使用false,则在用户更正数据之前我们不希望提交该页面,在这种情况下也无需执行操作当您通过jquery提交时,可以从表单中删除action =“”属性)
此外,您已经在test.php中包含html
标记,包括在test2.php中的html
标记将产生问题。
建议直接致电test.php,您不需要test2.php。
这是test.php的完整代码,
<?php
if (isset($_POST['submit'])) {
$arr=array();
foreach ($_POST as $key => $value) {
$arr[$key]=$value;
}
print_r($arr);
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Form </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<link rel="stylesheet" href="css/main.css" />
</head>
<body>
<div id="content" id="login_form">
<form method="post" id="f1" name="f1" action="">
<table>
<tr>
<td>Name :</td><td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Register Number :</td><td><input type="text" name="regno"/></td>
</tr>
<tr>
<td>Date of Birth :</td><td><input type="text" name="dob"/></td>
</tr>
<tr>
<td><input type="submit" id="b1" name="submit" value="Submit" style="font-size:16px;"/></td>
</tr>
</table>
</form>
</div>
<script>
$('#f1').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('post'),
url: $(this).attr('test.php'),
success: function(response) {
$('#content').html(response);
}
});
return true;
});
</script>
</body>
</html>
可能是您找到了解决方案...........
<?php
if(isset($_POST['submit'])){
$arr=array();
foreach( $_POST as $key => $value ){
$arr[$key]=$value;
}
print_r($arr);
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Form </title>
<link rel="stylesheet" href="css/main.css" />
</head>
<body>
<div id="content" id="login_form">
<form method="post" id="f1" name="f1" action="test.php">
<table>
<tr>
<td>Name :</td><td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Register Number :</td><td><input type="text" name="regno"/></td>
</tr>
<tr>
<td>Date of Birth :</td><td><input type="text" name="dob"/></td>
</tr>
<tr>
<td><input type="submit" id="b1" name="submit" value="Submit" style="font-size:16px;"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
这必须解决您的问题。 更换线
<form method="post" id="f1" name="f1" action="test.php">
与
<form method="post" name="f1" action="">
您不能在“表单”中使用“ id”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.