7

I am new to PHP but I need to create a button on a simple page and have its event handler. Any clue?

3
  • 1
    My 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. :) Commented 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=12m28s Commented Aug 25, 2020 at 1:37
  • 1
    @stevec Oh wow, I'm famous. Haha Commented Feb 15, 2021 at 2:14

3 Answers 3

6

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'" />
Sign up to request clarification or add additional context in comments.

2 Comments

Can you write this part of code please. The one that connects from javascript to PHP?
Nothing "connects" from Javascript to PHP. When the Javascript runs (on the client, ie. the user's browser), the PHP has already completed (on the server). The Javscript can fire off a new HTTP request (either a new page, or an Ajax request in the background) which a new PHP program (or at least a new instance of the program - it might actually be the same program if you write it that way) can process.
0

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

0

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 ;(

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.