-3

How can i call a php function with a query inside on ajax?

My html:

<input type="submit" value="Open Process" name="openProcess" id="openProcess" onclick="javascript:applicationOpenProcess()" >

And my php function:

function openProcess(){
    $id = $_GET['application']; //Nodata need where is coming from the URL
    $openProcess = "UPDATE applications SET  process='1' WHERE application_id='".$id."'";
    $valProcess = db_query($openProcess, "+"); //function to execute query
}

Guys i really need help on this one Thanks mates

Xatenev
  • 6,383
  • 3
  • 18
  • 42
MrJootta
  • 13
  • 1

4 Answers4

1

The first thing you need to do is take a look at the Ajax documentation:https://api.jquery.com/jQuery.ajax/

Ajax is for sending requests but it is possible to get something to run on the success:

Here is a basic Ajax call:

$.ajax({
    url: "ajax_content.php",
    type: "POST",
    data: ({
        page: page,
        content: content
    }),
    success: function (data) {  alert (data) }
 });

As you can see the function in success passes back 'data'. so, lets say you wanted to pass back some information you had got from a database. you would need to make sure that the output is json_encode($data); in the ajax_content.php page. That would then give you something. You can then use alert() to see the data. Make sense?

MarkP
  • 2,546
  • 5
  • 31
  • 48
  • page:page and content:content are declared variables that allow you pass info to the file. Like if you needed to pass a password or id etc.. – MarkP Apr 28 '14 at 09:20
0

By ajax you can only send request. You may send it to the page that calls your function as you need

Igor
  • 178
  • 10
0

what you need to is create a php file write your code and simply call that php file via ajax

 $.ajax({
        type: 'POST',
        url:"example.php",
        dataType:'json',
        success: function () { }
});
sshet
  • 1,152
  • 1
  • 6
  • 15
0

Check this other answer: jQuery Ajax POST example with PHP

as lampdev wrote above, you'll just need to adapt the basic aJax call to your code

 $.ajax({
        type: 'POST',
        url:"code.php",
        dataType:'json',
        success: function () { }
});

The other answer will help you doing what you want, and it's great for ajax beginners

You'll need 3 files, supposing you've js in a different file as the form. It will be something like:

  • form.php/html
  • script.js
  • code.php
Community
  • 1
  • 1
xickoh
  • 304
  • 3
  • 10