weixin_33695450 2015-02-17 08:10 采纳率: 0%
浏览 19

通过发布php ajax发送

Good morning, I want to send to an external site to my two variables by post are username and password and the page I create a cache in the browser when I go to another url already be logged in code what I do is check for through PHP if the data is correct or not, if they are correct returned if, but returns no, if they are correct what I want to do is force the form to send the submit the url, the problem is that everything works fine to the point the submit the form no longer do anything, can you help me?

Thank You

My code:

    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $("#submit").click(function () {

                var user = $("#user").val();
                var pass = $("#pass").val();
    // Returns successful data submission message when the entered information is stored in database.
                var dataString = 'user=' + user + '&pass=' + pass;
    // AJAX Code To Submit Form.
                $.ajax({
                    type: "POST",
                    url: "index.php",
                    data: dataString,
                    cache: false,
                    success: function (result) {
                        if (result == 'si') {
                            $("form").attr('action', 'http://xzc.tv/login');
                            $("form").submit();
                            //window.open('http://xzc.tv');
                            alert('entro if');
                            return true;
                        } else {
                            alert('entro else');
                        }
                    }
                });
                return false;
            });
        });
    </script>
</head>
<body>
    <form action="" method="post" id="formoid"> 

        <div><label>Usuario:</label><input name="user" placeholder="Usuario" type="text" id="user"></div> 
        <div><label>Contraseña:</label><input name="pass" placeholder="Contraseña" type="text" id="pass"></div> 
        <div><input name="enviar" type="submit" value="Enviar" id="submit"></div>
        <div><input name="btnPass" type="submit" value="¿Olvidaste tu contraseña?"></div>
    </form> 


</body>
  • 写回答

3条回答 默认 最新

  • weixin_33717117 2015-02-17 08:19
    关注

    Just declare the event parameter in the click handler, and then do event.preventDefault(). Doing so, the default submit action is ignored and your code will be executed instead:

         $(document).ready(function () {
            $("#submit").click(function (e) {
                e.preventDefault();
                var user = $("#user").val();
                var pass = $("#pass").val();
                // Returns successful data submission message when the entered information is stored in database.
                var dataString = 'user=' + user + '&pass=' + pass;
                // AJAX Code To Submit Form.
                $.ajax({
                    type: "POST",
                    url: "index.php",
                    data: dataString,
                    cache: false,
                    success: function (result) {
                        if (result == 'si') {
                            $("form").attr('action', 'http://xzc.tv/login');
                            $("form").submit();
                            //window.open('http://xzc.tv');
                            alert('entro if');
                            return true;
                        } else {
                            alert('entro else');
                        }
                    }
                });
                return false;
            });
        });
    
    评论

报告相同问题?