Note that there are some explanatory texts on larger screens.

plurals
  1. POAJAX can't POST to PHP file
    primarykey
    data
    text
    <pre><code>FIXED! See bottom for solution. </code></pre> <p>I am having an incredibly hard time with this. I've been at it for weeks now. I am trying to use AJAX to add a new record into mysql. The PHP file works 100% but I can't seem to be able to make AJAX to work with my form which has a POST method of sending data. This is my first time here on StackOverflow btw so take it easy on me. Here is the code:</p> <p>HTML code:</p> <pre><code>&lt;form name='addform' method='POST' &gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' id='cname'&gt;&lt;/td&gt; &lt;td&gt; &lt;select id='fuel'&gt; &lt;option value='Petrol'&gt;Petrol&lt;/option&gt; &lt;option value='Diesel'&gt;Diesel&lt;/option&gt; &lt;option value='Hybrid'&gt;Hybrid&lt;/option&gt; &lt;option value='LPG'&gt;LPG&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt; &lt;select id='trans'&gt; &lt;option value='Automatic'&gt;Automatic&lt;/option&gt; &lt;option value='Manual'&gt;Manual&lt;/option&gt; &lt;option value='Semi-Auto'&gt;Semi-Auto&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt;&lt;input type='text' id='engine'&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' id='doors'&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' id='total'&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type='submit' value='Add Car' onclick='addRecord()'&gt;&lt;/td&gt; ... </code></pre> <p>I have an external .js file to handle Javascript:</p> <pre><code>function addRecord(){ if ( document.addform.cname.value == "" || document.addform.fuel.value == "" || &gt;document.addform.trans.value == "" || document.addform.engine.value == "" || document.addform.doors.value == "" || document.addform.total.value == "" ) { alert ("Empty &gt;field(s) - Cannot create record"); return; } var mydata = null; // Mozilla/Safari if (window.XMLHttpRequest) { xmlHttpReq2 = new XMLHttpRequest (); } // IE else if (window.ActiveXObject) { xmlHttpReq2 = new ActiveXObject ("Microsoft.XMLHTTP"); } var cname = document.getElementById('cname').value; var fuel = document.getElementById('fuel').value; var trans = document.getElementById('trans').value; var engine = document.getElementById('engine').value; var doors = document.getElementById('doors').value; var total = document.getElementById('total').value; mydata = '?cname='+cname+'&amp;fuel='+fuel+'&amp;trans'+trans+'&amp;engine'+engine+'&amp;doors'+doors+'&amp;total'+total; alert ("To Server (Add New Record):\n\nadd.php" + mydata); xmlHttpReq2.open('POST', "add.php" +mydata, true); xmlHttpReq2.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlHttpReq2.send(null); xmlHttpReq2.onreadystatechange = addRecordCallback; } </code></pre> <p>The PHP code:</p> <pre><code>&lt;?php $link = mysql_connect ("194.81.104.27", "www", "www"); mysql_select_db ("xyz") or die(mysql_error()); $tot=$_POST[total]; $door=$_POST[doors]; $query = "INSERT INTO tbl (ID, CARNAME, FUELTYPE, TRANSMISSION, ENGINESIZE, DOORS, TOTAL, AVAILABLE) VALUES ('$_POST[cname]','$_POST[cname]', '$_POST[fuel]', '$_POST[trans]','$_POST[engine]', $door, $tot, $tot)"; $result = mysql_query($query); mysql_close ($link); ?&gt; </code></pre> <p>What happens is I put the info into the form, click the button and the alert tells me that it does indeed get the data from the form. But after that nothing happens. I think that the data is not being sent in the right format to the PHP file for some reason.</p> <p>Thanks!</p> <pre><code>Solution was: </code></pre> <p>I managed to get it working! Part of the fault was that like @adeneo said I did not have the <code>onclick='addRecord(); return false;'</code> in there! Another thing I did was to remove the id of the form, not sure if that did anything at all. I have also attached "mydata" to the .send like so: <code>xmlHttpReq2.send(mydata);</code> and finally I had to remove a "?" which was in front of cname in the mydata variable gathering thing. So it was <code>mydata = '?cname='+cname+...</code> and I had to remove the <code>?</code>. Thanks everyone for all the help!</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.
 

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