我有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;
}
一种方法是使用添加标头('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上。
内容太多了,需要更改代码以告诉您确切的操作方法。但我会尝试总结一下:
<form></form>
您周围的<select></select>
,而是添加事件处理程序以单击事件在pptimeline.php中:
if (isset ($_POST['v']))
而不是$ _REQUEST []header('Content-Type: application/json')
如果您想要JSON中的数据,请别忘了保留一个AJAX发布框架:
$("#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;
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句