Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax won't post to PHP MYSQL
    primarykey
    data
    text
    <p>I have a page that has 2 cascading Select using Ajax. The Ajax correctly populates the second Select, however when I go to post the data to MySQL using PHP i get an error that the field populated by AJAX is "Unidentified." The exact error is </p> <blockquote> <p>Notice: Undefined index: probcode in ..\input.php on line 21</p> </blockquote> <p>It does post if the Ajax call is not made and the default select is still there. It only errors after AJAX has replaced the DIV.</p> <p>I would like to add that it is like the from element disappears from PHP even though AJAX adds it to the form. Is there another PHP function I should use to post AJAX data to MySQL?</p> <p>I do not know Jquery and am looking for a way to get this to work with just JavaScript at the moment (because of time and my lack of Jquery knowledge), however if there is an easy way to add Jquery to the existing code I would definitely be interested. Also i know the data is not secure going into MySQL at the moment, That will be addressed immediately after this. I am just working on a wire-frame of a specific function at the moment.</p> <p>First the two selects. </p> <pre><code> &lt;tr&gt; &lt;td&gt;Problem Type&lt;/td&gt; &lt;td&gt;&lt;?php mysql_connect("localhost", "root", "") or die("Connection Failed"); mysql_select_db("test")or die("Connection Failed"); $query = "SELECT * FROM wfprobtype ORDER BY probtype ASC"; $result = mysql_query($query); ?&gt; &lt;select name="probtype" id="probtype" onchange="changeContent(this.value)"&gt; &lt;?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?&gt; &lt;option value="&lt;?php echo $line['ID'];?&gt;" &gt; &lt;?php echo $line['probtype'];?&gt; &lt;/option&gt; &lt;?php } mysql_close() ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt;Problem: &lt;/td&gt; &lt;td&gt; &lt;div id="val2div"&gt; &lt;select name="probcode" id="probcode"&gt; &lt;option value="default"&gt;&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>This is what the Ajax calls to replace "val2div" val2.php</p> <pre><code>&lt;?php $val2=intval($_GET['val2']); mysql_connect("localhost", "root", "") or die("Connection Failed"); mysql_select_db("test")or die("Connection Failed"); $query = "SELECT * FROM wfprobcode WHERE typeID='$val2' ORDER BY Probcode ASC"; $result = mysql_query($query); ?&gt; &lt;select name="probcode" id="probcode" onchange=""&gt; &lt;?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?&gt; &lt;option value="&lt;?php echo $line['Probcode'];?&gt;"&gt; &lt;?php echo $line['Probcode'];?&gt; &lt;/option&gt; &lt;?php } mysql_close() ?&gt; </code></pre> <p>Ajax Script Dny.js</p> <pre><code>function changeContent(val) { if (val=="") // if blank, we'll set our innerHTML to be blank. { document.getElementById("val2div").innerHTML=""; return; } if (window.XMLHttpRequest) // new browser { xmlhttp=new XMLHttpRequest(); // new browser alert("newb"); } else // Old Browser { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } // on state change xmlhttp.onreadystatechange=function() { // if we get a good response from the webpage, display the output if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { document.getElementById("val2div").innerHTML=xmlhttp.responseText; } } // GET file. xmlhttp.open("GET","val2.php?val2="+val, true); xmlhttp.send(); alert(val) } </code></pre> <p>post to MySQL input.PHP</p> <pre><code> &lt;?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $sql="INSERT INTO `test`.`test` ( `ID`, `Dattime`, `probtype`, `probcode` ) VALUES( NULL,NOW(),'$_POST[probtype]','$_POST[probcode]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); ?&gt; </code></pre> <p>I am hoping I am just missing simple something at the moment. This is my first time working with Ajax. I really appreciate any and all help at the moment.</p> <p>EDIT: - One final edit before I attempt to rewrite the whole thing. Is there a way that using Jquery I could fix this? If not does anyone have a down and dirty way to make it just full postback without the AJAX?</p> <p>I really appreciate those that have contributed so far an anyone who has any ideas at all. </p> <p>Thanks in Advance!</p> <p>-D</p>
    singulars
    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