【发布时间】:2015-05-12 08:46:03
【问题描述】:
我真的从来没有这样做过,我很沮丧,因为我不确定它是如何组合在一起的。我有一个函数,我想调用我的 php(一个 php 文件从数据库中选择信息,第二个文件插入到数据库中)......我需要以我的网站设置方式使用 ajax,但我不知道如何从和向 php 文件传递数据。
在第一个 .js 文件中:
q1LoadVar();
这是我目前在第二个 .js 文件中的 ajax 函数(不工作):
//ajax code has been edited here since original post:
function q1LoadVar() {
alert("called"); //works!
$.get( "q1LoadVar1.php", function( data ) {
console.log(data); //nothing happens!
// alert(data); //nothing happens!
}, "json" );
}
这是我在 q1LoadVar1.php 中的代码,我想从中选择数据并能够在我的 html 中填充文本区域:
/*works when I type this file path directly into the url;
but the file is not communicating back to the ajax function on the
.js file that is calling it*/
<?php
$config = parse_ini_file('../config.ini');
$link = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
echo '<script type="text/javascript">alert("working from php!");</script>';
$query = "SELECT * FROM Game1_RollarCoaster";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
$newRow[] = $row;
}
$json = json_encode($newRow);
echo $json; //works on php file directly!
/*while ($row = mysqli_fetch_array($result)) {
echo $row[Q1_AnswerChoosen];
}*/
mysqli_free_result($result);
mysqli_close($link);
?>
有人可以帮助我了解如何使这一切协同工作吗?谢谢你,克里斯汀
【问题讨论】:
-
您的控制台是否有任何错误?也可能是响应错误或 PHP 错误。尝试在 PHP 文件的顶部包含 `error_reporting(E_ALL); ini_set('display_errors', 1);`.您在控制台中的响应可能会返回您面临的错误类型。
-
您的 INSERT 缺少 VALUES。 dev.mysql.com/doc/en/insert.html - 为了一件事。
-
另外,
mysqli_error($conn)应该是mysqli_error($link) -
我编辑了上述问题,专注于从数据库中选择的 ajax 和 php。当我运行“q1LoadVar1.php”文件时 - php代码可以工作,但是当使用上述ajax调用时它当然不会运行......当我运行ajax时根本没有任何反应......不知道如何测试它..
-
那么,您对
q1LoadVar的函数调用在哪里?而且你有语法错误。检查你的控制台(f12)
标签: php jquery mysql ajax mysqli