Forum Moderators: coopster

Message Too Old, No Replies

onclick Update hit=hit+1

         

NeRu

10:02 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



how can write the code that when you click an image it will update table.

i have written the code but i could not have a clue working onclick function with sql update function.

for example here is my simple banner script

<?php
$colname_bannertop = "-1";
mysql_select_db($db);
$query_bannertop = "SELECT * FROM banners WHERE bannerCat = 2 ORDER BY RAND(bannerID) LIMIT 1";
$bannertop = mysql_query($query_bannertop) or die(mysql_error());
$row_bannertop = mysql_fetch_assoc($bannertop);
$totalRows_bannertop = mysql_num_rows($bannertop);
$updateHit = "UPDATE banners SET bannerHit = bannerHit+1 WHERE bannerID =".$_GET['bannerID'];
?>
<form id="bannerHit" name="bannerHit" method="post" action="">
<div align="center">
<a href="<?php echo $row_bannertop['bannerLink'];?>"><img src="<?php echo $row_bannertop['bannerImg'];?>" alt="<?php echo $row_bannertop['bannerAlt'];?>" border="0"/></a>
</div></form>
<?php
mysql_free_result($bannertop);
?>

StupidScript

11:20 pm on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome, NeRu.

In order to register the hit, you will need to (a) target the banner link to open in a new window and (b) do the update after the click, when the 'counting' page is requested. You should use hidden form elements to pass the info back to the server for PHP to act on.

<? ...

if($_POST["bannerID"]) {

[i]do the update[/i]

and

<form ...>

<input type="hidden" name="bannerID" value="<?$row_bannertop['id']?>" />

[i]the rest of the form[/i]

If you still want to send the visitor away from your site when they click a banner, then also include a hidden

$row_bannertop["bannerLink"]
form field, and when the form is submitted:

$thisLink=$_POST["bannerLink"];

header("Location: $thisLink");

to redirect them after the update.

newbie2006

12:02 am on Jan 27, 2006 (gmt 0)

10+ Year Member



Folks,
with full my respect, what for in the code message # 1, the form tags?
StupidScript, what for you explaine about the hidden fields, if the form is not submit by anything?
there is need at least to add to the tag <a>, 'onClick="javascript:document.bannerHit.submit(); return false;"'
in this case there is sense to add hidden firld, as StupidScript recommended and setup the redirects.
NeRu, I do not understand what do you mean "...a clue working onclick function with sql update function..."

StupidScript

12:12 am on Jan 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not a terrible thing to have a graphic click activate a form, so I don't think it is odd to include that, although, now that you mention it, the image would need to be a form element, like <input type="image"... that would act as a submit button.

Ahh. Rats. You're right, newbie2006. The form is useless.

The way to go would be to add attributes to the URI the banner ad is linked to, send the visitor back to this same page where the PHP could get the dynamic attributes and do the counter update, then re-direct to the banner ad's link destination.

No form, no hidden elements necessary. No Javascript.

<sigh> Too much typing, today. Good catch. ;)

StupidScript

12:32 am on Jan 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



NeRu, I mean like:

<div align="center">

<a href="<?php echo $PHP_SELF."?bannerLink=."$row_bannertop['bannerLink']."&bannerId=".$row_bannertop["bannerId"]?>"><img src="<?php echo $row_bannertop['bannerImg'];?>" alt="<?php echo $row_bannertop['bannerAlt'];?>" border="0"/></a>

</div>

Test for

$_GET["bannerId"]
and (1) do the update using the id and (2) do the redirect using the link.

NeRu

8:55 am on Jan 27, 2006 (gmt 0)

10+ Year Member



thanks for help, i solved the problem.

here is my solution

first, since my banner script using more than once, i made an hitupdater page.

<?php require_once('config.php);?>
<?php
mysql_select_db($db);
$query_updateBanner = "UPDATE banners SET bannerHit = bannerHit+1 WHERE bannerID =".$_GET['bannerID'];
$updateBanner = mysql_query($query_updateBanner);
header("Location: index.php");
?>

than linked this hit updater to the banner by a form and image input as

<?php
$colname_rs_bannertop = "-1";
mysql_select_db($db);
$query_bannertop = "SELECT * FROM banners WHERE bannerCat = 2 ORDER BY RAND(bannerID) LIMIT 1";
$bannertop = mysql_query($query_bannertop) or die(mysql_error());
$row_bannertop = mysql_fetch_assoc($bannertop);
$totalRows_bannertop = mysql_num_rows($bannertop);
?>
<form id="bannerHit" name="bannerHit" method="post" action="hitupdater.php?bannerID=<?php echo $row_bannertop['bannerID'];?>">
<div align="center">
<a href="<?php echo $row_bannertop['bannerLink'];?>" onClick="javascript:document.bannerHit.submit(); return false;"><input type="image" src="<?php echo $row_bannertop['bannerImg'];?>" alt="<?php echo $row_bannertop['bannerAlt'];?>" border="0"/>
</a></div>
</form>
<?php
mysql_free_result($bannertop);
?>