Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing return variable from ajax to php

Tags:

ajax

php

this is my ajax code that work perfectly if i show using an 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 'cekmasa' value pass to the php.? i need to use that value to do some code in php. is it possible to get the data from ajax? in the same page.

there is no problem when i return value in

<input align='right' type="text" name="tkh_akhir" maxlength="3" id="cek_tarikh" class="span2" readonly/>
like image 962
user3487681 Avatar asked Feb 26 '26 07:02

user3487681


1 Answers

no, that's not possible.
once your PHP page is processed by the server it sends the generated output to the client. and there is no PHP executed unless you reload the page or go to another page in your browser.

additional information: that's what often gets confused by people that start working with ajax. you use ajax because you want some "realtime behavior" without having to reload a whole html page. but ajax still is executed by your browser on the clientside.

simplified that's what you want to achieve with ajax, a communication between your browser and the server:

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


but what you are asking for 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.

like image 155
low_rents Avatar answered Feb 28 '26 19:02

low_rents



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!