将所选名称POST到数据库

I have a combobox which is populated from my database:

<select id="product" name="product">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>

I am using the form method post to send the selected value to my database:

$product = $_POST['product'] ;

When I send the data to my database I only get the selected value in my database ('1', '2' or '3').

I also want to send the name of the selected option to my database ('One', 'Two' or 'Three').

Does someone know if it is possible to post the name of the selected value to the database?

You could use a hidden field, and update that field, on option change with JavaScript.

<input type="hidden" name="numberWritten" id="numberWritten" value="" />

JavaScript:

document.getElementById('product').onchange = function() {
    var element = document.getElementById('product');
    var otherValueFromOption = element[element.selectedIndex].innerHTML;
    document.getElementById('numberWritten').value = otherValueFromOption;
}

You now have a $_POST['numberWritten']. With a value of either One Two or Three

There is a very tricky solution for this . If your drop-down is populated from Database , then replace my solution with the variable names. Use the following way :-

<select id="product" name="product">
    <option value="One|1">One</option>
    <option value="Two|2">Two</option>
    <option value="Three|3">Three</option>
</select>

Now explode the $_POST['product'] with '|' at the time of database insert .

When you use fields inside an HTML form, only the value of the fields gets sent. My suggestion is that, on the script that receives the POST, you transform whatever you receive, into whatever you want to insert into the database, for instance using a switch statement or some other way of evaluating what was posted.

$value_to_insert = '';
switch($_POST['product']){
    case '1':
    $value_to_insert = 'One';
    break;
case '2':
    ...
}

Another possibility would be to use the onChange of the select, to set the value of the selected item into a hidden input field, which will also go on the post.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>setvalue</title>
</head>
<body>
    <script>
        function setValue(sel){
            document.getElementById('valueToPost').value = sel.options[sel.selectedIndex].text;
        }
    </script>
    <form action="xpto.php" method="post">
        <input type="text" id="valueToPost" name="valueToPost" value="One">
        <select onchange="setValue(this)">
            <option value="1" selected>One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
        </select>
    </form>
</body>
</html>

P.S.: change <input type="text" to <input type="hidden" to hide the field.

You would have to creat a dynamic variable that will changed based on user's selection.

I hope this helps

<?php
$product = $_POST['product'];
$productName;
if(product == 1){
$productName = 'One';
}
if(product == 2){
$productName = 'Two';
}
if(product == 3){
$productName = 'Three';
}

$insert = ("INSERT INTO table (product, productName) VALUES ('$product', '$productName')");

// query SQL to insert data..



//hope this is helpful 

?>

It's not a good idea to do that. If you really need the text value in your server side script then you should obtain the text from the DB in the server side script.

If you are loading this combo box from the database and you defined properly your table keys, you shouldn't need to send back the text value of the field.

You need to define a key value for this data and use it for the 'value' of the field. If you don't have a key field in this table and you want to get the text value of the combo then you could use the text as a value in your combo and forget the value you are using now, but this is not recommended