Jump to content

Recommended Posts

What I want to do is when someone clicks on a link, call a php function in another file which updates the database. This is what I've kinda come up with, doesn't work though. (Uses Jquery)

<?php
$result3 = array();
$result3[0] = $_POST['result0'];
$result3[1] = $_POST['result1'];
$result3[2] = $_POST['result2'];
$result3[3] = $_POST['result3'];
mysql_query("UPDATE adverts SET views = views + 1 WHERE id = $result3[0]")or die(mysql_error());
mysql_query("UPDATE adverts SET views = views + 1 WHERE id = $result3[1]")or die(mysql_error());
mysql_query("UPDATE adverts SET views = views + 1 WHERE id = $result3[2]")or die(mysql_error());
mysql_query("UPDATE adverts SET views = views + 1 WHERE id = $result3[3]")or die(mysql_error());
?>

 

Jquery:

$(document).ready(function(){
var result0 = <?php echo $result3[0]; ?>
var result1 = <?php echo $result3[1]; ?>
var result2 = <?php echo $result3[2]; ?>
var result3 = <?php echo $result3[3]; ?>
$('.advert').click(function(){
	$.post("functions.php", {result0: "result0", result1: "result1", result2: "result2", result3: "result3" })
})
});

 

HTML:

<a class="advert" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[0]]; ?>')">Banner #1</a>

Link to comment
https://forums.phpfreaks.com/topic/225460-onclick-post-to-php-form/
Share on other sites

Yeah I redesigned the code, this works kinda. Well it did in a test but in practical it won't, do you see the problem in this?

ajax.php

<?php
if(isset($_POST['functionid'])){
switch ($_POST['functionid']){
	case 1:
		ad1();
		break;
	case 2:
		ad2();
		break;
	case 3:
		ad3();
		break;
	case 4:
		ad4();
		break;
}

} 
function ad1(){
if(isset($_POST['result'])){
	$advert = $_POST['result'];
	mysql_query("UPDATE adverts SET views = views+1 WHERE id = $advert");
}
}
?>

Javascript.js:

$(document).ready(function() {

// Get advert numbers
var advert1 = <?php echo $cds[$result3[0]]; ?>
var advert2 = <?php echo $cds[$result3[1]]; ?>
var advert3 = <?php echo $cds[$result3[2]]; ?>
var advert4 = <?php echo $cds[$result3[3]]; ?>
	  
  	$("#advert1").click(function(){
	alert("Success")
  $.post("ajax.php?", 
  		{result: advert1, functionid: 1},
		function(data){alert("Success")})
  	});
$("#advert2").click(function(){
  $.post("ajax.php?", 
  		{result: advert2, functionid: 2})
  	});
$("#advert3").click(function(){
  $.post("ajax.php?", 
  		{result: advert3, functionid: 3})
  	});
$("#advert4").click(function(){
  $.post("ajax.php?", 
  		{result: advert4, functionid: 4})
  	});

});

 

HTML:

<a id="advert1" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[0]]; ?>')">Banner #1</a>
<a id="advert2" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[1]]; ?>')">Banner #2</a>
<a id="advert3" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[2]]; ?>')">Banner #3</a>
<a id="advert4" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[3]]; ?>')">Banner #4</a>

 

Fine.

include 'config.php';
$result = mysql_query("SELECT * FROM adverts") or die(mysql_error()); // Get Ads
$result2 = mysql_query("SELECT * FROM lil_urls WHERE id=$text") or die(mysql_error()); // Get User Link
$link = mysql_fetch_array($result2); // Array Convert User Link
$ads = array(); // array for ad id
$bds = array(); // array for ad url
$cds = array(); // array for the two aboved combined to set the index as the id

// Fill first 2 arrays
while($row = mysql_fetch_array($result)){
$ads[] = $row['id'];
$bds[] = $row['url'];
}
// Combine the 2 arrays to set the index as the id
$cds = array_combine($ads, $bds);


// Start Generating 4 random numbers to choose 4 random adds  
$result3 = array();
$numberCount = 4;
$thisCount   = 0;
while($thisCount<$numberCount) {
    $thisNumber = array_rand($cds);
    if(!in_array($thisNumber, $result3)) {
      $result3[$thisCount] = $thisNumber; 
 $thisCount++;
   }
  }  
// End generating. 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript" src="jquery/javascript.js"></script>
</head>
<body>
<pre>
<?php print_r($result3); print_r($cds); ?>
</pre>
<a id="advert1" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[0]]; ?>')">Banner #1</a>
<a id="advert2" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[1]]; ?>')">Banner #2</a>
<a id="advert3" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[2]]; ?>')">Banner #3</a>
<a id="advert4" href="<?php echo $link['url']; ?>" onclick="window.open('<?php echo $cds[$result3[3]]; ?>')">Banner #4</a>
</body>
</html>

Don't worry I got it working, was a couple of problems.

 

A) I wasn't connecting to my database at all in the ajax.php

B) I didn't use semicolons on the 4 javascript variables, which is strange because I thought javascript didn't need semicolens

C) Apparently having the code in another document like I did also stuffs it up, probably because of the php I used in it.

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.