duanpin9531 2014-05-30 12:25
浏览 62

AJAX使用php变量返回写if语句(Jquery Mobile / Ajax / Php)

I'm trying to create a login script where the user will enter their username and password. This will be sent via ajax to a php script to check if the details are correct. If they are the user should then be able to select a site, which ive been trying to display like this $('#logonContainer').load("login_DisplaySelectSite.php");

My problem is that I cant figure out how to run this code if the session has started with a valid username. Heres my code so far, maybe someone can help me!

html/jquery mobile:

<div data-role="content">   
    <form id='login' onsubmit="return false;">      
                <div id='logonContainer' style="background-color: #F8F4FA; padding: 10px;  width:70%; margin: 0 auto; margin-top:15%;">
                    <input type="text" name='user' id='user' placeholder='Username'>
                    <input type="password" name='password' id='password' placeholder='Password'>
                    <input type="submit" id="submitLogin" name="login" value="Login" class='ui-btn ui-btn-c ui-btn-inline' data-theme='c' data-transition='pop'>
                </div>
            </form>
    <div id="resultLog"></div>
</div>

ajax:

$(function userLogin() {

        $("#submitLogin").click(function() {
            var theUser = $.trim($("#user").val());
            var thePassword = $.trim($("#password").val());

            if(theUser.length > 0)
            {
                $.ajax({
                  type: "POST",
                  url: "callajax.php",
                  data: ({usr: theUser, pwd: thePassword}),
                  cache: false,
                  dataType: "text",
                  success: onSuccess
                });
            }
        });

        $("#resultLog").ajaxError(function(event, request, settings, exception) {
          $("#resultLog").html("Error Calling: " + settings.url + "<br />HTTP Code: " + request.status);
        });

        function onSuccess(data)
        {
            $("#resultLog").html(data);


        }

This next bit is not working at all!! This is where I'm trying to display a radio button list to replace the username and passowrd fields. After this I guess I will use a similar function to select the site, again, dependant on the session being active - once all these checks have been done the user should be sent to the home page

        if(onSuccess(data) == 1) 
        {
            $('#logonContainer').load("login_DisplaySelectSite.php");

        } else {

        }

    });

php:

session_start();
if($_REQUEST['usr']=="admin" && $_REQUEST['pwd']=="1234"){

    $_SESSION['USERNAME'] = "admin";
    $_SESSION['SESSIONID']='123456';

    echo "Login CORRECT WOOOOOOOO!! </br>" ;
    //print json_encode($_SESSION);
    print $_SESSION['USERNAME'];
    $loggedon=1;
    echo json_encode( $loggedon);

}
else{
    echo "Incorrect Login :(" ;
    $loggedon=0;
    echo  json_encode($loggedon);
}

Thanks

  • 写回答

2条回答 默认 最新

  • dry0106 2014-05-30 12:34
    关注

    You are missing the signature of the success event. Just add the parameter to Success: success: onSuccess(data) and try to check, it should work fine.


    to check if the success, event is triggered its better to use some debugging tools or alerts/console messages/print to screen etc based on your development environment and IDE that you use, that will tell you if its working fine.

    thanks.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮