Biker John
Biker John

Reputation: 2711

PHP post with ajax

I would like to trigger the php post to database script without refreshing the page (like it happens with regular php self).

Upvotes: 0

Views: 177

Answers (4)

Satyapriya Mishra
Satyapriya Mishra

Reputation: 130

The below code might help you in php post using jquery ajax. Suppose you have a form with id=form and a button to submit the form whose id=submit1. Now to send the form data to another page using jquery ajax you need the following code just before the end of body.Dont forget to attach the jquery script before writing the below code.

$(document).ready(function(){
  $('submit1').click(function(event){
   //first stop the default submit action
    event.preventDefault();
   //now we will do some ajax
   $.ajax({
    url:'test.php',//the target page where to submit the data
    type:'post',
    cache:false,
    data:$('#form').serialize(),//data to be sent from the form
    dataType:'json',//expected data type from the server
    error:function(xhr){
                 alert("the error is"+xhr.status);
                    }
  })
     //success function in terms of .done
    .done(function(data){
     alert("your data successfully submitted");
    });
  });
});    

Upvotes: 1

Prasad Rajapaksha
Prasad Rajapaksha

Reputation: 6190

Here is a basic sample code for Ajax request.

function postData()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    // Do anything you want with the response
    alert(xmlhttp.responseText);
    }
  }
xmlhttp.open("POST","page_to_be_post.php",true);
xmlhttp.send();
}

However you can use some frameworks to do this easily with less code.

Ex:

http://www.prototypejs.org/learn/introduction-to-ajax

http://api.jquery.com/jQuery.post/

There are many more frameworks available.

Cheers!

Upvotes: 1

user669677
user669677

Reputation:

I'm using this to do what you want:

function parseJsonIfPossible(code){
    try {
        return $.parseJSON(code);
    } catch (e) {
        return code;
    }
}
function cmd(command,p1,p2,p3,p4,p5,p6){ 
    return parseJsonIfPossible($.ajax({
        url: core_url,
        global: false,
        type: "POST",
        data: {command : command,p1:p1,p2:p2,p3:p3,p4:p4,p5:p5,p6:p6},
        dataType: "html",
        async:false,
        success: function(msg){
          //alert(msg);
        }
    }).responseText);   
}

PHP:

function cmd()
{
$command = &$_POST['command'];
$p1 = &$_POST['p1'];$p2 = &$_POST['p2'];$p3 = &$_POST['p3'];$p4 = &$_POST['p4'];$p5 = &$_POST['p5'];$p6 = &$_POST['p6'];
if($command)echo($command($p1,$p2,$p3,$p4,$p5,$p6));
}
function test($i)
{
    //simple
    return mysql_query("UPDATE ... SET b='$i'");
    //array
    return json_encode(array(
       mysql_query("UPDATE ... SET b='$i'"),
       "fklsdnflsdnl",
       ));
}

usage (script):

$('#btn').click(function(){
   var i = 99;
   var result = cmd("test",i);
   //simple
   alert(result);
   //array
   console.log([result[0],result[1]]);
});

Upvotes: 2

Wes Crow
Wes Crow

Reputation: 2967

Check out jQuery $.post( ) functionality. Here is a link to get you started: http://api.jquery.com/jQuery.post/

Upvotes: 3

Related Questions