How to call a php function from a html button?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

How to call a php function from a html button?

Post 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?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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

&lt;form name="nameit" action="&lt;?$_SERVER&#1111;'PHP_SELF']?&gt;" method="post"&gt;
&lt;input type="submit" name="invoke_delete" value="Delete Entry"&gt;
&lt;/form&gt;
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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'];
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post 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
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

Can anyone help me on this?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ...
;)
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post 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?
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post 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.
Post Reply