通过POST将变量从一个页面传递到另一个页面,并打印前一个页面中的数据


Passing variable with POST from one page to another and print data in the previous one

我有两个文件:basic.phppptimeline.php。第一个保存接口,第二个是从数据库获取数据的文件,它应该模仿json文件,因为我在上面使用它:header('Content-Type: application/json');

我有一个组合框,显示数据库中进程的编号,然后在时间轴中显示。我设法将$nprocesso从basic.php传递到pptimeline.php并在那里打印。问题是,我想将$nprocesso传递到pptimeline.php,运行数据库的查询,然后在basic.php中打印数据,但我不知道如何做到这一点,因为表单操作让我停留在pptimelinephp页面上,打印的文本为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.hp:

if (isset ($_REQUEST['nprocesso'])) {
    $nprocesso = $_REQUEST['nprocesso'];
    echo $nprocesso;
    header('Location: basic.php?v=$nprocesso'); 
}

在basic.php中,在末尾添加以下内容:

if (isset($_GET['v']))
{
    echo $_GET['v']; 
}

MOREOVER

上面提供的解决方案确实奏效,但远不是最佳的方法。

您正在寻找的是从basic.php向pptimeline.php发出AJAX请求,并将结果返回到basic.php上显示。

要涵盖的内容太多了,要更改代码来告诉你如何做到这一点。但我将尝试总结一下:

  1. 在basic.php中包含Jquery
  2. 删除<select></select>周围的<form></form>,而是添加一个事件处理程序来单击事件
  3. 在单击处理程序中,获取用户选择的值
  4. 在点击处理程序中发出AJAX请求,并(通过POST)发送值

在pptimeline.php中:

  1. if (isset ($_POST['v']))而不是$_REQUEST[]
  2. 删除header()函数,而只是将要发送回basic.php的数据回显
  3. 如果您想要JSON格式的数据,请不要忘记保留header('Content-Type: application/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<?php

if (isset ($_POST['value'])) {
    $infotimeline = array(); 
    /*
     * REPLACE this with Database fetching/querying  
     */ 
    $infotimeline['id'] = 322; 
    $infotimeline['title'] = "the lion king";
    $infotimeline['description'] = "good movie";
    $infotimeline['focus_date'] = 1990; 
    $data = json_encode($infotimeline); 
    echo $data; 
}
?>