I have a problem with ajax, it does not connect to the file that I specify

0

I have a index.php file that identifies a variable for get and with that variable it looks for the corresponding file /index.php ? i = login

The index is in the main directory and the files of the pages are in the folder pages / .... php

This file contains the following:

function msg_info($status, $msg) {

 $stored = array("status" => $status, "msg" => $msg);

 echo json_encode($stored);exit();
}


if($_POST['a']=='submit'){

    msg_info(0,'probando');

}

<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript" src="./js/jquery-ui-1.9.1.custom.min.js">
<script type="text/javascript" src="./js/l2blockit.js"></script>

//fin php y comienza html y javascritp


    function login(id){

            httplocal = location.href;

            $.ajax({ data: $("#"+id).serialize(), type: 'POST', url: httplocal, dataType:"json",

                error: function(){

                    $("#msg").l2error("Error in connection to the server");

                },success: function(data){

                    if(data.status == 0){

                        $("#msg").l2error(data.msg);

                    }

                }

            });

        return false;

    }

//html

    <form method="POST" id="loginform" onsubmit="return login(this.id);" class="formulario">

            <input type="hidden" name="a" value="submit"/>

            <input type="text" class="formulario__input" name="username" />

            <input type="text" class="formulario__input" name="password" />

            <input type="submit" class="formulario__submit" value="Login"/>


            <div id='msg'>

        </div>

    </form>

It does not give me any answer and it does not give me an error in the php log file, I'm working locally.

I await your opinions.

    
asked by yunis martinez 14.05.2017 в 21:15
source

1 answer

0

Hello I tried the code and it gave me error in this part "$ (" # msg "). l2error (data.msg);" , try to put an isset to validate the variable that you receive per post,

I leave the code you use to prove if something is useful, greetings and luck!.

<?php

function msg_info($status, $msg) {

 $stored = array("status" => $status, "msg" => $msg);

 echo json_encode($stored);exit();
}


if(isset($_POST["a"]) && $_POST['a']=='submit'){

    msg_info(0,'probando');

}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
  function login(id){

            httplocal = location.href;

            $.ajax({ data: $("#"+id).serialize(), type: 'POST', url: httplocal, dataType:"json",

                error: function(){

                    $("#msg").l2error("Error in connection to the server");

                },success: function(data){

                    if(data.status == 0){

                        $("#msg").html(data.msg);

                    }

                }

            });

        return false;

    }


</script>


    <form method="POST" id="loginform" onsubmit="return login(this.id);" class="formulario">

            <input type="hidden" name="a" value="submit"/>

            <input type="text" class="formulario__input" name="username" />

            <input type="text" class="formulario__input" name="password" />

            <input type="submit" class="formulario__submit" value="Login"/>


            <div id='msg'>

        </div>

    </form>
    
answered by 14.05.2017 в 22:27