美元。邮寄似乎行不通

时间:2022-08-23 13:08:13

There are a couple of alert() calls in the jQuery which are showing up, but the alert inside the $.post does not seems to respond. I already tested the PHP code and it works fine. I think it is my path, but I already check them before.

jQuery中出现了几个alert()调用,但$中出现了警报。post似乎没有回应。我已经测试了PHP代码,它运行良好。我想这是我的路,但我已经检查过了。

<form id="login-form" method="post" class="form-inline" target="_top">
    <input type="text" tabindex="2"  id="username" placeholder="Username" name="email" class="inputtext">
    <input type="password" tabindex="3" id="userpass" placeholder="Password" name="pass" class="inputtext">
    <button tabindex="4" id="loginButton">Login</button>
</form>
$(document).ready(function(e) {
    $('#loginButton').click(function(e) {
        alert("start function");
        $.post('../php/login.php', {
            'username': $('#username').val(),
            'userpass': $('#userpass').val()
        }, function(data){
            alert(data);
        });
        alert("end function");
    });
});
<?php    
    require('config.php');

    $queryStmt = 'SELECT user_first, user_last FROM users WHERE user_name=:sqlIdName AND user_password=:sqlPass';
    $queryPrepare = $dba_connect->prepare($queryStmt);
    $queryPrepare->execute(array(':sqlIdName'=>$_POST['username'],':sqlPass'=>md5($_POST['userpass'])));
    $queryResult = $queryPrepare->fetch(PDO::FETCH_ASSOC);
    $queryPrepare->closeCursor();

    var_dump($queryResult);

    if ($queryResult == false)
        return false;
    else 
        return true;  
?>

My file structure is this:

我的文件结构是:

美元。邮寄似乎行不通

3 个解决方案

#1


2  

The button is actually submitting the form normally as you have not given it a type="button" attribute, nor are you using preventDefault() on the click event itself.

按钮实际上是正常地提交表单,因为您没有给它一个type="button"属性,也没有在单击事件本身上使用preventDefault()。

Regardless of that, it is much better practice to hook to the submit event of the form, not the click of the button. Try this:

无论如何,更好的实践是将它与表单的提交事件挂钩,而不是单击按钮。试试这个:

$('#login-form').submit(function(e){
    e.preventDefault(); // stop standard form submission
    $.post('../php/login.php', {
        'username': $('#username').val(),
        'userpass': $('#userpass').val()
    }, function(data){
        console.log(data);
    });
});

You may also want to return something other than a boolean value from your PHP code, as it will just be turned in to a single string response. JSON would seem to be the best fit for your case, check out the json_encode function.

您可能还想从PHP代码中返回一个布尔值以外的值,因为它将被转换为单个字符串响应。JSON似乎最适合您的情况,请查看json_encode函数。

Finally, note that you should always use console.log for debugging as it does not coerce data types.

最后,请注意,您应该始终使用控制台。记录调试,因为它不强制数据类型。

#2


1  

try this .its not exact ans may be the solution for it.

试试这个,它的解可能不太精确。

echo string in php server side something like this

php服务器端的echo字符串类似这样。

if ($queryResult == false)
echo 'false'; 
else 
 echo 'true'; 

and then change your jquery to

然后将jquery改为

function(data){
    if(data=='true')
     {
      alert(data);
      }
      else
      {
     alert(data);
     }
      });

#3


0  

Try this:

试试这个:

$(document).ready(function(e) {
    $('#loginButton').click(function(e) {
        $.post('../php/login.php', {
            functions: 'userLogin',
            username: $('#username').val(),                    
            userpass: $('#userpass').val()
        }).done(function( data ) {
            alert("Data Loaded: " + data);
        });
    });
});

#1


2  

The button is actually submitting the form normally as you have not given it a type="button" attribute, nor are you using preventDefault() on the click event itself.

按钮实际上是正常地提交表单,因为您没有给它一个type="button"属性,也没有在单击事件本身上使用preventDefault()。

Regardless of that, it is much better practice to hook to the submit event of the form, not the click of the button. Try this:

无论如何,更好的实践是将它与表单的提交事件挂钩,而不是单击按钮。试试这个:

$('#login-form').submit(function(e){
    e.preventDefault(); // stop standard form submission
    $.post('../php/login.php', {
        'username': $('#username').val(),
        'userpass': $('#userpass').val()
    }, function(data){
        console.log(data);
    });
});

You may also want to return something other than a boolean value from your PHP code, as it will just be turned in to a single string response. JSON would seem to be the best fit for your case, check out the json_encode function.

您可能还想从PHP代码中返回一个布尔值以外的值,因为它将被转换为单个字符串响应。JSON似乎最适合您的情况,请查看json_encode函数。

Finally, note that you should always use console.log for debugging as it does not coerce data types.

最后,请注意,您应该始终使用控制台。记录调试,因为它不强制数据类型。

#2


1  

try this .its not exact ans may be the solution for it.

试试这个,它的解可能不太精确。

echo string in php server side something like this

php服务器端的echo字符串类似这样。

if ($queryResult == false)
echo 'false'; 
else 
 echo 'true'; 

and then change your jquery to

然后将jquery改为

function(data){
    if(data=='true')
     {
      alert(data);
      }
      else
      {
     alert(data);
     }
      });

#3


0  

Try this:

试试这个:

$(document).ready(function(e) {
    $('#loginButton').click(function(e) {
        $.post('../php/login.php', {
            functions: 'userLogin',
            username: $('#username').val(),                    
            userpass: $('#userpass').val()
        }).done(function( data ) {
            alert("Data Loaded: " + data);
        });
    });
});