Page 1 of 1

how to call php function from an onclick.

Posted: Fri Nov 21, 2008 2:46 pm
by kuhlpal
Hi Everyone,

I have searched everywhere on web to find the answer of this question: how to call a php function from an onclick event. Right now the code looks like this:

Code: Select all

<form method="post" name="form" action="<? somefile.php ?>">
<input type="submit" value="print" name="print"  />
and it is working fine. But now I want to add one function which will update the database on clicking the submit button. Can anyone tell me what is the best to call that function. If not onclick then any other possible way.

Any help will be appreciated.

Re: how to call php function from an onclick.

Posted: Fri Nov 21, 2008 3:04 pm
by requinix
You're used to application programming I guess?

There is no "onclick" event, or anything of that nature, at least not in terms of PHP - JavaScript can do something. What you probably want to do is have the form submit to a file and have that file do whatever it is you want.
So basically, make somefile.php do the update.

Re: how to call php function from an onclick.

Posted: Fri Nov 21, 2008 6:03 pm
by califdon
The "action" parameter of a <FORM...> tag is the filename of the script that you want to send the form's data to. It bears no relationship to a click event. In your case, you would not use any PHP tags (and definitely never a bare <?). Your example didn't show any Form Inputs, so it would be pointless to use a Form at all. (Maybe you just omitted the rest of the Form, in which case disregard that comment.)

Now, click events are something altogether different. If you have no Form data that you need to send, just use the onClick event of an <INPUT Type='button' .../> without any Form. You would need to use Javascript to redirect to a new page, not just the name of a file.

If you do need to send data from a Form, it would look like this:

Code: Select all

<form method='post' action='somescript.php'>
Color: <input type='text' name='color' /> <br />
Quantity: <input type='text' name='qty' /> <br />
<input type='submit' value='Go' /> </form>