View Full Version : Call php function when button is pressed using javascript...

11-24-2007, 06:14 PM
Hey guys I haven't used javascript for like 7-8 months... I quickly check W3 schools, but they don't have something like this...

I want to call a php function

function checkLanguage(){
$words = array("uh","oh","i","can't","list","these","words");
$lower = strtolower($Word);
$length = strlen($lower);
echo str_replace($words,"Yep its a bad word...",$lower);

Now i want to call this function and make it so that it appears on the page or something like that... thanks!

11-25-2007, 10:11 PM
You can use Ajax if you must make PHP occur live.

The only way to get a server side language to execute is to reserve the page.

You can refresh, or use Ajax to make Javascript refresh and react to the output.

With a form, it's often best to have a (nonsecure) method using just Javascript on the form, then return errors after checking (securely) serverside. No matter how great your client side check (even using Ajax) is, it can be bypassed, so the secure check is always going to be serverside.

The only time you'd need to do a client side check of something that requires PHP is if you needed to check something against the server, like, for example, if a username is already taken in the database. That's a perfect use for Ajax.

With the above example, I don't see the need for PHP there. Just make a similar Javascript function, then do a serious check after with PHP.

They CAN fake it, and if they want to, they will. So, the client side check is just a helping hand, showing them the right way, just like directions on the page that say "maximum 8 characters" or "no naughty words". Once they know what's going on, it should be clear enough, and your server side check can be precise and its response should be no surprise to them. If it fails because they didn't go with the rules, then it's up to them to fix it.

11-25-2007, 10:25 PM
k thanks

11-25-2007, 10:30 PM
By the way, $Word seems to be a random variable in the PHP script. I think you meant to have that be the input string, but there are no arguments in your function. Anyway, maybe just an example for the JS question.

11-25-2007, 11:00 PM
uh, the reason why its a random variable up there is because... uh... idk... good point... i'll check that out!

12-25-2007, 02:56 PM
what would a code example of this be (Ajax) to call the PHP functione very say.. 1 second?

Like to display a live check to see weather or not a user has a new message in their inbox ect..

12-25-2007, 07:31 PM
Well, you can create a function which makes the inbox-check request to the server and when the answer comes, you'll do something if it is necessary and use setTimeout to set a timer after 1000ms to call the above mentioned function.

08-12-2008, 05:27 PM
it's possible to call PHP method/function from javascript. there's simple way to do that. You can use jqSajax in http://satoewarna.com/jqsajax/ to call PHP method/function from javascript.