1

I have a list of records which loop from mysql table, for each record i had assigned an onclick function in order to pass the data for mysql query use to avoid page refresh whenever it was call,

<a href='javascript:void(0)' onclick='PlayMV(\"".$rows["v_type"]."\",\"".$rows["v_id"]."\");'>Play</a>

below is passing values to jquery function:

<script type="text/javascript">
function PlayMV(data1, data2){
$.post("mtv.php", { var1: "data1", var2: "data2" },
    function(data){
    $('#result').html(data);
});
}
</script>

here comes the problem, the "$('#result').html(data);" was always returned me a whole page in source code instead of held only the values, what I want is only able to post 'data1' and 'data2' and assigned into a php variable and mysql query like below:

$var1 = data1;
$var2 = data2;
$q = mysql_query("SELECT * FROM table WHERE mvtype='".$var1."' AND mvid='".$var2."'");

how to use JSON to pass those data into mysql query to retrieve the final result, can anyone help?

Thanks very much.

0

2 Answers 2

2

your script mtv.php will recieve them in the $_POST global.

you should be able to get the params with $_POST['var1'], $_POST['var2']

Script should be:

<script type="text/javascript">
    function PlayMV(data1, data2){
        $.post("mtv.php", { var1: data1, var2: data2 },
            function(data){
                alert("Data Loaded: " + data);
        });
    }
</script>

You had quotes round the values.

EDIT

Oh, if you want some data BACK from php, you can return XML or JSON, simply alert(data) to see what IS being returned just now.

In mtv.php, use json_encode() and echo it, this will get returned as 'data' which you can then do something liek this:

Example return JSON: {'thing': 1234}

var myObject = $.parseJSON(data);    

alert(myObject.thing);

The alert would display 1234.

Sign up to request clarification or add additional context in comments.

2 Comments

I think he actually just set the variables to those strings for the sake of the example/test.
Nope... they are parameters of the function, check his Q you will see they are quoted... while his call is passing in alternative data $rows["v_type"] and $rows["v_id"].
1

How do you retrieve the data in php? You should be able to fetch it like so:

$var1 = $_POST['var1'];
$var2 = $_POST['var2'];

2 Comments

Hi, i can retrieve the data properly assigned in php variable, this is the hardest part for me to parse into mysql query, function(data){ $('#result').html(data); } please advise me for more json sample on how to use it.
Ah, your question did not exactly state that this was the issue :) Just put your results into a php array, then do a json_encode on that array and echo out the result. You can then use that result from javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.