Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are quite a few things wrong.</p> <p>You are not using the <code>name</code> attribute on the <code>input</code>/<code>select</code> elements. They need to have a <code>name</code> in order for them to be sent with the POST request. The <code>id</code> attribute has nothing to do with forms (except when using labels; the <code>for</code> attribute points to them)</p> <pre><code>&lt;form action="/path/to/php/file.php" method="post" id="addform"&gt; &lt;div&gt; &lt;label for="carname"&gt;CName&lt;/label&gt; &lt;input type="text" name="carname" id="carname"&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="fuel"&gt;Fuel&lt;/label&gt; &lt;select id="fuel" name="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;/div&gt; &lt;div&gt; &lt;label for="transmission"&gt;Transmission&lt;/label&gt; &lt;select id="transmission" name="transmission"&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;/div&gt; &lt;div&gt; &lt;label for="engine"&gt;Engine&lt;/label&gt; &lt;input type="text" id="engine" name="engine"&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="doors"&gt;Doors&lt;/label&gt; &lt;input type="text" id="doors" name="doors"&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="total"&gt;Total&lt;/label&gt; &lt;input type="text" id="total" name="total"&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="submit"&gt;Engine&lt;/label&gt; &lt;input type="submit" id="submit" value="Add Car" onclick="addRecord()"&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p>As for the javascript, it is much easier to use jQuery (or a similar library). It abstracts away all the nitty-gritty details and browser differences, and makes your life much easier.</p> <pre><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script&gt; // When the page is done loading... $(function () { // When the form is submitted $("form#addform").on("submit", function () { // Get the form data and serialize it var data = $(this).serialize(); // Create a POST request var post = $.post("/path/to/php/file.php", data); // When the request is done show a message post.done(function () { alert("Form submitted!"); }); // If the request fails show another message post.fail(function () { alert("Error! Form not submitted."); }); // Returning false prevents the form's normal // behaviour, i.e. its submission return false; }); }); &lt;/script&gt; </code></pre> <p>Looking at your PHP code it looks like you have just learnt PHP: You are using a deprecated extension (<code>mysql</code>); you just use POST variables without bothering with security; and your SQL code looks a little iffy (e.g. what is <code>AVAILABLE</code> and why is it set to <code>$total</code>?); and finally, string array indices should be quoted.</p> <pre><code>// Database handle $db = new MySQLi('194.81.104.27', 'www', 'www', 'xyz'); // SQL query with placeholders $sql = "INSERT INTO tbl (CARNAME,FUELTYPE,TRANSMISSION,ENGINESIZE,DOORS,TOTAL) VALUES (?,?,?,?,?,?)"; // Create a statement object $stmt = $db-&gt;prepare($sql); // Fill in the placeholders $stmt-&gt;bind_param('ssssii', $carname, $fuel, $transmission, $engine, $doors, $total); // Set the values $carname = filter_input(INPUT_POST, 'cname', FILTER_SANITIZE_STRING); $fuel = filter_input(INPUT_POST, 'fuel', FILTER_SANITIZE_STRING); $transmission = filter_input(INPUT_POST, 'transmission', FILTER_SANITIZE_STRING); $engine = filter_input(INPUT_POST, 'engine', FILTER_SANITIZE_STRING); $doors = filter_input(INPUT_POST, 'doors', FILTER_SANITIZE_NUMBER_INT); $total = filter_input(INPUT_POST, 'total', FILTER_SANITIZE_NUMBER_INT); // If the statement was not executed then there was an error if ($stmt-&gt;execute() === false) { header('HTTP/1.0 404 Not Found', true, 404); } // Done </code></pre> <p><strong>Note:</strong> I never tested any of the examples. I just wrote them down in my text editor, as needed, so you will probably need to adapt them.</p>
 

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