Jump to content

php question


hikzero

Recommended Posts

Hello forum, im new so go easy on me.

 

In a html form, how do i set the action so that if i have 3 <input> buttons that call different php functions still work?

 

like I want 3 different buttons to be able to call upon each of the three different functions depending on which one is pressed. all php code is on the same page but i don't know what to put in the action attribute of my html form

 

Can someone give me an example

Link to comment
https://forums.phpfreaks.com/topic/193057-php-question/
Share on other sites

Be patient. Not everyone here visits on a daily basis, nor do they get paid for their help.

 

In response to your question, you can do something like this:

 


<form action="test.php" method="post">
<input type="submit" value="One" name="choice[]" />
<input type="submit" value="Two" name="choice[]" />
<input type="submit" value="Three" name="choice[]" />
</form>
<?php

if($_POST["choice"][0] == "One") {
echo "One clicked"; 
} elseif($_POST["choice"][0] == "Two") {
echo "Two clicked";
} elseif($_POST["choice"][0] == "Three") {
echo "Three clicked";
} elseif(empty($_POST)) {

} else {
echo "Uh oh, hacking attempt! lol";
}

?>

 

By adding [] to the end of an input's name, you turn it in to an array (akin to $blah[] = "Hello"; $blah[] = "World"; ) so you can add 100 buttons, all with the same name and echo out $_POST["blah"][0] to find out which one was clicked.

 

If you do the same with checkboxes or listboxes with multiple="multiple", you can find out exactly which boxes were ticked. For example:

 

<?php
for($i = 0; $i <= 100; $i++) {
echo "Was $i clicked?: " . $_POST["blah"][$i] . "<br />";
}
?>

 

Hope that helps! And remember that your answer might take a few days or even a few weeks to be answered. It's whoever stops by and knows the answer. If you're looking for immediate answers, try a company that does telephone support for PHP ;D

Link to comment
https://forums.phpfreaks.com/topic/193057-php-question/#findComment-1017848
Share on other sites

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.