connect_error); }-6ren">
gpt4 book ai didi

javascript - 如何在javascript中获取php echo结果

转载 作者:行者123 更新时间:2023-12-02 23:59:20 25 4
gpt4 key购买 nike

我的 php 文件位于从数据库中检索数据的服务器上。

<?php
$servername = "myHosting";
$username = "myUserName";
$password = "MyPassword";
$dbname = "myDbName";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, description FROM tableName;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row_number = 0;
while($row = $result->fetch_assoc()) {
$row_number++;
echo $_GET[$row_number. ";". $row["id"]. ";". $row["name"]. ";". $row["description"]. "<br>"];
}
} else {
echo "0 results";
}
$conn->close();
?>

不幸的是,我不知道如何使用 javascript 从 php 文件接收数据。我希望 JavaScript 中的脚本能够在浏览器的控制台中显示接收到的数据。

用javascript编写的脚本是我的浏览器扩展(tampermonkey)中的用户脚本,php文件位于我的服务器上。我尝试过使用ajax,不幸的是没有积极的结果。(php 脚本按预期工作)。

JS(不工作):

    $.ajax({
url: 'https://myserver.com/file.php',
type: 'POST',
success: function(response) {
console.log(response);
}
});

最佳答案

循环内的代码有点奇怪

$_GET[$row_number. ";". $row["id"]. ";". $row["name"]. ";". $row["description"]. "<br>"]

这表明有一个命名非常奇怪的查询字符串参数,我认为这不是我们想要的。

相反,也许可以尝试这样:

<?php

$servername = 'myHosting';
$username = 'myUserName';
$password = 'MyPassword';
$dbname = 'myDbName';
$conn = new mysqli($servername, $username, $password, $dbname);

if( $conn->connect_error ) {
die( 'Connection failed: ' . $conn->connect_error );
}

$sql = 'select `id`, `name`, `description` from `tablename`;';
$result = $conn->query($sql);

if( $result->num_rows > 0 ) {

$row_number = 0;

while( $row = $result->fetch_assoc() ) {
$row_number++;
/* print out row number and recordset details using a pre-defined format */
printf(
'%d;%d;%s;%s<br />',
$row_number,
$row['id'],
$row['name'],
$row['description']
);
}
} else {
echo '0 results';
}

$conn->close();
?>

一个完整的例子来说明你的ajax代码如何与数据库交互。示例顶部的 php 代码是模拟您的远程脚本 - 查询与您自己的查询或多或少相同,并且 javascript 仅略有修改...如果您要更改自己的 sql 查询,它应该去工作...

<?php

error_reporting( E_ALL );
ini_set( 'display_errors', 1 );

if( $_SERVER['REQUEST_METHOD']=='POST' ){
ob_clean();

/* emulate the remote script */
$dbport = 3306;
$dbhost = 'localhost';
$dbuser = 'root';
$dbpwd = 'xxx';
$dbname = 'xxx';

$db = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$sql= 'select `id`, `address` as `name`, `suburb` as `description` from `wishlist`';


$res=$db->query( $sql );
$row_number=0;

while( $row=$res->fetch_assoc() ){
$row_number++;

/* print out row number and recordset details using a pre-defined format */
printf(
'%d;%d;%s;%s<br />',
$row_number,
$row['id'],
$row['name'],
$row['description']
);
}


exit();
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<script src='//code.jquery.com/jquery-latest.js'></script>
<title>Basic Ajax & db interaction</title>
<script>
$( document ).ready( function(){
$.ajax({
url: location.href,
type: 'POST',
success: function( response ) {
console.log( response );
document.getElementById('out').innerHTML=response;
}
});
} );
</script>
</head>
<body>
<div id='out'></div>
</body>
</html>

关于javascript - 如何在javascript中获取php echo结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55245040/

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