最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Jquery, php ajax post 500 Internal Server Error returns - Stack Overflow

matteradmin9PV0评论

I have some problem with jquery ajax & php. I'm not good in php, but I have to create a login page with php.

The promblem is, when I try to get some information from the server I get server error insted of the value.

Here is my php code (if someone know better method to create login on server side, please correct me):

<?php
//$link = mysql_connect('localhost', 'root', '') or die('A kapcsolódás nem sikerült: '+mysql_error());
$link = mysql_connect("127.0.0.1", "qm", "supersecret") or die("Nem sikerült kapcsolódni az adatbázishoz!"+mysql_error()); 
mysql_query("SET NAMES UTF8");
mysql_select_db("qm", $link);
$uname = mysql_real_escape_string($_POST['name']);
$pass = mysql_real_escape_string($_POST['pass']);
$fetch = mysql_query("SELECT * FROM felhasznalok WHERE nev ='{$uname}' AND jelszo = Sha1('{$pass}')") or die(mysql_error());
if (mysql_num_rows($fetch) == 1) {
    $a = array();
    $['true'] = 'true';
    echo json_encode($a);
}else{
    $a = array();
    $['true'] = 'true';
    echo json_encode($a);
}
?>

And here is the code of the login on the client side:

function handleLogin(){
var _url = preUrl + "login.php";
var name = $("#loginForm #name").val();
var pass = $("#loginForm #pass").val();

$.ajax({
    type: 'POST',
    url: _url,
    data: {name: name, pass: pass},
    dataType: 'jsonp',
    beforeSend: function(){
        if((!name) || (!pass)){
            notify('error','Kérlek tölts ki minden adatot!');
            return false;
        }else{
            $("#loginForm #loginBtn").prop("disabled", true);  
        }
    },
    success: function(data){
    if(data[0].true == 'true'){
            window.localStorage["username"] = name;
            window.localStorage["password"] = pass;  
            $.mobile.changePage("#wall",{transition:"slide", reverse:false});
        }else{
            $('#loginForm #name').val("");
            $('#loginForm #pass').val("");
            notify('error','Hibás adatok!');
        }
    },
    error: function(err){
    //átírni ha a cordovajs be lesz szúrva!!!
    alert('Hiba: '+err.message);
}
});

$("#loginForm #loginBtn").prop("disabled", false);
return false;
}

I tried it out on different servers but nothing changed. I only get the error.

I have some problem with jquery ajax & php. I'm not good in php, but I have to create a login page with php.

The promblem is, when I try to get some information from the server I get server error insted of the value.

Here is my php code (if someone know better method to create login on server side, please correct me):

<?php
//$link = mysql_connect('localhost', 'root', '') or die('A kapcsolódás nem sikerült: '+mysql_error());
$link = mysql_connect("127.0.0.1", "qm", "supersecret") or die("Nem sikerült kapcsolódni az adatbázishoz!"+mysql_error()); 
mysql_query("SET NAMES UTF8");
mysql_select_db("qm", $link);
$uname = mysql_real_escape_string($_POST['name']);
$pass = mysql_real_escape_string($_POST['pass']);
$fetch = mysql_query("SELECT * FROM felhasznalok WHERE nev ='{$uname}' AND jelszo = Sha1('{$pass}')") or die(mysql_error());
if (mysql_num_rows($fetch) == 1) {
    $a = array();
    $['true'] = 'true';
    echo json_encode($a);
}else{
    $a = array();
    $['true'] = 'true';
    echo json_encode($a);
}
?>

And here is the code of the login on the client side:

function handleLogin(){
var _url = preUrl + "login.php";
var name = $("#loginForm #name").val();
var pass = $("#loginForm #pass").val();

$.ajax({
    type: 'POST',
    url: _url,
    data: {name: name, pass: pass},
    dataType: 'jsonp',
    beforeSend: function(){
        if((!name) || (!pass)){
            notify('error','Kérlek tölts ki minden adatot!');
            return false;
        }else{
            $("#loginForm #loginBtn").prop("disabled", true);  
        }
    },
    success: function(data){
    if(data[0].true == 'true'){
            window.localStorage["username"] = name;
            window.localStorage["password"] = pass;  
            $.mobile.changePage("#wall",{transition:"slide", reverse:false});
        }else{
            $('#loginForm #name').val("");
            $('#loginForm #pass').val("");
            notify('error','Hibás adatok!');
        }
    },
    error: function(err){
    //átírni ha a cordovajs be lesz szúrva!!!
    alert('Hiba: '+err.message);
}
});

$("#loginForm #loginBtn").prop("disabled", false);
return false;
}

I tried it out on different servers but nothing changed. I only get the error.

Share Improve this question edited Mar 11, 2023 at 7:29 Gábor asked Apr 10, 2013 at 14:13 GáborGábor 8194 gold badges12 silver badges18 bronze badges 1
  • It can be error in server php script – Victor Commented Apr 10, 2013 at 14:15
Add a ment  | 

3 Answers 3

Reset to default 2

You made a typo:

$['true'] = 'true';

Should probably be

$a['true'] = true;

(note the $ a )

Also note that whether your login would succeed or not, it will always fill that 'true value' with true. Looking at your Javascript, that's not what you want. Consider setting it on false in your else statement.

learn basic php: $['true'] = 'true'; is a flat-out syntax error. this is the most likely cause of your server 500 error.

Add string like that to see errors in your php script:

error_reporting(E_ALL);
ini_set('error_log', 'path_to_log_file');
ini_set('log_errors_max_len', 0);
ini_set('log_errors', true);

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far