Reputation: 279
I'm trying to pass 2 variables through AJAX. But right now only one of them gets passed and saved in my database.
$image_id gets passed.
$uid doesn't get passed.
<div class="interaction"><a href="#" class="like" id="<?php echo $image_id ?>"
xml-id="<?php echo $uid ?>"><?php echo number_format($image_likes) ?></a>
</div>
<script type="text/javascript">
$(function() {
$(".like").click(function()
{
var id = $(this).attr("id");
var uid = $(this).attr("uid");
var dataString = 'id='+ id +'&uid=' + uid ;
var parent = $(this);
$.ajax({
type: "POST",
url: "like.php",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}
});
return false;
});
});
</script>
like.php:
if(($_POST['id']) && ($_POST['uid'])) {
$image_id=$_POST['id'];
$user_id=$_POST['uid'];
*some mysql query*
What am I'm missing here?
ps: I tried the SQL query with some dummy values and it works fine.
Upvotes: 0
Views: 110
Reputation: 2125
You could change your anchor:
<a href="#" class="like" id="<?php echo $image_id ?>" xml-id="<?php echo $user_id ?>">
To:
<a href="#" class="like" id="<?php echo $image_id ?>" data-uid="<?php echo $user_id ?>">
And then grab the value for user_id like:
var uid = $(this).data("uid");
You can use the network tab of the web inspector in Chrome to see what parameters are actually getting passed to your server.
Upvotes: 3
Reputation: 2443
use the below code to pass multiple parameters in ajaxrequest
parameters: {parametername1:value1,parametername2:value2},
then get them in php
if(($_POST['parametername1']) && ($_POST['parametername2'])) {
$image_id=$_POST['parametername1'];
$user_id=$_POST['parametername2'];
*some mysql query*
Upvotes: 2