Passing return variable from ajax to php

this is my ajax code which works fine if i display using id.

$("#masa").change(function() 
    { //if theres a change in the nokakitangan textbox

        var masa = $("#masa").val();
        var tkh_kerja = $("#tkh_kerja").val();
        //Get the value in the nokakitangan textbox

        if(tkh_kerja=='')
        {
            $("#cek_tarikh").html('<font color="Red"> &nbsp; Sila Isi Maklumat Tarikh Mula Bekerja </font>');
        }

        else
        {
            $("#cek_tarikh").html('<align="absmiddle">&nbsp;Cek Tarikh Akhir...');
            $.ajax
            ({  //Make the Ajax Request
                type: "POST",  
                url: "ajax_cek_tarikhakhir.php",  //file name
                data: "bulan="+ masa +"&tkh_kerja="+tkh_kerja,  //data
                success: function(cekmasa)
                { 
                    $("#cek_tarikh").ajaxComplete(function(event, request)
                    { 
                         $("#cek_tarikh").val(cekmasa);

                    });
                }
            });
        }

        return false;
    });

      

the problem is, can the value "cekmasa" go to php.? I need to use this value to execute some code in php. is it possible to get data from ajax? on the same page.

no problem when i return the value in

<input align='right' type="text" name="tkh_akhir" maxlength="3" id="cek_tarikh" class="span2" readonly/>

      

+3


source to share


3 answers


No, It is Immpossible.
as soon as your PHP page is processed by the server, it sends the generated output to the client. and PHP fails unless you reload the page or navigate to another page in your browser.

additional information: which is often confused by people who start working with ajax

. you are using ajax because you need some kind of "realtime behavior" without reloading the whole html page. but the ajax is still done by your browser client side.

simplified what you want to achieve with ajax, communication between your browser and server:

client(browser) <- ajax -> server(PHP)

      




but what you are asking would be something like this:

server(PHP) <- ajax -> server(PHP)

      

which doesn't work and doesn't really make sense if you think about it.

+1


source


you can call another ajax function to use php file inside success response like below



$("#masa").change(function() 
    { //if theres a change in the nokakitangan textbox

        var masa = $("#masa").val();
        var tkh_kerja = $("#tkh_kerja").val();
        //Get the value in the nokakitangan textbox

        if(tkh_kerja=='')
        {
            $("#cek_tarikh").html('<font color="Red"> &nbsp; Sila Isi Maklumat Tarikh Mula Bekerja </font>');
        }

        else
        {
            $("#cek_tarikh").html('<align="absmiddle">&nbsp;Cek Tarikh Akhir...');
            $.ajax
            ({  //Make the Ajax Request
                type: "POST",  
                url: "ajax_cek_tarikhakhir.php",  //file name
                data: 
                 {
                   bulan:masa,
                   tkh_kerja:tkh_kerja
                     },  //data
                success: function(cekmasa)
                { 
                    $("#cek_tarikh").ajaxComplete(function(event, request)
                    { 
                         $("#cek_tarikh").val(cekmasa);
           $.ajax
            ({  //Make another Ajax Request
                type: "POST",  
                url: "",  //file name
                data: 
                 {

                     },  //data
                success: function()
                { 
                }
                });

                    });
                }
            });
        }

        return false;
    });

      

0


source


You can do one thing. You can store the ajax response in a php or cookie session and then just refresh the page in response to ajax.

 //File: ajax_cek_tarikhakhir.php
 // Your ajax handling code.
 if(isset($__SESSION('ckmasa')){
    $ckmasa = $__SESSION('ckmasa');     
    // use this $ckmasa for your php
 }

 $ckmasa = $output; // $ckmasa have data which you send to ajax response.
 $__SESSION['ckmasa'] = $ckmasa;
 echo $ckmasa;
 return;

      

For your jQuery. Follow these steps.

$ ("#masa"). Modify (function () {// if change in textbox nokakitangan

    var masa = $("#masa").val();
    var tkh_kerja = $("#tkh_kerja").val();
    //Get the value in the nokakitangan textbox

    if(tkh_kerja=='')
    {
        $("#cek_tarikh").html('<font color="Red"> &nbsp; Sila Isi Maklumat Tarikh Mula Bekerja </font>');
    }

    else
    {
        $("#cek_tarikh").html('<align="absmiddle">&nbsp;Cek Tarikh Akhir...');
        $.ajax
        ({  //Make the Ajax Request
            type: "POST",  
            url: "ajax_cek_tarikhakhir.php",  //file name
            data: "bulan="+ masa +"&tkh_kerja="+tkh_kerja,  //data
            success: function(cekmasa)
            { 
                $("#cek_tarikh").ajaxComplete(function(event, request)
                { 
                     $("#cek_tarikh").val(cekmasa);
                     // Reload page
                     location.reload();
                });
            }
        });
    }

    return false;
});

      

I just reloaded your page in ajax response. I have no idea how your server side code is. but I tried to give a short description. hope this helps you. I have not tested the code written above. Sorry for my bad english.

0


source







All Articles