Note that there are some explanatory texts on larger screens.

plurals
  1. PO$_POST is empty when using AJAX (Prototype)
    primarykey
    data
    text
    <p>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. </p> <p>Here is the JS code:</p> <pre><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"); } }); </code></pre> <p>All JS is in the element...The function is called when choosing an option from a box</p> <p>PHP code: </p> <pre><code>&lt;?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-&gt;connect_error) { die('Connect Error: '.$connection- &gt;connect_error); } else { //Refering to $DB_NAME declared earlier //Select DB global $DB_NAME; $DB_selected = $connection-&gt;select_db($DB_NAME); if (!$DB_selected) { die ('Can\'t use : ' . $connection-&gt;connect_error); } else { $result = true; } } return $result; } ?&gt; &lt;?php echo "&lt;form&gt;"; echo "&lt;select&gt;"; //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-&gt;prepare($QUERY_SELECT_PODCASTS_FOR_PROGRAM)) { $stmt-&gt;bind_param("i", $program); //$stmt-&gt;bind_param("i", $program); //$program = $_POST["programs"]; $program = $_POST["programID"]; $stmt-&gt;execute(); $stmt-&gt;bind_result($title, $refnr, $filepath); } if (is_null($_POST["programs"])) { echo "&lt;option&gt;Choose a program first...&lt;/option&gt;"; //echo "&lt;option&gt;".$file."&lt;/option&gt;"; } else { if (is_numeric($_POST["programs"])) { while($stmt-&gt;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 "&lt;option value=\"".$refnr."\" id=\"".$refnr."\" onclick=\"play('".$filename."')\"&gt;".utf8_encode($title).utf8_encode($filename)."&lt;/option&gt;"; } } } $con-&gt;close(); } echo "&lt;/select&gt;"; echo "&lt;/form&gt;"; ?&gt; </code></pre> <p>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.</p> <p>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..</p> <p>This is kind of difficult when I don't get the POST'ed variabel</p> <p>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..</p> <p>Read this post to get a better understanding.. <a href="http://www.phpfreaks.com/forums/index.php?topic=213086.0" rel="nofollow">Same problem</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload