Reputation: 934
Being relatively new to javascript, AJAX and jQuery, I want to post 3 values to the database, 2 of which are results of a mysql query. Looking at the source code, var userid
has the correct value of the users id. The second (var obid
), and third(var authoruserid
) values. The rows returned from the query are empty when I check the source code, and the values in firebug are just simply the name of the var. The data is also not being posted to the database.
Any help is always appreciated.
I really would be happy if someone could just tell me that I am doing the AJAX part right, and that AJAX can use SQL rows as data, then I can begin to look for other reasons why this isn't working.
Server side:
if( isset( $_POST['user_id'] ) && isset($_POST['ob_id'] ) && isset( $_POST['author_user_id']) ) {
$result = mysql_query("INSERT INTO ilike (ilike_user_id, ilike_object_id, ilike_author_user_id) VALUES (" . mysql_real_escape_string( $_POST['user_id'] ) . ", " . mysql_real_escape_string( $_POST['ob_id'] ) . ", " . mysql_real_escape_string( $_POST['author_user_id'] ). ")" );
echo $result ? 'Vote Succeeded' : 'Vote Failed: ' . mysql_error();
exit;
}
$status = $rows['status_id']; // done via a separate query
$aid = $rows['user_id']; // as above
$user_id = uid(); // User cookie check function
HTML:
<a href="javascript:;" onclick="updateScore(this)" class="blue">Vote</a>
Javascript:
<script type="text/javascript">
function updateScore( answer )
{
var userid = '<?php echo $user_id; ?>';
var obid = '<?php echo $status_id; ?>';
var authoruserid = '<?php echo $aid; ?>';
if ( confirm( "Are you sure?" ) )
{
$.post('index.php', {user_id: "userid", ob_id: "obid", author_user_id: "authoruserid"}, function(d)
{
alert('Vote Accepted: ' + d);
$(answer).after("<span>You Voted!</span>").remove();
});
}
}
</script>
Source code:
<script type="text/javascript">
function updateScore( answer )
{
var userid = '5';
var obid = '';
var authoruserid = '';
if ( confirm( "Are you sure?" ) )
{
$.post('index.php', {user_id: "userid", ob_id: "obid", author_user_id: "authoruserid"}, function(d)
{
alert('Vote Accepted: ' + d);
$(answer).after("<span>You Voted!</span>").remove();
});
}
}
</script>
Upvotes: 1
Views: 1488
Reputation: 934
Im need to go see the eye doctor. I over looked a curely bracket in my php code. Im so sorry to bother you all. I also got rid of the vars, and just echoed them in the properties list.
Cheers.
Upvotes: 1
Reputation: 4619
I'm pretty new to jQuery as well, and I haven't actually used its AJAX functionalities yet, so please pardon my ignorance. But are you sure you need quotes around userid, obid, and authoruserid in the key:value pairs sent as data parameters in the $.post function? Because I don't see quotes being used around variable names in the jQuery reference manual I have, nor in other code samples online. It seems to me that you would only use quotes around literal values, like if you were trying to actually post something like:
{user_id: "32", username: "Bob", message: "hello world!"}
Perhaps something like this might work better:
$.post('index.php', {user_id: userid, ob_id: obid, ...}, ...
But I might be totally wrong...
Upvotes: 0
Reputation: 342655
Where is the part of your code that assigns values from the DB to $user_id, $status_id
?
I can only see the assignment to $aid. Could that be your problem, nothing getting assigned to $user_id
and $status_id
before the page gets displayed?
On your server side code, you've got $status = $rows['user_status'];
, whereas on the page you are calling it $status_id
Replace the last part of the server code with this:
$status_id = $rows['user_status']; //changed $status to $status_id
$aid = $rows['user_id']; // this works
//You need an assignment to $user_id
$user_id = $rows['something'];//I'm guessing this assignment is missing
Upvotes: 1