简体   繁体   中英

jQuery Ajax and POST method in PHP

I have two files basic.php and pptimeline.php . The purpose here is to select a value from a combobox in basic.php , process it in the pptimeline.php and print it back in basic.php . But I'm not having success in doing so. If anyone can help I appreciate.

EDIT: The pptimeline.php file acts as a json file header('Content-Type: application/json'); . It has only querys which I then echo in the file but are not relevant here since everything works if I use default values instead of $nprocess in the query. The code that displays my data is inside the sucess: parameter in basic.php . If I remove what is around it everything works fine, but then it doen'st change based on what I choose.

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."";
}

Your $.ajax() call is unnecessary. Remove it, and just add your selected value to the .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>

Then change your php code from $_POST to $_GET

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM