how to call php function from an onclick.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kuhlpal
Forum Newbie
Posts: 10
Joined: Wed Oct 22, 2008 3:56 pm

how to call php function from an onclick.

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: how to call php function from an onclick.

Post 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.
Last edited by requinix on Fri Nov 21, 2008 6:52 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to call php function from an onclick.

Post 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>
Post Reply