Jump to content

image onclick event


widget

Recommended Posts

Hi, I am trying to setup a random event type situation where an image appears and if the user clicks on the image it is inserted into their inventory.

 

 

I have the part where it randomly calls the image working fine but not sure how to do the image clicking bit.

 

$randplay = rand(1,50);





if ($randplay == 27)
{
$randring = rand(1,5);

if($randring == 1)
{
$ring = 33331;
}
if($randring == 2)
{
$ring = 33332;
}
if($randring == 3)
{
$ring = 33333;
}
if($randring == 4)
{
$ring = 33334;
}
if($randring == 5)
{
$ring = 33335;
}

$code = mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$userid','$ring','1','$game')");

 

Any help is much appreciated.

 

After clicking, the user can either stay on the page they were on or be directed to another URL - either way would work fine.

 

Also this piece of code will be added using a include function.

Link to comment
Share on other sites

I dont mind the page reloading.

 

i know I can make the image a link that refers to another page with the insert the item into the inventory code but I was hoping to do it all on the one page.

 

I know theres a way to do it as a form and add...

 

if ($act=click)

{

 

perform the code here

 

}

 

but I dont know how to do it exactly, just know that its possible

Link to comment
Share on other sites

Indeed. Make the link refer to the current page. You'll also want to allow for anything that might already be in the URL. Something like this:

 

<?php
$page = $_SERVER['PHP_SELF'];
$act = "act=click&imageid=1";
if(empty($_SERVER['QUERY_STRING'])){
$page = $page.'?'.$act;
}else{
$page = $page.'?'.$_SERVER['QUERY_STRING'].'&'.$act;
}
echo '<a href="'.$page.'">Clicky Clicky</a>";
?>

 

You'd then use something like this to check to see if the action is click:

 

if(isset($_GET['act']) && $_GET['act']=='click'){
    //add to inventory
}

Link to comment
Share on other sites

ok this is what i have and here is the error

Parse error: syntax error, unexpected $end in /home/chicka/public_html/ring_event.php on line 51

 

<?php

$page = $_SERVER['PHP_SELF'];
$act = "act=click";

$randplay = rand(1,2);

if ($randplay == 2)
{
$randring = rand(1,5);

if($randring == 1)
{
$ring = 33331;
}
if($randring == 2)
{
$ring = 33332;
}
if($randring == 3)
{
$ring = 33333;
}
if($randring == 4)
{
$ring = 33334;
}
if($randring == 5)
{
$ring = 33335;
}

if(empty($_SERVER['QUERY_STRING']))
{
$page = $page.'?'.$act;
}

else{

$page = $page.'?'.$_SERVER['QUERY_STRING'].'&'.$act;
}

echo '<a href="'.$page.">Clicky Clicky</a>";

if(isset($_GET['act']) && $_GET['act']=='click')
{
  mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$userid','$ring','1','$game')");
}


?>

Link to comment
Share on other sites

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.