gpt4 book ai didi

javascript - javascript-php json通信问题

转载 作者:行者123 更新时间:2023-12-01 01:34:14 25 4
gpt4 key购买 nike

我需要你们的帮助,因为我无法弄清楚我的 javascript 代码发生了什么。

我的 php 文件中有这些代码行:(我确信我的 PHP 文件工作正常)

PHP 文件:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<?php
header('Access-Control-Allow-Origin: *');
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("127.0.0.1", "root", "", "mysql3");
// Check connection
if($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}

$user_id =$_POST['user_id'];
$book_id =$_POST['book_id'];
$game_id =$_POST['game_id'];
$site_id =$_POST['site_id'];

//Attempt insert query execution
$query = "SELECT site_id FROM components WHERE user_id='$user_id' && book_id='$book_id' && game_id='$game_id' ORDER BY site_id DESC LIMIT 1";
$res = mysqli_query($link,$query);
$result = array();
$res = mysqli_query($link,$query) or die(mysqli_error($link));
while($row = mysqli_fetch_assoc($res)){
$result=$row['site_id'];
$myJSON = json_encode($result);
}
file_put_contents('data.json', $myJSON);//save my data json in file json
echo $myJSON;
//$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
//fwrite($myfile, $result);
//fclose($myfile);

// Close connection
mysqli_close($link);
?>

脚本:

<script>
function load3() {
var flag1 = true;
do{
var selection = window.prompt("Give the User Id:", "Type a number!");
if ( /^[0-9]+$/.test(selection)) {
flag1=false;
}
}while(flag1!=false);
$("#user_id").val(selection)

var flag2 = true;
do{
var selection2 = window.prompt("Give the Book Id:", "Type a number!");
if ( /^[0-9]+$/.test(selection2)) {
flag2=false;
}
}while(flag2!=false);
$("#book_id").val(selection2)

var flag3= true;
do{
var selection3 = window.prompt("Give the Game Id:", "Type a number!");
if ( /^[0-9]+$/.test(selection3)) {
flag3=false;
}
}while(flag3!=false);
$("#game_id").val(selection3)

var mydata =JSON.parse(data);
alert(mydata[0].name);//get name's value
}
</script>

不幸的是,我发现值“4”无法返回到我的脚本中的 js 函数。我做错了什么?

最佳答案

手动创建文件 .json(空) 在我的示例中是 data.json

你的html文件:

<!DOCTYPE html>
<html>
<head>
//call your file data.json
<script type="text/javascript" src="data.json"></script>

<script type="text/javascript">
//use your file data.json
var mydata =JSON.parse(data);
alert(mydata[0].name);//get name's value
</script>
</head>
<body>
</body>
</html>

php 文件:

<?php

//example data
$myArr = [];
$myArr['name'] = "John";
$myArr['age'] = 30;
$myArr['city'] = "New York";
/*
or object
$myArr = stdClass();
$myArr->name = "John";
$myArr->age = 30;
$myArr->city = "New York";
*/


$myJSON = json_encode($myArr);

file_put_contents('data.json', "data ="."'"."[".$myJSON."]';");//save my data json in file json
echo $myJSON;
?>

php 将数据放入 data.json 文件中,如下所示:data ='[{"name":"John","age":30,"city":"New York0"}]';

在这种情况下你可以使用之前编写的html文件。当您重用 php 文件时,它会重写文件 json。我希望这对您有所帮助...

关于javascript - javascript-php json通信问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52984703/

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