为什么当我使用JavaScript时将无法在此代码中工作

时间:2013-10-12 14:00:58

标签: javascript php jquery json

我厌倦了找错误。我使用javascript与jquery和php发送和接收数据,刷新浏览器。所有这些都在一个文件中。当我点击按钮什么也没发生。这是我文件的内容:

<?

if(isset($_POST['user']) && isset($_POST['pass'])){
    header('content-type: text/javascript');

    $resut = ($_POST['user'] == 'ahmad') ? true : false;
    $pass = ($_POST['pass'] == '1234') ? true : false;

    $output = array('user' => $resut,'pass'=>$pass);

    echo json_encode($output);

    exit();
}

?>

<html>
<head>
    <title>Test</title>
    <script src='http://code.jquery.com/jquery-1.10.2.js'></script>
    <script type='text/javascript'>

        $('#send').on('click',function(){
            var user = $('#user').val();
            var pass = $('#pass').val();

            $.post("<? echo basename(__FILE__); ?>",{user : user, pass : pass}, function(data){
                if(data.user == true && data.pass == true){
                    $('#content').text('hello '+ user + ' you are already loged in');
                } else {
                    $('#content').text('wrong try it again');
                }
            });
        });

    </script>
</head>
<body>


    <input type='text' id='user'>
    <input type='password' id='pass'>
    <input type='submit' id='send' value='login'>

    <div id='content'></div>


</body>

3 个答案:

答案 0 :(得分:1)

中包装JavaScript
 $(document).ready(function() {
  // The JavaScript or jQuery code goes here
});

答案 1 :(得分:0)

我想你错过了在第一行编写php

那是

<?php 
// Your php code
?>

答案 2 :(得分:0)

   $(document).ready(function(){
            $('#send').on('click',function(){
                var user = $('#user').val();
                var pass = $('#pass').val();

                $.post("<? echo basename(__FILE__); ?>",{user : user, pass : pass}, function(data){
                    if(data.user == true && data.pass == true){
                        $('#content').text('hello '+ user + ' you are already loged in');
                    } else {
                        $('#content').text('wrong try it again');
                    }
                });
            });
});