Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2013
    Location
    Calgary AB,Canada
    Posts
    55
    Thanks
    28
    Thanked 1 Time in 1 Post

    Server side validation.

    Hi,


    I am doing a validation on the server side (PHP) based on the input in the FORM which is in HTML, and trying to output the error in JavaScript, but its not showing the error in JavaScript via "alert" command. Please help me.

    HTML :
    Code:
    <form name="xxx" action="" method="post" enctype="multipart/form-data" onsubmit="return checkForm();">
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    JavaScript Function to validate:
    Code:
    function checkForm()
         {
             alert("Inside checkForm()");
             <?php
                        $strCount = count(explode('.',$_FILES['file']['name']));
                        echo $strCount;
             ?>
             alert (strCount);    
             if(strCount > 1){
                        alert('Invalid File Name...');
                        return false;
             }
    
         }
    Please help me.

    thanks,

    Isaac

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Don't use alert() in live code. That has been for debugging use only since Netscape 4 died. You should be displaying error messages within the page itself.

    Doing the error messages inside the HTML works for both JavaScript and PHP validation whereas alert just gives some visitors the opportunity to turn JavaScript off for your site.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You cannot combine JS and PHP validation together. PHP is a server side technology and JS is a client side one, so you cannot mix and match them together. PHP is capable of generating but not executing JS code, and JS isn't capable of executing PHP code. You can use AJAX if you would like though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Isaac Sureshkum (10-09-2013)

  • #4
    New Coder
    Join Date
    Jun 2013
    Location
    Calgary AB,Canada
    Posts
    55
    Thanks
    28
    Thanked 1 Time in 1 Post
    Thanks for the reply, but how to show the error messages ?

    I tried this:

    Code:
                    if(strCount > 1){
                        document.getElementById("xxx").innerHTML="Invalid File......";
                        return false;
                     }
    HTML:

    Code:
    <div>
        <p id="xxx">
         </p>
    </div>
    Thanks,

    Isaac

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Isaac Sureshkum View Post
    Thanks for the reply, but how to show the error messages ?
    Your example code is the simplest way to do it from JavaScript. You just need to make sure the JavaScript is attached at the bottom of the page so the id is attached to the DOM before you try to reference it.

    From PHP you'd insert the error message in the same place but by echoing it there if the error condition is satisfied when the web page is being output.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

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