Jump to content

Recommended Posts

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?  :shrug:

 

 

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!');	

} 

Link to comment
https://forums.phpfreaks.com/topic/243520-new-and-have-no-idea-how-to-do-this/
Share on other sites

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();
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.