Jump to content

running a php function from an html link


belti
Go to solution Solved by mac_gyver,

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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);

?>
Edited by QuickOldCar
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Thank you very much for your tip regarding the page controller and also thank you for the suggestion how i should go about the country issue.  I will try to work it out as suggested.

 

Once again thanks.

Link to comment
Share on other sites

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.