www.webdeveloper.com
Results 1 to 8 of 8

Thread: Javascript function and PHP Function

  1. #1
    Join Date
    May 2014
    Posts
    7

    Post Javascript function and PHP Function

    Hi,
    I want that onsubmit, the system will fire the javascript first to validate the form and after getting OK result then it will call PHP function to connect/receive data from database

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,530
    So....stick an onsubmit event into the form tag and have it call the JS function, right?

    And don't forget that malicious users can easily bypass your JavaScript, so you still have to do any necessary validation on the server side, too.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2014
    Posts
    7
    I want the system to call the javascript first, and then PHP function... please give some example code too

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,530
    HTML Code:
    <form action="your_php_script.php" method="post" onsubmit="return yourJavascriptFunction();">
    Then define the JavaScript function to return true if validated successfully, otherwise return false.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2014
    Posts
    7
    now, how to go for the second step "calling PHP function" very after the javascript comes true

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,530
    Code:
    <form action="your_php_script.php" method="post" onsubmit="return yourJavascriptFunction();">
    Unless you want some sort of AJAX implementation, in which case your JavaScript function would use an AJAX request to a PHP file that would execute the desired function and return whatever it is the JS function needs as the response.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    May 2014
    Posts
    7
    ok, i have got this clue from the net, but it is also not working... pls check
    Code:
                echo "<script type = \"javascript\">
                var x = document.forms['frmlogin']['txtuser'].value;
                if (x ==null || x==\"\") {
                    alert (\"Missing username !\");
                    return false;
                    exit;
                </script>";

  8. #8
    Quote Originally Posted by NogDog View Post
    And don't forget that malicious users can easily bypass your JavaScript, so you still have to do any necessary validation on the server side, too.
    Which is why one should also make the page work securely without JavaScript first, since any data from user-side is suspect and should be re-validated on the server too.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles