3

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/>
user3487681
  • 149
  • 1
  • 1
  • 9
  • You have to send another ajax request to some php file to do the working. – Umair Khan Jun 08 '15 at 06:22
  • That won't make any difference @Saty and instead it should be `data: {bulan:masa ,tkh_kerja:tkh_kerja}` – Narendrasingh Sisodia Jun 08 '15 at 06:26
  • Try to `alert(cekmasa)` and check are you getting response – Narendrasingh Sisodia Jun 08 '15 at 06:27
  • @Uchiha thanks for picking up . Is there is no difference between `data: {bulan:masa ,tkh_kerja:tkh_kerja}` and `data: "bulan="+ masa +"&tkh_kerja="+tkh_kerja, ` ??? – Saty Jun 08 '15 at 06:28
  • Nope there's no difference between them you can check it over here http://stackoverflow.com/questions/10078085/how-to-send-multiple-data-with-ajax-jquery @Saty and also check OPs question _this is my ajax code that work perfectly if i show using an id_ – Narendrasingh Sisodia Jun 08 '15 at 06:33
  • @Uchiha thanks for this link. Now i understand there is no difference between then. Thanks once again!! – Saty Jun 08 '15 at 06:38

3 Answers3

1

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.

low_rents
  • 4,481
  • 3
  • 27
  • 55
0

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;
    });
Vivek Singh
  • 2,453
  • 1
  • 14
  • 27
0

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.

jagad89
  • 2,603
  • 1
  • 24
  • 30