0

I would like to use AJAX to call a single query in a php file which has multiple but I'm not entirely sure how to go about doing this.

In essence the php file will have three PDO queries which will be update, delete and add. Currently it just has the update query. How would i add a second query to the file and get AJAX to call particular query?

The AJAX code:

function updateCall() {
  var data = $('#updateForm').serialize();
  $.post('ManageCourses_DeleteSubmit.php', data, function(response){

    $("#updateForm").html(response);
    //'soft'reload parent page, after a delay to show message
    setTimeout(function(){
      $('#editModal').modal('hide')
      location.reload();
    },2000);

  }).fail(function(jqXHR, textStatus) {
    alert( "Request failed: " + textStatus );
  });
}

the php file:

<?php

include "db_conx.php";

try
{
    $db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);

    $db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = $db_conx->prepare("UPDATE course_details SET course_title = :course_title 
        WHERE course_code = :course_code");

    $course_title = $_POST['course_title'];
    $course_code = $_POST['course_code'];

    $sql->bindParam(':course_title', $course_title, PDO::PARAM_STR);
    $sql->bindParam(':course_code', $course_code, PDO::PARAM_STR);


    /*** execute the prepared statement ***/
    $sql->execute();

    /*** success message ***/


    $message = "<p class='text-success'> Record Successfully Updated <span class='glyphicon glyphicon-ok'/></p>";
} 
catch(Exception $e)
{
    $message = 'Message: ' .$e->getMessage();
}

die($message);
?>

Any examples?

Thanks!

user90210
  • 115
  • 4
  • 17

1 Answers1

2

The $.ajax() code block looks like this:

$.ajax({
    type: "POST",
    url: "receiving_file.php",
    data: 'selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,
    success:function(data){
        alert('This was sent back: ' + data);
    }
});

Note the data: line

Just use another variable to identify which PHP routine you will call. Something like:

data: 'myrequest=add&selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,

versus

data: 'myrequest=delete&selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,

Then, in your PHP file, test for that variable:

<?php
$req = $_POST['myrequest'];
if ($req == 'add'){
    //do the add
}else if ($req == 'delete'){
    //etc
}

Alternately, you can use a single $.ajax() code block, and use a variable to determine which PHP function to call:

if (useraction=='add') {
    myreq = 'add';
}else if(useraction=='del') {
    myreq = 'delete';
}

//later on in the code...

$.ajax({
    type: "POST",
    url: "receiving_file.php",
    data: 'myrequest=' +myreq+ '&selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,
    success:function(data){
        alert('This was sent back: ' + data);
    }
});
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • i was going to create three separate ajax calls - one for update, one for delete and one for add. and then hoped something in the code would be able to distinguish which query to carry out – user90210 Mar 02 '15 at 16:21
  • Yes, you would have three `$.ajax()` code blocks in your javascript/jQuery code, but each would have a different `myrequest=____` parameter. On the PHP side, you can see the contents of that variable. – cssyphus Mar 02 '15 at 16:23
  • @user90210 If you would like to see a few more simplified AJAX examples, [see this post](http://stackoverflow.com/questions/17973386/ajax-request-callback-using-jquery/17974843#17974843) – cssyphus Mar 02 '15 at 16:29
  • thank you, forgive me for being naive but I'm confused how the 'myrequest=____ parameter' would connect to the php file. How do i create multiple queries in the php and get ajax to distinguish between them? – user90210 Mar 02 '15 at 16:34
  • oh i see the last code bit in your comment. sorry! :$ – user90210 Mar 02 '15 at 16:35
  • No worries -- do please take a look at the 3 posts I recommended above [(here they are again)](http://stackoverflow.com/questions/17973386/ajax-request-callback-using-jquery/17974843#17974843). You may find something helpful. Best regards. – cssyphus Mar 02 '15 at 16:36