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;
            });
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决