从表单提交上的PHP页面获取数据
我有一个带有表单的index.php。提交时,我希望process.php的结果显示在index.php的result div中。非常确定我需要某种AJAX,但我不确定 index.php从表单提交上的PHP页面获取数据,php,jquery,ajax,forms,get,Php,Jquery,Ajax,Forms,Get,我有一个带有表单的index.php。提交时,我希望process.php的结果显示在index.php的result div中。非常确定我需要某种AJAX,但我不确定 index.php <div id="result"></div> <form action="" id="form" method="get"> <input type="text" id="q" name="q" maxlength="16"> </form&g
<div id="result"></div>
<form action="" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
</form>
<?php
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
?>
<div id="result">
<?php include('process.php'); ?>
</div>
<form action="index.php" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Check if form was submitted
if(isset($_GET['submit'])){
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
}
?>
process.php
<div id="result"></div>
<form action="" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
</form>
<?php
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
?>
<div id="result">
<?php include('process.php'); ?>
</div>
<form action="index.php" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Check if form was submitted
if(isset($_GET['submit'])){
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
}
?>
在index.php中执行此操作如何:
在index.php中执行此操作如何:
这是jquery Ajax示例
$.ajax({
type: "POST",
url: "somescript.php",
datatype: "html",
data: dataString,
success: function(data) {
doSomething(data);
}
});
<script>
//wait for page load to initialize script
$(document).ready(function(){
//listen for form submission
$('form').on('submit', function(e){
//prevent form from submitting and leaving page
e.preventDefault();
// AJAX goodness!
$.ajax({
type: "GET", //type of submit
cache: false, //important or else you might get wrong data returned to you
url: "process.php", //destination
datatype: "html", //expected data format from process.php
data: $('form').serialize(), //target your form's data and serialize for a POST
success: function(data) { // data is the var which holds the output of your process.php
// locate the div with #result and fill it with returned data from process.php
$('#result').html(data);
}
});
});
});
</script>
这是jquery Ajax示例
$.ajax({
type: "POST",
url: "somescript.php",
datatype: "html",
data: dataString,
success: function(data) {
doSomething(data);
}
});
<script>
//wait for page load to initialize script
$(document).ready(function(){
//listen for form submission
$('form').on('submit', function(e){
//prevent form from submitting and leaving page
e.preventDefault();
// AJAX goodness!
$.ajax({
type: "GET", //type of submit
cache: false, //important or else you might get wrong data returned to you
url: "process.php", //destination
datatype: "html", //expected data format from process.php
data: $('form').serialize(), //target your form's data and serialize for a POST
success: function(data) { // data is the var which holds the output of your process.php
// locate the div with #result and fill it with returned data from process.php
$('#result').html(data);
}
});
});
});
</script>
您确实不需要AJAX,因为您可以将其提交给自己并包含流程文件:
index.php
<div id="result"></div>
<form action="" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
</form>
<?php
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
?>
<div id="result">
<?php include('process.php'); ?>
</div>
<form action="index.php" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Check if form was submitted
if(isset($_GET['submit'])){
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
}
?>
process.php
<div id="result"></div>
<form action="" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
</form>
<?php
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
?>
<div id="result">
<?php include('process.php'); ?>
</div>
<form action="index.php" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Check if form was submitted
if(isset($_GET['submit'])){
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
}
?>
实现AJAX将使事情更加用户友好,但它肯定会使代码复杂化。无论你走哪条路,祝你好运
这是一个jquery Ajax示例
$.ajax({
type: "POST",
url: "somescript.php",
datatype: "html",
data: dataString,
success: function(data) {
doSomething(data);
}
});
<script>
//wait for page load to initialize script
$(document).ready(function(){
//listen for form submission
$('form').on('submit', function(e){
//prevent form from submitting and leaving page
e.preventDefault();
// AJAX goodness!
$.ajax({
type: "GET", //type of submit
cache: false, //important or else you might get wrong data returned to you
url: "process.php", //destination
datatype: "html", //expected data format from process.php
data: $('form').serialize(), //target your form's data and serialize for a POST
success: function(data) { // data is the var which holds the output of your process.php
// locate the div with #result and fill it with returned data from process.php
$('#result').html(data);
}
});
});
});
</script>
//等待页面加载以初始化脚本
$(文档).ready(函数(){
//收听表格提交
$('form')。关于('submit',函数(e){
//阻止表单提交和离开页面
e、 预防默认值();
//阿贾克斯天哪!
$.ajax({
类型:“获取”//提交的类型
cache:false,//重要,否则您可能会得到返回给您的错误数据
url:“process.php”,//目的地
数据类型:“html”,//process.php中的预期数据格式
数据:$('form').serialize(),//以表单的数据为目标,并为POST序列化
success:function(data){//data是保存process.php输出的变量
//找到带有#result的div,并用process.php返回的数据填充它
$('#result').html(数据);
}
});
});
});
您确实不需要AJAX,因为您可以将其提交给自己并包含流程文件:
index.php
<div id="result"></div>
<form action="" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
</form>
<?php
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
?>
<div id="result">
<?php include('process.php'); ?>
</div>
<form action="index.php" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Check if form was submitted
if(isset($_GET['submit'])){
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
}
?>
process.php
<div id="result"></div>
<form action="" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
</form>
<?php
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
?>
<div id="result">
<?php include('process.php'); ?>
</div>
<form action="index.php" id="form" method="get">
<input type="text" id="q" name="q" maxlength="16">
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Check if form was submitted
if(isset($_GET['submit'])){
$result = $_GET['q'];
if($result == "Pancakes") {
echo 'Result is Pancakes';
}
else {
echo 'Result is something else';
}
}
?>
实现AJAX将使事情更加用户友好,但它肯定会使代码复杂化。无论你走哪条路,祝你好运
这是一个jquery Ajax示例
$.ajax({
type: "POST",
url: "somescript.php",
datatype: "html",
data: dataString,
success: function(data) {
doSomething(data);
}
});
<script>
//wait for page load to initialize script
$(document).ready(function(){
//listen for form submission
$('form').on('submit', function(e){
//prevent form from submitting and leaving page
e.preventDefault();
// AJAX goodness!
$.ajax({
type: "GET", //type of submit
cache: false, //important or else you might get wrong data returned to you
url: "process.php", //destination
datatype: "html", //expected data format from process.php
data: $('form').serialize(), //target your form's data and serialize for a POST
success: function(data) { // data is the var which holds the output of your process.php
// locate the div with #result and fill it with returned data from process.php
$('#result').html(data);
}
});
});
});
</script>
//等待页面加载以初始化脚本
$(文档).ready(函数(){
//收听表格提交
$('form')。关于('submit',函数(e){
//阻止表单提交和离开页面
e、 预防默认值();
//阿贾克斯天哪!
$.ajax({
类型:“获取”//提交的类型
cache:false,//重要,否则您可能会得到返回给您的错误数据
url:“process.php”,//目的地
数据类型:“html”,//process.php中的预期数据格式
数据:$('form').serialize(),//以表单的数据为目标,并为POST序列化
success:function(data){//data是保存process.php输出的变量
//找到带有#result的div,并用process.php返回的数据填充它
$('#result').html(数据);
}
});
});
});
有两种方法
或者使用ajax调用process.php(我建议——发送ajax调用并根据结果执行操作非常容易),然后使用javascript更改表单
或者让创建表单的php代码与表单提交的php代码相同,然后根据是否有get参数输出不同的内容。(编辑:蒙基宙斯向您详细介绍了如何做到这一点。)有两种方法
或者使用ajax调用process.php(我建议——发送ajax调用并根据结果执行操作非常容易),然后使用javascript更改表单
或者让创建表单的php代码与表单提交的php代码相同,然后根据是否有get参数输出不同的内容。(编辑:MonkeyZeus向您详细介绍了如何做到这一点。)如果您对AJAX(这是一种实现AJAX的方法)非常确定,请阅读一些相关内容。谷歌
AJAX
至少:)你为AJAX做了哪些尝试?试着使用AJAX,然后告诉我们问题所在。我不想回答这个问题,因为它很琐碎,但如果你读了这篇文章,你就可以在一行内完成(如果你想点击它,可以在2-3行内完成)@Dexa可能不会有任何问题,考虑到OP的业绩记录,从未接受过任何答案。所以你的和其他人一样好。如果你对AJAX(这是一种实现AJAX的方法)非常确定,那么就读一读它。谷歌AJAX
至少:)你为AJAX做了哪些尝试?试着使用AJAX,然后告诉我们问题所在。我不想回答这个问题,因为它很琐碎,但如果你读了这篇文章,你就可以在一行内完成(如果你想点击它,可以在2-3行内完成)@Dexa可能不会有任何问题,考虑到OP的业绩记录,从未接受过任何答案。所以你的也和其他人一样好。你的if(isset($\u POST['submit']){
不起作用。我把它改为if(isset($\u GET['submit']){
现在它起作用了。在Ajax方法中,你的类型:“POST”
很可能需要设置为类型:“GET”,
。现在,如果有人输入“煎饼”怎么办不是“煎饼”?;-)哈哈,我需要更多的咖啡!这是一个大的“10-4”(不需要额外的字符);-)你有if(isset($\POST['submit']){
不起作用。我把它改为if(isset($\u GET['submit']){
现在它起作用了。你的类型:“POST”,
很可能需要设置为类型:“GET”,
在您的Ajax方法中。现在,如果有人输入“煎饼”而不是“煎饼”;-)哈哈,我还需要一些咖啡!这是一个很大的“10-4”(不需要额外的字符);-)