Spring Posted August 1, 2011 Share Posted August 1, 2011 I want to UPDATE the database once a button is pressed, how do I do this? I've tried, but I'm new with Ajax and I'm having trouble with the Data param. What do I need to do with this $.post function? earn.html $(document).ready(function() { $('#pika').click(function() { $.post('earn.php', { pika:pika }, function(data) { alert(data); }); return false; }); }); Here's my HTML earn.html <div id = "wraper" > <form action = "" method = "POST" id = "form"> <input type = "submit" id = "pika" name = "pika" value = "" /> </form> </div> And here's what I want to do. earn.php if( isset($_POST['pika']) ){ $sql = "INSERT INTO phpbb_user_pets (pet_id, owner_id, clicks, name, nickname, level, frozen, atti) VALUES(1,$user_id, 0, 'Pikachu', 'Default', 0, 0, '" . $attitudes[$random_atti] . "')"; $db->sql_query($sql); $template->assign_var('GRATS','You have earned your first pet!'); } Quote Link to comment Share on other sites More sharing options...
trq Posted August 2, 2011 Share Posted August 2, 2011 You need to send the actual data. $(document).ready(function() { $('#pika').click(function() { $.post('earn.php', { pika:$('pika') }, function(data) { alert(data); }); return false; }); }); Quote Link to comment Share on other sites More sharing options...
Spring Posted August 2, 2011 Author Share Posted August 2, 2011 You need to send the actual data. $(document).ready(function() { $('#pika').click(function() { $.post('earn.php', { pika:$('pika') }, function(data) { alert(data); }); return false; }); }); If it's not to troubling could you explain to me how this works? pika:$('pika') I know it's JSON, but am I basically saying that I want to set the value 'pika' to $('pika')? So my PHP would still look the same correct? I couldn't find any tutorials on this that was understandable for me. Could you explain to me what's going on? I don't need a whole run down, just a summary of what everything is doing, I'd appreciate it!! It would help me understand. Also, when I run this , my page uses too much data, any more tips? <?php define('IN_PHPBB', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './'; $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); // Start session management $user->session_begin(); $auth->acl($user->data); $user->setup(); //logged on? if ($user->data['user_id'] == ANONYMOUS){ login_box('', $user->lang['LOGIN']); } //User vars $username = $user->data['username']; $user_id = $user->data['user_id']; //Does the user have an egg? $sql = "SELECT * FROM phpbb_user_pets WHERE owner_id = $user_id"; $result = $db->sql_query($sql); //if($pet = $db->sql_fetchrow($result)){ // trigger_error($user->lang['ERR_EGG']); // $db->sql_freeresult($result); //} $attitudes = array('Angry', 'Happy', 'Grumpy', 'Estatic', 'Loving', 'Herp', 'Selfish', 'Emo'); $random_atti = array_rand($attitudes, 1); if( isset($_POST['pika']) ){ //$sql = "INSERT INTO phpbb_user_pets (pet_id, owner_id, clicks, name, nickname, level, frozen, atti) // VALUES(1,$user_id, 0, 'Pikachu', 'Default', 0, 0, '" . $attitudes[$random_atti] . "')"; //$db->sql_query($sql); $template->assign_var('GRATS','You have earned your first pet!'); } page_header('Earn your egg'); $template->set_filenames(array( 'body' => 'earn.html', )); make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx")); page_footer(); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted August 2, 2011 Share Posted August 2, 2011 Actually, my mistake, you should be setting it to $('#pika').val(). $('#pika') is a jQuery selector, nothing to do with json. You can read about them here: http://api.jquery.com/category/selectors/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.