Page 1 of 1
How to call a php function from a html button?
Posted: Thu Jun 26, 2003 3:25 pm
by slipstream
here is my button:
echo ' <td><input type="submit" value="Delete Entry"></td>';
I just need to call a php function called delete() when it is pressed. How can I do this and remain in the same script?
Posted: Thu Jun 26, 2003 4:02 pm
by daven
HTML is client-side, while PHP is server-side. Therefore, you cannot invoke a PHP function from an HTML form. The only way to achieve the effect you want is to have the form pointing to PHP_SELF, and then run the function.
That being said, below is some example code
PHP Portion
Code: Select all
<?php
function delete(){
// do stuff here
}
if(isset($_POST['invoke_delete'])){
delete(); // run the function
header("Location: ".$_SERVER['PHP_SELF']); // re-route to self, prevents people from hitting "refresh" and running delete() again
}
?>
HTML portion
Code: Select all
<form name="nameit" action="<?$_SERVERї'PHP_SELF']?>" method="post">
<input type="submit" name="invoke_delete" value="Delete Entry">
</form>
Posted: Thu Jun 26, 2003 4:12 pm
by m3rajk
you don't need the name for the form unless you use javascript... i think...
in any case, i's say have the form start:
Code: Select all
<form action="<?php echo $_SERVER[PHP_SELF]; ?>">
if that's the only thing inthe forum then have a hidden variable so it looks like...
Code: Select all
<?php
/* some php */ ?><form action="<?php echo $_SERVER[PHP_SELF]; ?>"><input type="hidden" name="function" value="delete"><input type="submit"></form>
then when it's recalled it'll have the url of the form ?function=delete
so the /* some php */ might actually have two displays...
one asking if you wanna delete the item, one saying you have.
and start the some phph part with $function=$_GET['function'];
Posted: Fri Jun 27, 2003 10:03 am
by slipstream
I tried that but as you see by the code, it has to be in a loop, as it stands now nothing happens.
Code: Select all
<?php
$file = file("dockets/".$cboSel.".txt");
$totHours = 0;
echo '<FONT FACE="Arial" SIZE="-1">';
echo '<font size="+2"><i>Mettle - View All Entries</i></font>';
echo '<br/>';
echo $cboSel;
echo '<br/>';
echo "________________________________________";
echo '<br/>';
echo '<br/>';
echo "<table name=t1 cellpadding=3 cellspacing=5>";
if(isset($_POST['invoke_delete'])){
delete(); // run the function
header("Location: ".$_SERVER['PHP_SELF']); // re-route to self,
}
$i=0;
$info=array();
foreach($file as $line){
$info[$i]=array();
$info[$i]=explode("|",$line);
$i++;
}
echo '<form name="nameit" action="<?$_SERVER['PHP_SELF']?>" method="post">';
for($j=0;$j<$i;$j++){
echo " <tr>\n";
echo " <td>" . $info[$j][0] . "</td>";
echo " <td>" . $info[$j][1] . "</td>";
echo " <td>" . $info[$j][2] . "</td>";
echo " <td>" . $info[$j][3] . "</td>";
echo ' <td><input type="submit" name="invoke_delete" value="Delete Entry"></td>';
echo " </tr>\n";
$totHours += $info[$j][2];
}
echo "</table>\n";
echo "________________________________________";
echo '<br/>';
echo '<br/>';
echo 'Total Hours Worked : '.$totHours;
function delete(){
echo 'delete function';
}
echo '</font>';
echo '</form>';
?>
mod_edit: bbcode enabled for this post
Posted: Mon Jun 30, 2003 9:51 am
by slipstream
Can anyone help me on this?
Posted: Mon Jun 30, 2003 10:48 am
by volka
take a closer look at this line of code
Code: Select all
<?php
...
echo '<form name="nameit" action="<?$_SERVER['PHP_SELF']?>" method="post">';
...
?>
I used
Code: Select all
tags instead of [quote] to make the issue more visible.
If you can use [url=http://php.net/echo]echo[/url] then you're already within a php-block and it's not necessary to open another one (in fact it's illegal). So this leads to
Code: Select all
<?php
...
echo '<form name="nameit" action="$_SERVER['PHP_SELF']" method="post">';
...
?>
still not looking good. php will throw a parse error here. You started a single-quoted string and php will take everything until the next unescaped single quote as content of the literal -in this case
<form name="nameit" action="$_SERVER[' - then trying to parse the next statement it will fail on recognizing
PHP_SELF as a valid statement.
Also in a single quoted literal no variable substituion will take place, so the output would always include
$_SERVER['PHP_SELF'], not the value of this variable.
Code: Select all
echo '<form name="nameit" action="', $_SERVER['PHP_SELF'], '" method="post">';
is one solution for this.
Next you have several table rows each containing a button named
invoke_delete
If one of these is pressed a new request is sent to the server. But your script cannot determine from which row this request has been initiated.
All parameters are passed as
key=value
So in your case regardless of which button has been pressed the key/value pair
invoke_delete=Delete%20Entry will be transmitted.
Somehow you have to add the information which row is meant.
Like value="Delete Entry #1", value="Delete Entry #2" and so on and parsing the this value.
Or by creating multiple forms each having a hidden field containing the id
Or ...
Or ...

Posted: Mon Jun 30, 2003 10:57 am
by slipstream
why can't I just jump out and into the delete function, then use my value $j to know what array element to remove, I then remove it, re-write the file. No?
Posted: Wed Jul 02, 2003 9:54 am
by slipstream
I am still having problems with this.
I am not trying to get someone to do it all, it's just that this is too advanced for my level right now.