/    Sign up×
Community /Pin to ProfileBookmark

calling functions

How can I call a PHP function in a <input> tag (in the onClick=…. part)?

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@mitconMar 15.2005 — You can't. Once the page is sent to the browser PHP processing is complete until the page gets re-"submitted".

If you want to do the processing on the client side you are going to have to use JavaScript or some sort of a Java applet.

What, exactly are you looking to accomplish? There might be an easier, less costly solution than resubmitting the page for PHP processing.
Copy linkTweet thisAlerts:
@phpnoviceMar 15.2005 — You can't -- at least, not using anything that is cross-browser <soapbox>nor using anything acceptable to the maggots that make standards and accessibility the number one priority over absolutely every other consideration.</soapbox>

Thus, cross-browser, there are only two ways to call a PHP function... #1 submit the form, or #2 use a link instead of a button.
Copy linkTweet thisAlerts:
@gebezisauthorMar 15.2005 — The exact situation is:

I have a button which stops the voice in the page an I have to make it change to another button when the user clicks on it. The new button has to dispalay a small window with some text when the visitor clicks on it.

In another words I have to make a button with 2 different functions and appearances.
Copy linkTweet thisAlerts:
@phpnoviceMar 15.2005 — JavaScript can do all of that -- unless you specifically need information from the server-side (in which case, your server-side code could build it into a JavaScript data table when the page is first loaded). You'll not get PHP to dynamically do it (as previously explained why -- unless you want to hear about an IE-only solution that allows JavaScript to call server-side functions).
Copy linkTweet thisAlerts:
@mitconMar 15.2005 — Reading between the lines in phpnovice's 2 replies (ok, not so between the lines on the first reply <g>), I would agree...an IE only solution is not the way to go. The risk with JavaScript is that the user may have it disabled. The cost of PHP is another round trip to the server. I'm sure there's a time and place for each of them; your challenge is to decide which one works for you.

Could you maybe work with multiple button images in an Anchor (<a...) tag? Swapping images would probably be trivial. For that matter, changing the procedure associated with an action (onclick, etc.) may be simple as well...I would have to investigate...have you tried it Mr. Novice?

Bottom line...refresh your memory or take some time to learn the DOM (Document Object Model) and how JS can interact with it. It's really powerful and fun. I learn something new almost every time I venture down that path, it's worth the effort. Look for the Anchor and IMG objects in the DOM. There will be properties and methods associated with each.
Copy linkTweet thisAlerts:
@phpnoviceMar 16.2005 — [i]Originally posted by mitcon [/i]

[B]Could you maybe work with multiple button images in an Anchor (<a...) tag? Swapping images would probably be trivial. For that matter, changing the procedure associated with an action (onclick, etc.) may be simple as well...I would have to investigate...have you tried it Mr. Novice?[/B][/QUOTE]

Yes, I have done that. It is not hard at all. However, it is also not really required. A single event handler can determine the state of the invoking object and perform two or more different sets of code as a result. As for determining the state, e.g., the displayed text on the face of a text button can be changed and tested to see which text is currently displayed. An image link or button can also be changed and tested to see which image is currently displayed. These would be triggers to determine which parts of the code to execute.

To change the event handler is as simple as:
<i>
</i>&lt;script language="JavaScript" type="text/javascript"&gt;
&lt;!--//
function oneFunction() {
}
function twoFunction() {
}
function ctlFunction() {
if (some condition) {
obj.onclick = oneFunction;
} else {
obj.onclick = twoFunction;
}
}
//--&gt;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@mitconMar 16.2005 — Nice, clean solution!
Copy linkTweet thisAlerts:
@JonaMar 16.2005 — [i]Originally posted by phpnovice [/i]

[B]JavaScript can do all of that -- unless you specifically need information from the server-side (in which case, your server-side code could build it into a JavaScript data table when the page is first loaded). You'll not get PHP to dynamically do it (as previously explained why -- unless you want to hear about an IE-only solution that allows JavaScript to call server-side functions). [/B][/QUOTE]


[font=trebuchet ms]That isn't true at all! Have you heard of Ajax? The only browser you'll have issues with is Opera, but there are plenty of ways to remedy that issue. ?[/font]
Copy linkTweet thisAlerts:
@phpnoviceMar 16.2005 — [i]Originally posted by Jona [/i]

[B]That isn't true at all![/B][/QUOTE]

A bit ambiguous there, Jona. Pray tell, what isn't true?
Copy linkTweet thisAlerts:
@JonaMar 16.2005 — [font=trebuchet ms]JavaScript can send requests to the server in most browsers. The W3C's DOM Level III candidate for reading and saving hasn't even become a recommendation yet, so browsers decided to implement the idea themselves. The only browser that hasn't implemented it is Opera.[/font]
Copy linkTweet thisAlerts:
@phpnoviceMar 16.2005 — [i]Originally posted by Jona [/i]

[B]JavaScript can send requests to the server in most browsers.[/B][/QUOTE]

Now, I'm not talking about just sending. I'm talking about two-way communication in an event handler, for example. If this has been implemented in most browsers, I'd like to see what such code looks like. Thanks.
Copy linkTweet thisAlerts:
@JonaMar 16.2005 — [i]Originally posted by phpnovice [/i]

[B]Now, I'm not talking about just sending. I'm talking about two-way communication in an event handler, for example. If this has been implemented in most browsers, I'd like to see what such code looks like. Thanks. [/B][/QUOTE]


[font=trebuchet ms]Yes, I'm talking about two-way communication, too. You can send and retrieve data from the server and even execute server-side scripts without ever reloading the page. This is all via the proprietary [url=http://developer.apple.com/internet/webcontent/xmlhttpreq.html]XMLHttpRequest[/url] module. See: [url=http://www.xml.com/pub/a/2005/02/09/xml-http-request.html]Very Dynamic Web Interfaces[/url] by Drew McLellan; [url=http://www.modernmethod.com/sajax/]SAJAX toolkit[/url].[/font]
Copy linkTweet thisAlerts:
@phpnoviceMar 16.2005 — Yes, this is what I was talking about for IE:
<i>
</i>var req = new ActiveXObject("Microsoft.XMLHTTP");

I did not know that Mozilla had implemented this:
<i>
</i>var req = new XMLHttpRequest();

Thanks. ?
Copy linkTweet thisAlerts:
@JonaMar 16.2005 — [font=trebuchet ms]Glad to help. ? [/font]
Copy linkTweet thisAlerts:
@mitconMar 16.2005 — Likewise...thanks Jona!

I've got a couple of good candidates for the technique. Now, if I only had MORE time <g>.


You guys are good!
×

Success!

Help @gebezis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...