Page 1 of 1

How to call a php function within a hyperlink call

Posted: Mon Jun 04, 2007 9:26 pm
by desb01
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I try to do the following but without any success.

/* code start here */

Code: Select all

echo '<table>';
while ($row = mysql_fetch_row($result))
{
    echo '<tr><td>' . $row[2] . '</td><td>' . $row[1] . '</td><td><a href=\" ' . delete_record($row[1], $row[2]) . ' \">delete</a></td></tr>';
}
echo '</table>';
/* code end here */

Where delete_record is a php function that will delete the record in the SQL table.

So this will generate a table where the last column is only the term "delete" shown where the user can click the delete link to delete the current record in the row.

How do I do that ???

Thank you in advance for your answer.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jun 04, 2007 9:29 pm
by feyd
It's not possible like that. All PHP code is performed well before the browser receives data most often. You will need to make a link to a script with the necessary information needed to perform your task.

Re: How to call a php function within a hyperlink call

Posted: Tue Jun 05, 2007 8:03 am
by superdezign
desb01 wrote:

Code: Select all

echo '<table>';
while ($row = mysql_fetch_row($result))
{
    echo '<tr><td>' . $row[2] . '</td><td>' . $row[1] . '</td><td><a href=" ' . delete_record($row[1], $row[2]) . ' ">delete</a></td></tr>';
}
echo '</table>';
You're definitely thinking in terms of JavaScript right there.
PHP is server-side... Links don't affect the scripts, but you can use links to give variables form one script to the next. For example:

Code: Select all

<?php
if(isset($_GET['action']))
{
    switch($_GET['action'])
    {
        case 'delete': delete_record();
    }
}
?>
<a href="user.php?action=delete">Delete</a>

Posted: Tue Jun 05, 2007 11:02 am
by RobertGonzalez
The most common way (and the way taught in most tutorials) is to use the row ID from the database as a query string var then pass it back to the calling page and use that pass back as your trigger to delete.

Code: Select all

<?php
if (isset($_GET['item'])) {
  // Check, validate and ensure integrity of the value here
  // Then assign and use
  $delete = $_GET['id'];
  $sql = "DELETE FROM `table` WHERE `id` = $delete";
  // Continue processing
}

// The rest of your code that selects and displays
// with links to basename(__FILE__) . '?item=' . $row['id']
?>