我是PHP的新手,我遇到了一个问题,我找不到任何答案! 这真让我烦恼...... 我想使用AJAX将数据发布到PHP脚本。我正在使用JS框架Prototype来进行AJAX通信。
这是JS代码:
new Ajax.Request("/ondemand/Radio.php", {
method: 'POST',
parameters: {programID: id},
onSuccess: function(transport) {
window.alert("Success, " + id);
},
onFailure: function() {
window.alert("Communication problem");
},
onComplete: function() {
window.alert("Complete");
}
});
所有JS都在元素中......从框中选择选项时调用该函数
PHP代码:
<?php
//Selects all programs that have a podcast
$QUERY_SELECT_ALL_PROGRAMS = "SELECT DISTINCT d.defnr, d.name
FROM definition d, podcast p
WHERE p.program = d.defnr";
//Select podcasts that belongs to a given program
$QUERY_SELECT_PODCASTS_FOR_PROGRAM = "SELECT p.title, p.refnr, p.filename
FROM podcast p
WHERE program = ?";
//Selects all podcasts
$QUERY_SELECT_ALL_PODCASTS = "SELECT p.refnr, p.title, p.filename, p.filename
FROM podcast";
$BROADCAST_PATH = "";
$programID = $_POST["programID"];
/* Returns true if DB connection to server and database is OK
* Takes mysqli as parameter
* Connect to the database using the MySQLi API in PHP 5.x
* This is the prefered way*/
function DBconnection($connection) {
$result = false;
//Refering to $con declared eralier
//global $connection;
//Check DB connection
if ($connection->connect_error) {
die('Connect Error: '.$connection- >connect_error); }
else {
//Refering to $DB_NAME declared earlier
//Select DB
global $DB_NAME;
$DB_selected = $connection->select_db($DB_NAME);
if (!$DB_selected) { die ('Can\'t use : ' . $connection->connect_error); }
else { $result = true; }
}
return $result;
}
?>
<?php
echo "<form>";
echo "<select>";
//The MySQL connection object, must be created before connection
$con = new mysqli($MYSQL_SERVER, $MYSQL_USER_NAME, $MYSQL_PASSWORD, $DB_NAME);
if (DBconnection($con)) {
if ($stmt = $con->prepare($QUERY_SELECT_PODCASTS_FOR_PROGRAM)) {
$stmt->bind_param("i", $program);
//$stmt->bind_param("i", $program);
//$program = $_POST["programs"];
$program = $_POST["programID"];
$stmt->execute();
$stmt->bind_result($title, $refnr, $filepath);
}
if (is_null($_POST["programs"])) {
echo "<option>Choose a program first...</option>";
//echo "<option>".$file."</option>";
}
else {
if (is_numeric($_POST["programs"])) {
while($stmt->fetch()) {
print_r($title);
//$filepath holds the value of only the name of the broadcast without the entire path
//40 is the starposition of the name
$filename = substr($filepath, 40);
echo "<option value=\"".$refnr."\" id=\"".$refnr."\" onclick=\"play('".$filename."')\">".utf8_encode($title).utf8_encode($filename)."</option>";
}
}
}
$con->close();
}
echo "</select>";
echo "</form>";
?>
这是我的问题...... $ _POST的值总是“Array()”。 当使用发布的常规表单时,一切正常,我得到了值,但是当使用AJAX(不仅是Prototype)时,我不会。
我想做的只是:用AJAX发布数据 - &gt;在sql查询中使用收到的数据 - &gt;根据sql wuery的结果制作HTML文件..
当我没有获得POST'ed variabel时,这有点困难
我还对发送的内容采取了措施,并且POSTDATA是正确的。 拜托,我真的需要一些人的帮助...现在正在寻找几天的答案..
阅读此文章以便更好地理解.. Same problem
答案 0 :(得分:0)
我不知道你是否在你的问题中粘贴了错误的代码,但$var = $_POST["programID"];
似乎不在PHP块中,因此$ var将不会从POST数组中分配值。 ..
答案 1 :(得分:0)
不知道如何评论所以我必须将此作为答案发布。 如果从您的php脚本复制此代码:
$var = $_POST["programID"];
<p id="p">
<?php
echo "Program ID er " . utf8_encode($var);
?>
</p>
<?php and ?>
$var = $_POST["programID"];
编辑20101028:
好的,我看了一下你新发布的代码,找不到任何明显的东西,我对它做了一些修改:
<?php
$message = "<div id=\"php_debug\";
style=\"border:1px solid red;
background-color: black;
color: white;\"
>";// this is the debug message we will build and later print out where we need it
//Selects all programs that have a podcast
$QUERY_SELECT_ALL_PROGRAMS = "SELECT DISTINCT d.defnr, d.name
FROM definition d, podcast p
WHERE p.program = d.defnr";
//Select podcasts that belongs to a given program
$QUERY_SELECT_PODCASTS_FOR_PROGRAM = "SELECT p.title, p.refnr, p.filename
FROM podcast p
WHERE program = ?";
//Selects all podcasts
$QUERY_SELECT_ALL_PODCASTS = "SELECT p.refnr, p.title, p.filename, p.filename
FROM podcast";
$BROADCAST_PATH = "";
$message .= "\$_POST["programID"] = ".$_POST["programID"]."<hr />";
$programID = $_POST["programID"];
$message .= "\$programID = ".$programID."<hr />";
/* Returns true if DB connection to server and database is OK
* Takes mysqli as parameter
* Connect to the database using the MySQLi API in PHP 5.x
* This is the prefered way*/
function DBconnection($connection) {
$message .= "I am in DBconnection($connection)<hr />";
$result = false;
//Refering to $con declared eralier
//global $connection;
//Check DB connection
if ($connection->connect_error) {
die('Connect Error: '.$connection- >connect_error); }
else{
//Refering to $DB_NAME declared earlier
//Select DB
global $DB_NAME;
$DB_selected = $connection->select_db($DB_NAME);
if (!$DB_selected) { die ('Can\'t use : ' . $connection->connect_error); }
else{ $result = true; }
}
return $result;
}
// Start generating form
echo "<form>";
echo "<select>";
//The MySQL connection object, must be created before connection
$con = new mysqli($MYSQL_SERVER, $MYSQL_USER_NAME, $MYSQL_PASSWORD, $DB_NAME);
if (DBconnection($con)) {
if ($stmt = $con->prepare($QUERY_SELECT_PODCASTS_FOR_PROGRAM)) {
$message .= "\$program = ".$program."(\$program has never been seen before in this script)<hr />";
//$stmt->bind_param("i", $program); This line was here originally, $program is null, has never been initialized before, $programID is the var where you store $_POST data, so I changed it to below
$stmt->bind_param("i", $programID);
$program = $_POST["programID"];//I left this here, becaause I dont know what you use $program for
$message .= "\$program = ".$program."(\$program has been filled with \$_POST["programID"])<hr />";
$stmt->execute();
$stmt->bind_result($title, $refnr, $filepath);
}
$message .= "\$_POST["programs"] = ".$_POST["programs"]."(\$_POST["programs"] has never been sen before)<hr />";
if(is_null($_POST["programs"])) {
$message .= "\$_POST["programs"] was empty so I generated \"Choose program first...\" option"<hr />";
echo "<option>Choose a program first...</option>";
//echo "<option>".$file."</option>";
}elseif(is_numeric($_POST["programs"])){
$message .= "\$_POST["programs"] was numeric so I will start a loop here:<br /> START OF LOOP <br />";
while($stmt->fetch()) {
print_r($title);
//$filepath holds the value of only the name of the broadcast without the entire path
//40 is the starposition of the name
$filename = substr($filepath, 40);
$message .= "\$title = ".$title." and \$filename = ".$filename."<br />";
echo "<option value=\"".$refnr."\" id=\"".$refnr."\" onclick=\"play('".$filename."')\">".utf8_encode($title).utf8_encode($filename)."</option>";
}
$message .= "END OF LOOP<hr />";
}
$con->close();
}
echo "</select>";
echo "</form>";
$message .= "</ div>";
echo($message);//print out the debug message we were building through out the script
?>
您可以使用上面发布的代码并告诉我们它打印出来的内容吗?
另外,我建议你在脚本开头处理所有的帖子数据:
$var1 = $_POST["var1"];
$var2 = $_POST["var2"];
$var3 = $_POST["var3"];
$var4 = $_POST["var4"];
$var5 = $_POST["var5"];
因为这样你可以省去很多困惑,以后只能使用简单的读取变量名。例如什么是$ _POST [“程序”]?是否与$ _POST [“programID”]相同?
总的来说,有很多特色,至少对我来说有变量名称:
$program
$programID
$programs
$_POST["program"]
$_POST["programs"]
$_POST["programID"]
使用哪些?哪些是正确的?
我倾向于通过以其目的开始变量名来解决这个问题:
$ id_program - &gt; db中单个程序或行的ID $ txt_program - &gt;程序描述或程序txt $ num_programs - &gt;程序数量,有多少程序(有多少个ID) $ arr_programs - &gt;程序数组,例如所有程序ID的数组
希望它有所帮助,让我知道。 祝你好运:)
答案 2 :(得分:0)
我可能会在这里遗漏一些东西,但你究竟想做什么? Ajax.Request
默认情况下不返回任何内容。可以使用responseText更新您的网页,但您的代码没有参考此内容。查看代码,您将不会在页面中看到任何更新内容(如果这是您期望的内容),但您应该从onSuccess
回调中获取JavaScript警报。如果没有,请确认您收到的回调(或者如果没有错误)。
如果您希望使用AJAX更新页面中的值,则需要查看Ajax.Updater
。需要说明的是,如果你想用Ajax调用的输出更新“p”的ID,你可以这样做:
new Ajax.Updater('p', '/ondemand/Radio.php', {
parameters: { programID: id }
});
答案 3 :(得分:0)
也许它正在烦恼并将请求作为GET发送?尝试:
print_r($_REQUEST);
$ _REQUEST var将包含$ _POST和$ _GET vars。
我也很难相信,但有可能代替method: 'POST'
你应该使用method: 'post'
- 大多数Prototype示例都使用小写。
答案 4 :(得分:0)
如果您直接发布数据并且结果是您所期望的,那么生成的HTML就是您问题的根源。这一行:
echo "<option value=\"".$refnr."\" id=\"".$refnr."\" onclick=\"play('".$filename."')\">".utf8_encode($title).utf8_encode($filename)."</option>";
是导致问题的原因。 "Array()"
是数组连接到字符串时PHP所做的“toString()”版本。 $refnr
显然是一个数组,Prototype只是选择了这个值。
提示:使用printf('<option value="%d">%s</option>', ...)
来拯救你的眼睛斜杠综合征的疼痛; - )