PHP中的jQuery Ajax和POST方法

时间:2015-04-05 14:24:39

标签: php jquery ajax

我有两个文件basic.phppptimeline.php。这里的目的是从basic.php中的组合框中选择一个值,在pptimeline.php中对其进行处理,然后将其打印回basic.php。但我没有成功这样做。如果有人可以帮助我欣赏。

编辑:pptimeline.php文件充当json文件header('Content-Type: application/json');。它只有查询,然后我在文件中回显但在这里不相关,因为如果我在查询中使用默认值而不是$nprocess,一切都有效。显示我的数据的代码位于sucess:中的basic.php参数内。如果我删除它周围的东西一切正常,但它会根据我的选择做出改变。

Basic.php

<label for="Process"> NProcess : </label>
<select id="cproc">
  <?php
foreach ($products as $res3)
    {echo "<option value='".$res3["PROCESSO"]."'>".$res3["PROCESSO"]."</option>";}
    ?>
    </select>
<script type='text/javascript'> 
    $("#cproc").on("change", function(){
        var v1 = $(this).val();
        $.ajax({
            url: "pptimeline.php", 
            type: "POST", 
            data: {'value' : v1}, 
            success: function(response){

            /**DISPLAY DATA IN INTERFACE**/
             var tg1 = {};
    $(function () { 
        // jQuery widget implementation
        // with some basic options
        tg1 = $("#placement").timeline({
                "min_zoom":1, 
                "max_zoom":55, 
                "image_lane_height":100,
                "icon_folder":"timeglider/icons/",
                "data_source":"pptimeline.php",
                "constrain_to_data": false
        });
        tg_actor = tg1.data("timeline");
        // You'll use tg_actor as the key to access 
        // public API methods like "goTo", etc.         
    }); // end document-ready
                      /****/

   }, error: function(x,y,z){
                alert("error"); } }); }); 
</script>

pptimeline.php

if (isset ($_POST['value'])) {
    $nprocesso = $_POST['value'];

$query1 = "SELECT TO_CHAR(DATACRIACAO,'YYYY-MM-DD') AS DATACRIACAO, NPROCESSO, 
BLOCOOPERATORIO, TIPOINTERNAMENTO, TIPOINTERNAMENTOCIRURGIA, GLASGOW_HOSPITAL 
FROM PATIENT_TIMELINE_ADMISSAO WHERE NPROCESSO =".$nprocesso."";
}

1 个答案:

答案 0 :(得分:2)

您的$.ajax()电话是不必要的。将其删除,只需将所选值添加到.timeline() data_source -

即可
<script type='text/javascript'> 
    $("#cproc").on("change", function(){
        var v1 = $(this).val();
        var tg1 = {};
        $(function () { 
            // jQuery widget implementation
            // with some basic options
            tg1 = $("#placement").timeline({
                "min_zoom":1, 
                "max_zoom":55, 
                "image_lane_height":100,
                "icon_folder":"timeglider/icons/",
                "data_source":"pptimeline.php?value="+v1, //add select value to url
                "constrain_to_data": false
            });
            tg_actor = tg1.data("timeline");
            // You'll use tg_actor as the key to access 
            // public API methods like "goTo", etc.         
        }); // end document-ready
    }); 
</script>

然后将您的PHP代码从$_POST更改为$_GET

if (isset ($_GET['value'])) {
    $nprocesso = $_GET['value']; //make sure to sanitize value to prevent SQL injection

    $query1 = "SELECT ... NPROCESSO =".$nprocesso."";
}