user3487681
user3487681

Reputation: 149

passing return variable from ajax to 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/>

Upvotes: 3

Views: 90

Answers (3)

jagad89
jagad89

Reputation: 2643

You can do one thing. You can store ajax response in php session or cookie and then simply refresh page on ajax response.

 //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. Do as following.

$("#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);
                     // Reload page
                     location.reload();
                });
            }
        });
    }

    return false;
});

I have just reloaded your page on ajax response. I have no idea how your server side code. but i tried to give some brief. hope this helps you. I have not tested code written above. Sorry for my bad English.

Upvotes: 0

low_rents
low_rents

Reputation: 4481

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.

Upvotes: 1

Vivek Singh
Vivek Singh

Reputation: 2447

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;
    });

Upvotes: 0

Related Questions