ani
ani

Reputation: 109

Calling PHP function on HTML onClick() event?

I know there is one answer out on stackoverflow, but that is related to form submission.

Mine is different implementation. I need to call a function, which registers a variable in $_SESSION[], and then redirects to another page(html/php). Now I don't want to use AJAX. Please help me with this.

Upvotes: 0

Views: 2985

Answers (3)

Gene Parmesan
Gene Parmesan

Reputation: 102

If you are passing this with javascript to the php you could try

parameters.something = x;
parameters.somethingelse = y;
parameters.somethingelse1 = z;
$.post("class_handler", parameters)

and then your PHP needs to have a class_handler that will react when POST occurs.

Upvotes: 0

Quentin
Quentin

Reputation: 944320

Use a form with a submit button.

Store whatever you need to pass in hidden inputs (or use non-hidden inputs if it is supposed to be user configurable).

When the form is submitted, sanity check the data, set any session data you want, then redirect.

You don't need to use a JavaScript click event. The default behaviour of a submit button will handle everything you've asked for (that the client could contribute).

Upvotes: 1

Tom G
Tom G

Reputation: 3660

That is not possible. Php is executed on the server before the html is served. All php functions have been evaluated to HTML at the time onClick occurs.

You can do a form and set action="someFile.php" and use $_GET['someVaiable'] or $_POST['someVariable']

Upvotes: 2

Related Questions