0

I am sending data through jquery. my equivalent code is...

$('#pcpmnum').blur(function(){
//alert("HIiiiiii");
var pcpmnum = $("#pcpmnum").val();
if(pcpmnum === "" | pcpmnum === null)
    { 
        alert("Please Enter Mobile Number");
    }
else
    {
        alert(pcpmnum);
        $.post("searchpcp.php", {cntctnumber: "+pcpmnum+"}, function(){
            alert("Success");
                    }
    }

});

on my php file I have simply used.

echo "HIIII";

Is this $.post function is equivalent to Ajax function?

Rahul Singh
  • 1,614
  • 6
  • 22
  • 39
  • $.post is a shortcut for $.ajax so you don't have to fill in as many options. – Richard Dalton Sep 06 '11 at 10:44
  • `$.post` is a shorthand for `$.ajax` with certain presets (http://api.jquery.com/jQuery.post). From the code it should be calling the `searchpcp.php` properly, but why do you pass the `pcpmnum` as a string instead of passing its value (without the quotes and the +es)? – m90 Sep 06 '11 at 10:44

1 Answers1

2

simply do

  {cntctnumber: pcpmnum, second:"second variable" }

and in the php file you can get the value as

$contact = $_POST["cntctnumber"]; // you will get the value of pcpmnum here
$sec = $_POST["second"]; // you will get "second variable" here

in the success call back the argument data in your case contains the server response e.g. in you php file you are echoing

...
echo"howdy";

on the client side data will be holding this response

 $.post("searchpcp.php", {cntctnumber: pcpmnum, second:"second variable" }, function(data){
            alert(data);//howdy
           });

here are some useful links

jQuery, Ajax, Json and Php

Returning JSON from PHP to JavaScript?

Community
  • 1
  • 1
Rafay
  • 30,950
  • 5
  • 68
  • 101
  • If I need to send 2 variables? Second is this is equivalent to send Ajax request? what does it mean If I use `function(data)` – Rahul Singh Sep 06 '11 at 10:44
  • ill edit the answer for two vars and yes `$.post` uses ajax call at the backend it is just the short hand for `$.ajax` – Rafay Sep 06 '11 at 10:45
  • I want to return an array of data. and fill the respective text field from that returned data. Suppose I have input with `id=firstname` and my second would be 'id=middlename'. I am firing querey in my PHP file. How would I return those data. – Rahul Singh Sep 06 '11 at 11:01
  • i have posted some links hope they'll help – Rafay Sep 06 '11 at 11:05