简体   繁体   English

从html解析返回数据到ajax

[英]parse return data from html to ajax

I have some problem with the returned value of ajax. 我对ajax的返回值有一些疑问。

this is the ajax code: 这是ajax代码:

$(document).ready(function() {
    var request;
    $("#flog").submit(function(event) {
        if(request)
            request.abort();
        event.preventDefault();
        var form = $(this);
        var serializedData = form.serialize();
        var btnname = $('#log').attr('name');
        var btnval = $('#log').val();
        var btn = '&'+btnname+'='+btnval;
        serializedData += btn;      

        request = $.ajax({
            type: form.attr('method'),
            url: form.attr('action'),
            data: serializedData,
        });

        request.done(function(data, status, jdXHR) {
            alert(data);
        });

        request.fail(function(jdXHR, status, error) {

        });
    });
});

it takes data from a form and send it to another page. 它从表单获取数据并将其发送到另一个页面。

this is the second page: 这是第二页:

<?php include 'head.php'; ?>
    <?php
    if($_POST['login']) {

    session_regenerate_id(true);
    $con = mysqli_connect("localhost", "Alessandro", "ciao", "freetime")
        or die('Could not connect: ' . mysqli_error($con));

    $query = 'SELECT * FROM users WHERE username="' . $_POST['user'] . '"';
    $result = mysqli_query($con, $query) or die('Query failed: ' . mysqli_error($con));
    if(mysqli_num_rows($result) == 0) {
        mysqli_close($con);
        session_unset();
        session_destroy();
        $res = false;
        return $res;
    }

    $query = 'SELECT password FROM users WHERE username="' . $_POST['user'] . '"';
    $result = mysqli_query($con, $query) or die('Query failed: ' . mysqli_error($con));
    $line = mysqli_fetch_array($result, MYSQL_ASSOC);
    if(md5($_POST['password']) != $line['password']) {
        mysqli_close($con);
        session_unset();
        session_destroy();
        return false;
    }
?>
<?php include 'foot.php'; ?>

and in .done the returned data is all the html page. 在.done中,返回的数据全部为html页面。 How can I retrieve only a data, like $res? 我如何只检索数据,例如$ res? I tried with json_encode() but with no results. 我尝试了json_encode(),但没有结果。 If in the second page I delete the lines include 'head.php' and include 'foot.php' it works. 如果在第二页中删除了包括“ head.php”和“ foot.php”的行,则该行有效。 But I need that the secon page is html, too. 但是我也需要secon页面是html。 Somenone can help me? 有人可以帮助我吗?

Dont use the Data attribute from AJAX. 不要使用AJAX的Data属性。

Replace 更换

request.done(function(data, status, jdXHR) {
        alert(data);
    });

with

request.done(function(data, status, jdXHR) {
        alert(jdXHR.responseText);
    });

You could do it in a much simpler way. 您可以用一种更简单的方法来完成它。 In PHP store the result of the attempted login into a variable, for instance $result =0; 在PHP中,将尝试登录的结果存储到变量中,例如$ result = 0; to start with If the login is valid change it to 1 and return it to ajax by doing an echo at the end of your PHP file. 首先,如果登录名有效,则将其更改为1,然后通过在PHP文件末尾执行echo命令将其返回到ajax。 If you need other value returned such as the name you could add it to the variable with a separator such as || 如果需要返回其他值(例如名称),则可以使用||等分隔符将其添加到变量中。 for instance. 例如。

in javascript collect your return and go data = data.split('||'); 在javascript中收集您的退货并去data = data.split('||'); if (data[0] == 0){alert("Welcome back " + data[1]);}else{alert("wrong login...")} if(data [0] == 0){alert(“欢迎回来” + data [1]);} else {alert(“ wrong login ...”)}

Previous use is correct, you need to escape the user collected in your PHP script. 先前的使用是正确的,您需要转义收集在PHP脚本中的用户。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM