Jump to content

running a php function from an html link


belti

Recommended Posts

I have a page which reads content from a database and display its result.

<?php

include ("databaseconnections/conx_stampsforsale.php");
  
$sql = mysql_query("SELECT country, description, price FROM stampsforsale");

if (!$sql)
  die ("Error Displaying Data. " . mysql_error());
else
{
  echo ("<table><tr>");  
  $count = 0;
  while ($row = mysql_fetch_array($sql))
  {
  $target_path = "stampsforsale/" . basename($row['country'] . ".jpg");
  echo ("<td><a href='" . $target_path . "' target='_blank'><img src='" . $target_path . "'width=150 height=100></a><br />" . $row['country'] . "<br />" . $row['description'] . "</br>" . $row['price'] . "<br /><br /></td>");
  $count++;
  if ($count % 3 == 0)
  {
    echo ("<tr>");
  }
  }
  echo ("</tr></table>");
 }
 // end of displaying data
  
?>

The above code works fine and it loads inside a the main section on my web page as follows: index.php?mC=stampsforexchange.php.

 

 

In order not to get a huge number of results I am planning to put the above code inside a function and then set the stampforexchange.php page with flags for each country so that when a particular country is clicked only stamps from that country are shown.  The following is the above code revised into a function.

<?php

include ("databaseconnections/conx_stampsforsale.php");

function getCountryStamps($country)
{
$sql = mysql_query("SELECT country, description, price FROM stampsforsale WHERE country = $country");

if (!$sql)
  die ("Error Displaying Data. " . mysql_error());
else
{
  echo ("<table><tr>");  
  $count = 0;
  while ($row = mysql_fetch_array($sql))
  {
  $target_path = "stampsforsale/" . basename($row['country'] . ".jpg");
  echo ("<td><a href='" . $target_path . "' target='_blank'><img src='" . $target_path . "'width=150 height=100></a><br />" . $row['country'] . "<br />" . $row['description'] . "</br>" . $row['price'] . "<br /><br /></td>");
  $count++;
  if ($count % 3 == 0)
  {
    echo ("<tr>");
  }
  }
  echo ("</tr></table>");
 }
 // end of displaying data    
}

  
?>

And this is the html part where I am trying to call the function:

 

  <td><a href="index.php?mC=getCountryStamps(Malta)"><img src="flags/malta.gif" width=100px height="67px" /><br />Malta</td></a>

 

However I am getting the following warning message:

 

Warning: include(getCountryStamps(Malta)): failed to open stream: No such file or directory in C:\xampp\htdocs\stamps\index.php on line 21

Warning: include(): Failed opening 'getCountryStamps(Malta)' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\stamps\index.php on line 21

 

Can someone please point in the right direction as I am stuck with this one?

 

Thank you in advance.

you include the file that contains the function.... not the function name

 

then you echo out the function

echo getCountryStamps('malta');

 

a table filled with hyperlinks isn't going to work within a hyperlink

<td><a href="index.php?mC=getCountryStamps(Malta)"><img src="flags/malta.gif" width=100px height="67px" /><br />Malta</td></a>

To better explain

<?php
//include('your_script_name.php');
 
//or just place it this same script
 
function getCountryStamps($country)
{
$sql = mysql_query("SELECT country, description, price FROM stampsforsale WHERE country = $country");

if (!$sql)
die ("Error Displaying Data. " . mysql_error());
else
{
echo ("<table><tr>");
$count = 0;
while ($row = mysql_fetch_array($sql))
{
$target_path = "stampsforsale/" . basename($row['country'] . ".jpg");
echo ("<td><a href='" . $target_path . "' target='_blank'><img src='" . $target_path . "'width=150 height=100></a><br />" . $row['country'] . "<br />" . $row['description'] . "</br>" . $row['price'] . "<br /><br /></td>");
$count++;
if ($count % 3 == 0)
{
echo ("<tr>");
}
}
echo ("</tr></table>");
}
// end of displaying data
}
 
//call on the function to display
echo getCountryStamps($country);

?>

Hi

 

Thank you for your reply as I managed to solve part of the problem. I have added the echo statement as suggested and it works. The problem is that I place the echo statement inside the function's file so at that point I do not know which flag is going to be clicked. Maybe I have the logic wrong. The following is what i am trying to achieve:

 

I have a page "stampsforexchange.php" which shows various flags. When I click on a flag it triggers the above function and I will pass the name of the flag as a parameter to filter the Where clause.

 

So far my only other idea is that I will create a page for each country, but this way I am going to have the same function in all pages which I would like to avoid.

 

Any thoughts how can I achieve this would be greatly appreciated.

index.php?mC=stampsforexchange.php

 

 

^^^ firstly, your page controller SHOULDN'T use the actual filename for the file to be included as this will let a hacker include any of your .php files and bypass security you may have in place. your page controller should just accept the 'module' name, mC=stampsforexchange, validate the value in the current user's context (should the current user be able to access that module/page), then include the appropriate .php file based on the module name.

 

then, to do what you are asking, you would have a second parameter in the url to specify what you want the stampsforexchange module to do - index.php?mC=stampsforexchange&country=malta. your control logic in the tampsforexchange.php code would take the $_GET['country'] value, validate it, and call the getCountryStamps($country) function with the $_GET['country'] value as the parameter to the function.

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.