gpt4 book ai didi

php - jQuery JSON 无法正确处理 PHP 脚本

转载 作者:太空宇宙 更新时间:2023-11-03 11:12:01 25 4
gpt4 key购买 nike

出于某种原因,我无法让我的 JSON 脚本正确处理 if...else 语句。它会在 100% 的时间内处理 else 语句,并跳过 if 部分,即使值匹配也是如此。但是,当由静态 Html 表单调用时,PHP 脚本可以完美处理。有任何想法吗? jQuery 脚本如下:

$("#action_button").click(function() {
var username = $("#username").val();
var password = $("#password").val();
var dataString = '&username=' + username + '&password=' + password;
if(username=='' || password=='') {
$('#success').fadeOut(400).hide();
$('#error').fadeOut(400).show();
} else {
$.ajax({
type: "POST",
dataType: "JSON",
url: "processing/logsig.php",
data: dataString,
json: {session_state: true},
success: function(data){
if(data.session_state == true) { // true means user is logged in.
$("#main1").fadeOut(400);
} else if(data.session_state == false) { // false means user is being registered.
$("#main1").hide();
$('#main1').load('views/dashboard.php').fadeIn(400);
}
}
});
}
});

PHP 脚本:

<?php
header('Content-type:application/json');
session_start();
include("enc.php");
mysqlcon();

$email = mysql_real_escape_string(strip_tags($_POST["username"]));
$password = sha1($_POST["password"]);
$sql = "SELECT * FROM users WHERE username = '{$email}' AND password = '{$password}'";
$result = mysql_query($sql); // or exit("ERROR: " . mysql_error() . "<br>IN QUERY: " . $sql);

if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
$_SESSION["userid"] = $row['user_pid'];
$json1 = json_encode(array('session_state' => true));
echo $json1;
} else {
$userid_generator = uniqid(rand(), false);
mysql_query("INSERT INTO users (user_pid, email, password, datetime_registered, is_leader) VALUES ('$userid_generator', '{$email}', '{$password}', NOW(), 'no')");
$id = mysql_insert_id();
$leaders = mysql_query("SELECT * FROM users WHERE is_leader LIKE '%yes%'");
while($rows = mysql_fetch_array($leaders)) {
if ($rows['is_leader'] == 'yes') {
$leader_id = $rows['user_pid'];
mysql_query("INSERT IGNORE INTO friends (node1id, node2id, friends_since, friend_type)
VALUES('$leader_id', '$userid_generator', NOW(), 'full')");
}
$_SESSION["userid"] = $userid_generator;
}
$json2 = json_encode(array('session_state' => false));
echo $json2;
}
?>

最佳答案

在 firebug 中查看您的站点,POST 参数未正确发送。我看到了

password    undefined
username undefined

经进一步检查,您的输入似乎都没有 ID 属性,也未被找到

var username = $("#username").val();
var password = $("#password").val();

这与 JSON 或 PHP 无关。请麻烦执行基本的调试步骤。如果需要,请在每一行上添加 alert()

关于php - jQuery JSON 无法正确处理 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7860879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com