Hi. I am fairly new to PHP and I am experiencing a problem that I can't find an answer to anywhere!! It really bugs me... I want to post data to a PHP script using AJAX. I am using the JS framework Prototype to do AJAX communication.
Here is the JS code:
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");
}
});
All JS is in the element...The function is called when choosing an option from a box
PHP code:
<?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>";
?>
Here is my problem...The value of $_POST is allways "Array ()". When using a regular form that posts, everything is OK, I get the value, but when using AJAX (not only Prototype), I dont.
What i want to do simply put is: Post data with AJAX -> use recieved data in sql query -> make a HTML lement based on the result from the sql wuery..
This is kind of difficult when I don't get the POST'ed variabel
I also took a lokk at what was being sent, and POSTDATA was correct. Please, I really need someones help on this...been looking for days now for an answer..
Read this post to get a better understanding.. Same problem