0

While using jquery $.ajax or $.post, to submit a value (say uid {user id}) how can I define a callback function to retrieve multiple values from the php script and retrieve values from my mysql table say(user details, corresponding to the uid) and store them in vars, using the default datatype:datatype???

Can anyone please show me both the jquery and php scripts please. And yes the database is runninga mySQL 5.5.24 and PHP version is 5.3

2
  • Your question is quite broad - it might be better to break it down into getting the data to the server, running the query to get the data you wish to return then getting the data back to the client. If you already know how to do some of this, you can not ask about that step. Commented Jun 17, 2013 at 6:45
  • See i can do php and mysql, i can throw a query to my database and get values from my table about the users, but i dont know what is how to slice and dice the jquery to get those values from my php script. Say they are stored as $username, $address ..... in my php Commented Jun 17, 2013 at 6:56

6 Answers 6

2
instead of using $.ajax you can also use this...

var id=$("#id").val();
 $.getJSON('script.php', {id:id}, function(json) {
                        var id=json.id;
                        var name=json.name;
                        var email=json.email;

        });
            }


in your php scrip..    
<?php 
    mysql_connect($host,$dbuser,$dbpass);
    mysql_select_db($db_name);
    $id=$_GET['id'];
    $query="select * from tbl_name where id='$id'";
    $rs=mysql_query($query);
    $row=mysql_fetch_assoc($rs);
    $id=$row['id'];
    $name=$row['name'];
    $email=$row['email'];
    $data=array("id"=>$id,"name"=>$name,"email"=>$email);
    echo json_encode($data);
    ?>
Sign up to request clarification or add additional context in comments.

4 Comments

thanx Saqlain that helped a lot.
dear Jay P. Sonar you must approve my answer..:(
Sorry im new here, i dont have enough rep to upvote it, i will do wheni have enough rep :(
approving best answer is important beacuse it helps other. just click on the {tick} mark... :)
0

The best way to do this would be to send it back to javascript as a json object.... so for your user example

 // assuming post containing a username, using pseudo code for db access

$row = $db->fetchAssoc("select * from users where username = '".dbEscape($_POST["username"]."'");
header("Content Type: application/json");
echo json_encode($row);

Comments

0

ajax code will be like follows

$.ajax({
 type: "POST",
 url: "your_page.php",
 data: { param1: "val1", param2: "val2" }
 }).done(function( data) {
        // do your callback operation
});

get values from your_page.php like below

$_POST["param1"] , $_POST["param2"]

if you want to know more then click here

I think this will solve your problem.

4 Comments

so what you are saying is that data{param1.... actually are the php variables that my php script has, right????
Hey that was what i was working with, can yo also explain it the php , please?
what you want to know? do you know ajax? data: { param1: "val1", param2: "val2" } - this section is for sending information to ajax page
how do i capture the values returned by my php script through the ajax callback, thats what i want to know and also if there is something in php that is necessary to send back the variables to the ajax request. Like @saqlain786 above has used json_encode above. Can you explain that without the json think?
0
<script type="text/javascript">
$.ajax({ // ajax call starts
          url: 'serverside.php', // JQuery loads serverside.php
          data: 'button=' + $(this).val(), // Send value of the clicked button
          dataType: 'json', // Choosing a JSON datatype
          success: function(data) // Variable data contains the data we get from serverside
          {

          }
      });


</script>
dataType: json..so jason will return multiple values. from you php script 
echo json_encode($array_of_val);

2 Comments

canyou exp[lain the php side also please, in correspondence with my user example provided above
<?php mysql_connect($host,$dbuser,$dbpass); mysql_select_db($db_name); $id=$_POST['id']; $query="select * from tbl_name where id='$id'"; $rs=mysql_query($query); $row=mysql_fetch_assoc($rs); $id=$row['id']; $name=$row['name']; $email=$row['email']; $data=array("id"=>$id,"name"=>$name,"foo"=>$email); echo json_encode($data); ?>
0
$.ajax({
 type: "POST",
 dataType:"html",
 url: "ajax_page.php",
 data:"params1="$params1"&params2="+$params2,
 }).done(function(value) {
        // write your callback operation
});

you can get your data on ajax_page.php using an post method like

$_POST['params1']; and $_POST['params2'];

4 Comments

so what you are saying is that data{param1.... actually are the php variables that my php script has, right????
Hey that was what i was working with, can yo also explain it the php , please?
No it's not an PHP variables it's an script variables.it's getting from the DOM element.you can get by following $("Your DOM Element").val();
how do i capture the values returned by my php script through the ajax callback, thats what i want to know and also if there is something in php that is necessary to send back the variables to the ajax request. Like @saqlain786 above has used json_encode above. Can you explain that without the json think?
0
$query= select statement
$items=array();
while ($row = mysql_fetch_object($rs)) {
  array_push($items, $row);
}
$result["rows"] = $items;
echo json_encode($result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.