smile 22121
smile 22121

Reputation: 295

fill form after option select

I have a product list that filled automatically from the DB, I want to get the data corresponding to the selected product and fill the form.

<form name='login' action='<?php echo ($_SERVER['PHP_SELF']); ?>' method='post' enctype="multipart/form-data">
<?php
$sql = "SELECT product_name FROM products ORDER BY product_name";
$result = $mysqli->query($sql);

if ($result->num_rows > 0) {
echo "<select>";
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<option>" . $row["product_name"]."</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$mysqli->close();
?>
<input type="text" name="product_code" placeholder="Product Code" />
<input type="text" name="product_name" placeholder="Product Name" />
<input type="text" name="product_price" placeholder="Price" />
<input type="file" name="fileToUpload" accept="image/*">
<textarea rows="8" name="product_desc_de" placeholder="Deutsch description"></textarea>
<textarea rows="8" name="product_desc_en" placeholder="English description"></textarea>
<textarea rows="8" name="product_desc_es" placeholder="Spanich description"></textarea>
<button type="submit" class="btn btn-default">Submit</button>
</form>

I know it should be somehow with AJAX

Upvotes: 0

Views: 44

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94682

You have not shown any AJAX code so I assume that was just a mistake.

You do not NEED Ajax to make a form work.

So first give your <select> tag a name

echo '<select name="prods">';

And then your <option> tags a value

echo '<option value="' . $row["product_name"] . '">' . $row["product_name"] . '</option>';

Now when you look at the $_POST array you will have a $_POST['prods']

Personally I would add the id field to the SELECT

$sql = "SELECT id, product_name FROM products ORDER BY product_name";

And use the id as the value like this

echo '<option value="' . $row['id'] . '">' . $row['product_name'] . '</option>';

Upvotes: 1

Related Questions