Caro_deb
Caro_deb

Reputation: 279

Only one variable out of 2 gets passed through AJAX

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

Answers (3)

inteloid
inteloid

Reputation: 737

You don't have an attribute named "uid" in your markup.

Upvotes: 0

dinjas
dinjas

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

Shivam
Shivam

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

Related Questions