if JavaScript on submit using JS if JavaScript off submit using standard submit

Discussion in 'JavaScript' started by lionking, Jun 7, 2009.

  1. #1
    Hello Everybody

    I need help to making code

    I have form and I want to send data to php page by Ajax but if JavaScript enable submit Form using JavaScript code if JavaScript disable submit form using standard submit form

    And i have tried making this code but I failed to do so and This is the code created by


    HTML
    
    <form action="page.php" method="post" onsubmit="return login()">
                User Name 
                <input type="text" name="user" id="user" />
                <br/>Password
                <input type="password" name="pass" id="pass" />
                <br/><br/>
                <input type="submit" name="login" id="login" value="Login" onclick="" />
            </form>
            <br /><br />
            <div id="result"></div>
    
    Code (markup):
    JavaScript / Ajax
    
     function login(){
        var user = document.getElementById('user').value;
        var pass = document.getElementById('pass').value;
        
        Http.onreadystatechange = function(){
            if (Http.readyState == 4) {
                document.getElementById('result').innerHTML = Http.responseText;
            }
            else {
                document.getElementById('result').innerHTML = "Loading";
            }
        }
        url = "user=" + user + "&pass=" + pass;
        Http.open("post", "page.php", true);
        Http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        Http.send(url);
    }
     
    Code (markup):
    PHP
    
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    
    
    if($user == "ahmed" && $pass == "123"){
        echo "Right";
    }
    else{
        echo "Wrong";
    }
    
    PHP:


    Please Help Me
     
    lionking, Jun 7, 2009 IP
  2. lionking

    lionking Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Thank You Mr Unni krishnan For Help

    I think there are easy other ways. Used ( return True or False ), I want this way

    Thank you again
     
    lionking, Jun 8, 2009 IP
  3. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #3
    Hope this helps.

    "http://www.15seconds.com/issue/030303.htm"
     
    Unni krishnan, Jun 8, 2009 IP
  4. lionking

    lionking Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you Unni krishnan

    There is no benefit from this link
     
    lionking, Jun 8, 2009 IP
  5. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    MMJ, Jun 8, 2009 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    you must return false at the end of the login function or the form will submit anyway, if this doesn't work try calling the login function from the onclick event of the submit button (once again you will still need to return false).
     
    camjohnson95, Jun 9, 2009 IP
  7. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    If javascript is not enabled the script will not run and the form will submit.
     
    camjohnson95, Jun 9, 2009 IP