Calling a PHP Function on Button Click

Hello

I believe I finally have all my Custom PHP Functions in Working Order.

I have an image based button. When that is clicked, I want one or more of my custom functions to run.

I asked at another forum and a helpful member said I should do the following:

<a href="index.php?act=runfunctions"><img class="right" src="images/like.png" alt="A little green plus sign" title="I Like" /></a>

And then:

$act = isset($_GET['act']) ? protect($_GET['act']): false;

if ($act == "runfunctions") {

        recVisitor($jID, $ipaddress);
        upRating($jID);

}

This however is not working !! when I check the table in phpmyadmin, the data is still the same when in fact it should have changed due to my two custom made functions !

Where am I going wrong with this ?

I’d greatly appreciate any help.

Thank You.

$jID has no value… you need to pass that into the URL as well…

hmm how do I do that ? Sorry a n00b here.

By the way $jID does have a value, though it is defined above the code what I have pasted here !

But let’s try passing it into the URL > How do I do that please ?

echo your variables onto the page prior to a conditional statement such as if(){} or a ternary as you have used.

Try and get into the habit of using var_dump() too.

Thanks Cups, that is what I have done !! yet still it isn’t working :frowning: Thanks about the Var_Dump, it is indeed a useful function.

what is protect() doing?

Hi StarLion

Here it is:

function protect($input) {

	db_connect();

	mysql_real_escape_string($input);
	return $input;

}

Just protects the string from doing harm.

Shall I place all the content of my { functions.php } here ?

OK Here it is:

<?php
/*
 * The Function Below Connects to the Server and Selects the Necessary Database for the Jokemania Website
 * @return (Boolean) and a Resource
 *
 */

function db_connect() {

	// Omitted Private Information. But connects to Db successfully.
}

function protect($input) {

	db_connect();

	mysql_real_escape_string($input);
	return $input;

}

/*
 * The Function Below Checks whether the given Joke ID and IP Address of the Visitor are present within the
 * RATINGS Table inside our JOKES Database.
 * @param Joke ID and IP Address of Visitor.
 * @return Boolean
 *
 */

function ipCheckUp($jID, $ipaddress)  {

	db_connect(); // This function (defined above) gives us access to the Database. (note this is a function call, within a function)

	$query = "SELECT jokeid FROM ratings
                    WHERE
                    jokeid = %d AND ipaddress = '%s'"; // This is a MySQL Query that will grab the data (if it exists) from the RATINGS table.

	$result = @mysql_query(sprintf($query, $jID, mysql_escape_string($ipaddress)));
	
	return (bool)mysql_num_rows($result);

}

/*
 * The Function Below Records the Visitor's IP Address along with the Joke ID of the Joke which He or She
 * has decided to vote on (by clicking the Like button - Green Plus)
 * RATINGS Table inside our JOKES Database.
 * @param Joke ID and IP Address of Visitor.
 * @return void
 *
 */

function recVisitor($jID, $ipaddress) {

	db_connect(); // This function (defined above) gives us access to the Database. (note this is a function call, within a function)
	
	$query = "INSERT INTO ratings
                    SET
                    jokeid = %d, ipaddress = '%s'"; // This is a MySQL Query that will add the IP Address and Joke ID to the RATINGS table.

	mysql_query(sprintf($query, $jID, mysql_escape_string($ipaddress)));
}

/*
 * The Function Below Records the Visitor's IP Address along with the Joke ID of the Joke which He or She
 * has decided to vote on (by clicking the Like button - Green Plus)
 * RATINGS Table inside our JOKES Database.
 * @param Joke ID and IP Address of Visitor.
 * @return void
 *
 */

function upRating($jID) {

	db_connect(); // This function (defined above) gives us access to the Database. (note this is a function call, within a function)
	
	$query = "UPDATE jokes SET rating = rating + 1 WHERE jokeid = %d "; // This is a MySQL Query that will Increase the Rating by 1 in the JOKES table.
	
	mysql_query(sprintf($query, $jID));
}

$act = isset($_GET['act']) ? protect($_GET['act']): false;

if ($act == "runfunctions") {

        recVisitor($jID, $ipaddress);
        upRating($jID);

} ?>

Hopefully it’ll be easier to understand what’s going on with this :frowning:

okay… mysql_real_escape_string is a return function, not a void reference function. try making that $input = mysql… for starters :wink:

secondly… I dont see anywhere where $jID and $ipaddress are being defined…

Oh you mean like this:

$input = mysql_real_escape_string($input);

The $jID and $ipaddress are defined in the { index.php } file and that { functions.php } file is being included into the { index.php }.

Here is the bit from { index.php } that does the business.

<div id="jokesContainer">
				
				<!-- The PHP Loop -->
						
					<?php while ($row = mysql_fetch_array($result)) { ?>
					
					<?php	$jID = $row['jokeid']; // This is a variable to hold the Joke ID (jokeid).
							$ipaddress = $_SERVER['REMOTE_ADDR'];?>

						<div class="jokePost">
						
							<div class="actBar">
							
								<a href="#"><img src="images/report.png" alt="a little triangle with an exclamation mark" title="Report Joke" /></a>
								<a href="index.php?act=runfunctions"><img class="right" src="images/like.png" alt="A little green plus sign"
								
								title=" <?php if (ipCheckUp($jID, "$ipaddress")) {
									echo "Already Liked";
								} else {
									echo "I Like";
								}  ?> " /></a>
														
							</div>
							<?php echo $row['jbody'] . '</div>';
							
						 } /* End Joke Post */
					
				?><!-- End of PHP Loop -->
					
				
				</div><!-- End of Jokes Container -->

I hope all is clear. :frowning: I’ll be back in an hour or so … I have a Community Meeting to attend.

Thanks

Okay, but here’s the thing - the act lines you’re talking about arnt inside a function definition.
That means that as soon as you include that file, those lines get executed.
Is $jID and $ipaddress defined at THAT point? I think you might be needing to change your link to…

<a href=“index.php?act=runfunctions&jid=<?php echo $jID; ?>”>

and then having your act code something like…


$act = isset($_GET['act']) ? protect($_GET['act']): false; 
$jID = isset($_GET['jid']) ? protect($_GET['jid']): false;
if ($act == "runfunctions" && $jID !== false && !ipCheckUp($jID,$_SERVER['REMOTE_ADDR'])) { 
        recVisitor($jID, $_SERVER['REMOTE_ADDR']); 
        upRating($jID); 
}

Hmm I think I can understand the point your’re making.

You’re saying at THE POINT where I’m including the { functions.php } file, the { $jID & $ipaddress } are NOT YET defined. And that is why it isn’t working. Right ?

If I wanted to use a string variable called { $ipaddress } that stores the IP Address as a string and used that in my { act } code instead of the { $_SERVER[‘REMOTE_ADDR’] } … will I have to add another line to the { act } code and the anchor link code ?

Like the following perhaps ?

$act = isset($_GET['act']) ? protect($_GET['act']): false;
$jID = isset($_GET['jID']) ? protect($_GET['jID']): false;
$ipaddress = isset($_GET['ipaddress']) ? protect($_GET['ipaddress']): false;


if ($act == "runfunctions" && $jID !== false && $ipaddress !==false && !ipCheckUp($jID, $ipaddress)) {

        recVisitor($jID, $ipaddress);
        upRating($jID);

}

Or am I wrong because && only works for 2 ?