I am new to PHP but I need to create a button on a simple page and have its event handler. Any clue?
-
1My advice, really, is to take a step back and start with very simple tutorials on PHP. Not because you aren't smart enough to do this, but because you are TOO smart. You already know a different way, and because it is already wrapped in a framework, a better way. Going way down to super basics in PHP will help you to 'get' the side of it you are missing. Because you have a fundamental misunderstanding about what PHP does. Which totally isn't your fault. :)DampeS8N– DampeS8N2010-12-01 12:49:46 +00:00Commented Dec 1, 2010 at 12:49
-
@DampeS8N thought you may enjoy this (question/comment/answer used in tech talk): youtube.com/watch?v=m3nb_Qj3mRA#t=12m28sstevec– stevec2020-08-25 01:37:42 +00:00Commented Aug 25, 2020 at 1:37
-
1@stevec Oh wow, I'm famous. HahaDampeS8N– DampeS8N2021-02-15 02:14:45 +00:00Commented Feb 15, 2021 at 2:14
3 Answers
In the OnClick event of the button, insert JavaScript code that points to the PHP script which you want to run when the button is clicked.
<input type="button" value="Say Hi!" onclick="location='test.php'" />
2 Comments
PHP doesn't work like .Net. So what you are talking about it actually not directly possible.
What you will need to do is create the raw markup for your form. At minimum a form tag and your button. Set the form's action to a php script on your server, such as "button_action.php" and have it accept, via GET or POST, whatever data you wanted to send. (or if you are just talking about enacting some action, you don't need to check anything)
Again, this isn't .Net, so there is no postback, and you will not come back to the page. If you want to do that, you will have to redirect back from your other script.
Comments
Ahmad Farid, how say Geoffrey Van Wyk, you can use the event "onclick" of an input element (button, for example).
Maybe AJAX is your solution (not reload all page). A simple example of 'click' and run a script (maybe CGI, or PHP) it's here: http://www.degraeve.com/reference/simple-ajax-example.php
Easy & useful :)
PD: Sorry for my english ;(