将带有 POST 的变量从一页传递到另一页并打印前一页中的数据
Passing variable with POST from one page to another and print data in the previous one
我有 2 个文件:basic.php 和 pptimeline.php。第一个包含接口,第二个是从数据库获取数据的文件,它应该模仿 json 文件,因为我在上面使用它:header('Content-Type: application/json');
我有一个组合框,它显示数据库中的进程数,然后在时间轴中显示它。我设法将 $nprocesso
从 basic.php 传递到 pptimeline.php 并在那里打印。问题是我想将 $nprocesso
传递给 pptimeline.php,运行 数据库的查询,然后在 basic.php 中打印数据,但我不知道该怎么做因为表单操作让我停留在 pptimeline.php 页面,打印文本为 json 格式。
希望我说清楚了。
basic.php
<form action="json/pptimeline.php" method="POST" >
<label for="Process"> NProcess : </label>
<select id="cproc" name="NProc" onchange="document.getElementById('nprocesso').value=this.options[this.selectedIndex].text">
<?php
foreach ($products as $res3)
{
echo "<option value='".$res3["PROCESSO"]."'>".$res3["PROCESSO"]."</option>";
}
?>
</select>
<input type="hidden" name="nprocesso" id="nprocesso" value="" />
<input type="submit" name="search" value="Search"/>
</form>
<?php
if(isset($_POST['search']))
{
$nprocValue = $_POST['Proc'];
$nproc = $_POST['nprocesso']; // get the selected text
}
?>
pptimeline.php
if (isset ($_REQUEST['nprocesso'])) {
$nprocesso = $_REQUEST['nprocesso'];
echo $nprocesso;
}
一种方法是使用 add header('Location: $new_url');将您的 pptimeline.php 重定向回 basic.php:
if (isset ($_REQUEST['nprocesso'])) {
$nprocesso = $_REQUEST['nprocesso'];
echo $nprocesso;
header('Location: basic.php?v=$nprocesso');
}
并在basic.php末尾添加以下内容:
if (isset($_GET['v']))
{
echo $_GET['v'];
}
更多
上面提供的解决方案可以解决问题,但远非最佳方式。
您正在寻找的是从 basic.php 向 pptimeline.php 发出 AJAX 请求,并将结果返回显示在 basic.php。
要涵盖的内容太多,要更改的代码会告诉您确切的操作方法。但我会尝试总结一下:
- 在 basic.php
中包含 Jquery
- 删除
<form></form>
围绕您的 <select></select>
,而是将事件处理程序添加到 click events
- 在点击处理程序中,获取用户选择的值
- 在点击处理程序中创建一个 AJAX request 并发送(通过 POST)值
在pptimeline.php中:
if (isset ($_POST['v']))
而不是 $_REQUEST[]
- 删除 header() 函数,而只是将要发送回 basic.php 的数据回显。
- 如果你想要JSON
中的数据,别忘了保留header('Content-Type: application/json')
一个AJAXpost骨架:
$("#form1").on('click', function(){
// get the selected value from <select>
//var value = ....
$.ajax()
{
url: "pptimeline.php",
type: "POST",
data: {"v" : value}
success: function(response) {
// some code to neatly display the results
},
error: function(x,y,z){
alert("an error occured");
}
});
详细代码
basic.php
为了简单起见,我用索引值替换了 <select></select>
中的选项值。
<label for="Process"> NProcess : </label>
<select id="cproc" name="NProc" onchange="document.getElementById('nprocesso').value=this.options[this.selectedIndex].text">
<?php
$i = 0;
for ($i=0; $i < 4;$i)
{
echo "<option value='$i'>". $i ."</option>";
$i++;
}
?>
</select>
<button id="search-button"> Search-1 </button>
<table border="1px" id="processes-table">
<tr>
<th> id</th>
<th> title</th>
<th> description</th>
<th> focus_date</th>
</tr>
</table>
</body>
<script>
$("#cproc").on('change', function(){
var v1 = $(this).val();
$.ajax({
url: "pptimeline.php",
type: "POST",
data: {'value' : v1},
success: function(response){
var array = JSON.parse(response);
var _id = array['id'];
var _title = array['title'];
var _descr = array['description'];
var _focus_date = array['focus_date'];
var string = '<tr> <td>' + _id + ' </td> <td>'+ _title +' </td><td>'+_descr + '</td><td>' + _focus_date + '</td> </tr>';
$('#processes-table tr:last').after(string);
},
error: function(x,y,z){
alert("error");
}
});
});
</script>
pptimeline.php
if (isset ($_POST['value'])) {
$infotimeline = array();
/*
* REPLACE this with Database fetching/quering
*/
$infotimeline['id'] = 322;
$infotimeline['title'] = "the lion king";
$infotimeline['description'] = "good movie";
$infotimeline['focus_date'] = 1990;
$data = json_encode($infotimeline);
echo $data;
}
?>
我有 2 个文件:basic.php 和 pptimeline.php。第一个包含接口,第二个是从数据库获取数据的文件,它应该模仿 json 文件,因为我在上面使用它:header('Content-Type: application/json');
我有一个组合框,它显示数据库中的进程数,然后在时间轴中显示它。我设法将 $nprocesso
从 basic.php 传递到 pptimeline.php 并在那里打印。问题是我想将 $nprocesso
传递给 pptimeline.php,运行 数据库的查询,然后在 basic.php 中打印数据,但我不知道该怎么做因为表单操作让我停留在 pptimeline.php 页面,打印文本为 json 格式。
希望我说清楚了。
basic.php
<form action="json/pptimeline.php" method="POST" >
<label for="Process"> NProcess : </label>
<select id="cproc" name="NProc" onchange="document.getElementById('nprocesso').value=this.options[this.selectedIndex].text">
<?php
foreach ($products as $res3)
{
echo "<option value='".$res3["PROCESSO"]."'>".$res3["PROCESSO"]."</option>";
}
?>
</select>
<input type="hidden" name="nprocesso" id="nprocesso" value="" />
<input type="submit" name="search" value="Search"/>
</form>
<?php
if(isset($_POST['search']))
{
$nprocValue = $_POST['Proc'];
$nproc = $_POST['nprocesso']; // get the selected text
}
?>
pptimeline.php
if (isset ($_REQUEST['nprocesso'])) {
$nprocesso = $_REQUEST['nprocesso'];
echo $nprocesso;
}
一种方法是使用 add header('Location: $new_url');将您的 pptimeline.php 重定向回 basic.php:
if (isset ($_REQUEST['nprocesso'])) {
$nprocesso = $_REQUEST['nprocesso'];
echo $nprocesso;
header('Location: basic.php?v=$nprocesso');
}
并在basic.php末尾添加以下内容:
if (isset($_GET['v']))
{
echo $_GET['v'];
}
更多
上面提供的解决方案可以解决问题,但远非最佳方式。
您正在寻找的是从 basic.php 向 pptimeline.php 发出 AJAX 请求,并将结果返回显示在 basic.php。
要涵盖的内容太多,要更改的代码会告诉您确切的操作方法。但我会尝试总结一下:
- 在 basic.php 中包含 Jquery
- 删除
<form></form>
围绕您的<select></select>
,而是将事件处理程序添加到 click events - 在点击处理程序中,获取用户选择的值
- 在点击处理程序中创建一个 AJAX request 并发送(通过 POST)值
在pptimeline.php中:
if (isset ($_POST['v']))
而不是 $_REQUEST[]- 删除 header() 函数,而只是将要发送回 basic.php 的数据回显。
- 如果你想要JSON 中的数据,别忘了保留
header('Content-Type: application/json')
一个AJAXpost骨架:
$("#form1").on('click', function(){
// get the selected value from <select>
//var value = ....
$.ajax()
{
url: "pptimeline.php",
type: "POST",
data: {"v" : value}
success: function(response) {
// some code to neatly display the results
},
error: function(x,y,z){
alert("an error occured");
}
});
详细代码
basic.php
为了简单起见,我用索引值替换了 <select></select>
中的选项值。
<label for="Process"> NProcess : </label>
<select id="cproc" name="NProc" onchange="document.getElementById('nprocesso').value=this.options[this.selectedIndex].text">
<?php
$i = 0;
for ($i=0; $i < 4;$i)
{
echo "<option value='$i'>". $i ."</option>";
$i++;
}
?>
</select>
<button id="search-button"> Search-1 </button>
<table border="1px" id="processes-table">
<tr>
<th> id</th>
<th> title</th>
<th> description</th>
<th> focus_date</th>
</tr>
</table>
</body>
<script>
$("#cproc").on('change', function(){
var v1 = $(this).val();
$.ajax({
url: "pptimeline.php",
type: "POST",
data: {'value' : v1},
success: function(response){
var array = JSON.parse(response);
var _id = array['id'];
var _title = array['title'];
var _descr = array['description'];
var _focus_date = array['focus_date'];
var string = '<tr> <td>' + _id + ' </td> <td>'+ _title +' </td><td>'+_descr + '</td><td>' + _focus_date + '</td> </tr>';
$('#processes-table tr:last').after(string);
},
error: function(x,y,z){
alert("error");
}
});
});
</script>
pptimeline.php
if (isset ($_POST['value'])) {
$infotimeline = array();
/*
* REPLACE this with Database fetching/quering
*/
$infotimeline['id'] = 322;
$infotimeline['title'] = "the lion king";
$infotimeline['description'] = "good movie";
$infotimeline['focus_date'] = 1990;
$data = json_encode($infotimeline);
echo $data;
}
?>