views:

426

answers:

3

Hi all,

Im afraid its another noob question...lol. Being relatively new to javascript/ajax/jquery..programming ingeneral... I am fumbling where blind men drive chariots, so please go easy:). I am wanting to post 3 values to the database, 2 of which are results of a mysql query(Im not sure if I am doing this right, so feel free to correct me..lol..please). 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.

Can you pick my mistake?:P...lol

Any help is always appreciated.

Updated: Sunday, 5th April AEST

This question still remains unanswered. 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 isnt working.

Thanks all.

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>
+1  A: 

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
karim79
Hi there. Yes, I did spot that. I just havent updated it here, Ill do that right now.. thanks for letting me know. I am still returning empty vars though. The $user_id variable pertains to a PHP function asking if the users cookie exists. Ill add that too.
Lea
A: 

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...

Calvin
Hi there.. Thanks for your answer. Im not too sure about whether it is right wrong, but it hasnt affected any of the output sent to the vars by the php..the two vars still return empty. Thanks for your reply.
Lea
+1  A: 

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.

Lea