-1

I have written a PHP program to confirm deletion of data through jQuery confirm message (Refer: http://rathur.fr/jQuery/MsgBox/) and record the result back to the same page itself instantly. If the page refreshes, it'll return back its state to previous.

The part of line is below:

print "
<script type='text/javascript'>
$(document).ready(function(){
  $.msgbox('<br/>Are you sure you want to delete the selected record from the database?', {
    type : 'confirm'
  }, function(result){
    $('#output').text(result);
    var output = result;
  });
});
</script>";

I want to get the result of the action button to PHP variable instantly, like below (just a trial):

$x = $_SESSION['output']; OR
$x = $_POST['output']; OR
$x = print "<div id=\"output\"></div>"; OR
$x = some_function(output);

Please help me, or suggest if there is other better options.

ak-SE
  • 980
  • 1
  • 7
  • 27

2 Answers2

1

Here is a simple Ajax call to a Php File by an event : Click on a button.

Javascript client side :

 $("body").on("click", "#mybutton", function() {
            var mydata = $("#form").serialize();
            $.ajax({
                type: "POST",
                url: "/api/api.php",
                data: {data : mydata},
                timeout: 6e3,
                error: function(a, b) {
                    if ("timeout" == b) $("#err-timedout").slideDown("slow"); else {
                        $("#err-state").slideDown("slow");
                        $("#err-state").html("An error occurred: " + b);
                    }
                },
                success: function(a) {
                    var e = $.parseJSON(a);
                    if (true == e["success"]) {
                        $("#action").html(e['message']);
                        // here is what you want, callback Php response content in Html DOM
                    }
                }
            });
            return false;
        });

Next in your Php code simply do after any success function :

if ($result) {
            echo json_encode(array(
                'success' => true,
                'msg' => "Nice CallBack by Php sent to client Side by Ajax Call"
            ));
        }
Xanarus
  • 763
  • 1
  • 6
  • 18
  • How can I add Ajax to PHP call script with the existing jQuery script? I also need the existing jQuery script to run/display the jQuery alert message, as mentioned earlier. – ak-SE Nov 06 '14 at 18:48
0

You should use jQuery to POST the data to a PHP script using AJAX if you want to use the second pass.

http://api.jquery.com/category/ajax/ has many functions and tutorials on writing AJAX functions and handling return data. In particular, look at the post() function.

Ramsay Smith
  • 1,098
  • 13
  • 30