Question:
Calling a php function with the submit button in an HTML form?
anonymous
2011-04-05 12:27:50 UTC
I have an HTML form that contains a submit button. I want to use the OnClick to trigger a php function that is in a different file.

The HTML form is just a form that the user will fill out with various information. The information will then be used to retrieve data from the database. The data queried from the database will then build a graph (PHP File/Function does this).

...should I use AJAX, Javascript, etc?
Three answers:
David Harris
2011-04-06 02:17:47 UTC
AJAX, Yes.



Or you can just do this:

if(!isset($_POST['field'])) {

//your code here

} else {

echo "









";

}
anonymous
2011-04-05 19:32:45 UTC
Answer #1: PHP operates on the server side. Javascript operates on the client side.



Assume your web site is hosted in Chicago but your users come from all over the world like London, New York, and Paris. You write a file that is HTML mixed with some PHP. Then someone in London requests to see your web site. The first thing that happens is that the web server in Chicago reads your file from its hard drive. Then the web server looks at the PHP code and executes on the Chicago machine it which results in pure HTML. Finally, the Chicago machine sends the pure HTML (minus the PHP) to the user's machine in London.



When the user in London gets the web page, there is no PHP in it so there's no way for the web page to trigger those PHP commands - they don't exist in the pure HTML file that they receive.



You want to include some Javascript in the file. The HTML page that the user in London receives can have actions on buttons that display words when clicked. Since you are interested in displaying words when buttons are clicked, Javascript is probably the best language for you to be using.



Answer #2: For more advanced programmers, Ajax might be the right solution.



You can include Ajax code in your HTML file (the "j" in Ajax stands for Javascript so you're really just including Javascript). When you want the button click to effect some action on the Chicago web server, you can use Ajax so that the button click on the user's machine in London will send a message to the Chicago server that it should "do something".



But if that "something" is to print "blah blah blah" on the user's screen, then it wouldn't make sense to use Ajax because Ajax is going to invoke some action on the Chicago machine.



Therefore, simple Javascript (without Ajax) is probably the right tool for you to invoke an action on the user's machine - unless you have some need for the button click to make something happen on the web server.
anonymous
2011-04-05 19:31:54 UTC
For OnClick use javascript.

Or just set the button as part of a link.



This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...