0

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>
Lluis Bernabeu
  • 83
  • 3
  • 11
  • see http://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery/11736771#11736771 and http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – Arrok Feb 17 '15 at 08:16
  • @LuisBernabeu i won't solve your problem but why are you trying to force that form and pass it to login? You are sending username and password to backend already, can't you do authentication there, and return on success that user is already authenticated? Or nvm i've just saw that you are trying to post to another domain... – szapio Feb 17 '15 at 08:17

3 Answers3

0

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;
        });
    });
luis.ap.uyen
  • 1,182
  • 1
  • 10
  • 23
  • Are you using something like Chrome Developer Tools or Firebug? Then check the Network tab to check whether it is actually submitting something. In case it is, go to the Sources/Script tab and add a breakpoint at the first line in the success function, and inspect the result – luis.ap.uyen Feb 17 '15 at 09:05
0

checkout the result whether its returning "si" if its correct try

changing code from $("form").attr('action', 'http://xzc.tv/login'); $("form").submit(); to $("#formoid").attr('action', 'http://xzc.tv/login'); $("#form").submit();

try using id of form

siddhesh
  • 1,325
  • 1
  • 10
  • 16
0

To submit try the following

 $("form#formoid").submit();

$("form#formoid")[0].submit();

 $("form#formoid").submit(function(e) {});
Asif
  • 347
  • 2
  • 9