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
https://forums.phpfreaks.com/topic/119017-image-onclick-event/
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
https://forums.phpfreaks.com/topic/119017-image-onclick-event/#findComment-612855
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
https://forums.phpfreaks.com/topic/119017-image-onclick-event/#findComment-612860
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
https://forums.phpfreaks.com/topic/119017-image-onclick-event/#findComment-612876
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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