Results 1 to 8 of 8

Thread: Call php function when button is pressed using javascript...

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default Call php function when button is pressed using javascript...

    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
    PHP Code:
    <?php
    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!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    k thanks

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    uh, the reason why its a random variable up there is because... uh... idk... good point... i'll check that out!

  6. #6
    Join Date
    Dec 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    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..

  7. #7
    Join Date
    Dec 2007
    Location
    Ankara, Turkey
    Posts
    160
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default

    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.

  8. #8
    Join Date
    Aug 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •