BenMo Posted March 5, 2007 Share Posted March 5, 2007 Hello! I'm just starting to learn PHP, and I'm tinkering around with making some pages myself. I would like to make a page with two form buttons that can do two different things, say, clicking one creates a new entry in a database and clicking another lists all the data currently in the database (These actions could be anything for example's sake). I've gone through the w3schools PHP tutorial and have poked around on other PHP sites, but I can't seem to find how to do this. In javascript I can simply write a function() and then have the onclick of the button call the function, but no luck with PHP. How do I do this, and where can I go to find some tutorials that will give me a better understanding of having different buttons do different things? Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/41350-solved-different-actions-with-different-buttons/ Share on other sites More sharing options...
boo_lolly Posted March 5, 2007 Share Posted March 5, 2007 dig deeper into javascript. it will do what you are looking for and it can be integrated with php. Quote Link to comment https://forums.phpfreaks.com/topic/41350-solved-different-actions-with-different-buttons/#findComment-200357 Share on other sites More sharing options...
BenMo Posted March 5, 2007 Author Share Posted March 5, 2007 Hmm...ok, well, I tried to throw some PHP code in the middle of some javascript, but with no avail... <html> <head> <script type="text/javascript"> function displaymessage() { <?php echo "Hey"; ?> } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" > </form> </body> </html> ...is this the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/41350-solved-different-actions-with-different-buttons/#findComment-200361 Share on other sites More sharing options...
Psycho Posted March 5, 2007 Share Posted March 5, 2007 There are different ways to approach this. You could make each button a submit button with different values and then place them in different form tags. Then the page receiving the submission can test the value of $_POST['submit'] <html> <head></head> <body> <?php if (isset($_POST['submit'])) { if ($_POST['submit']=="Choice 1") { echo "Do something for choice 1"; } else { echo "Do something for choice 2"; } } ?> <br><br> <form name="test" action="<?=$PHP_SELF?>" method="POST"> <button type="submit" name="submit" value="Choice 1">Choice 1</button> </form> <form name="test" action="<?=$PHP_SELF?>" method="POST"> <button type="submit" name="submit" value="Choice 2">Choice 2</button> </form> </body> </html> Or, you could use two different submit buttons in the same form and use javascript tp populate a hidden field. <html> <head></head> <body> <?php if (isset($_POST['choice'])) { if ($_POST['choice']=="Choice 1") { echo "Do something for choice 1"; } else { echo "Do something for choice 2"; } } ?> <br><br> <form name="test" action="<?=$PHP_SELF?>" method="POST"> <input type="hidden" name="choice" id="choice"> <button type="submit" onclick="document.getElementById('choice').value='Choice 1'">Choice 1</button> <button type="submit" onclick="document.getElementById('choice').value='Choice 2'">Choice 2</button> </form> </body> </html> There are other ways as well. Quote Link to comment https://forums.phpfreaks.com/topic/41350-solved-different-actions-with-different-buttons/#findComment-200364 Share on other sites More sharing options...
BenMo Posted March 5, 2007 Author Share Posted March 5, 2007 That's great! I had such difficulty finding that in a tutorial anywhere. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/41350-solved-different-actions-with-different-buttons/#findComment-200365 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.