-1

Here's the html structure:

<select name="marke" class="marke">
<option value="1">Bosch</option></select>

<select name="modell" class="modell">
<option value="2">Nocria 14 LBC</option></select>

Here is my Php string (simplified)

$modellz = $_POST['marke']. ' ' .$_POST['modell'];
$insertQuery = "INSERT INTO producz (model)
VALUES
('" . $modellz . "')";

Im trying to get the name inside the <option> - tag to Post into the database. But with this code I just get the values. So when i run this i get 1 2 in the database.

But I want Bosch Nocria 14 LBC

OBS: I NEED THE VALUE attribute for my jquery selection script.

Please help!

4

4 回答 4

4

The value of the option is what gets passed to the server, if you want to receive the text within the option instead just remove the value attribute altogether:

<option>Nocria 14 LBC</option>

Will submit that actual string when its selected.

Other than that, please read up about SQL Injections because your current PHP code is vulnerable to attacks.

于 2012-07-16T14:20:06.187 回答
1

either make value='Bosch' or create an array in your php that converts '1' to the string you require.

This:

<select name="marke" class="marke">
<option value="Bosch">Bosch</option></select>

<select name="modell" class="modell">
<option value="Nocria 14 LBC">Nocria 14 LBC</option></select>

or

<?
Options = Array();
Options['1'] = "Bosch";
Options["2"] = "Nocria 14 LBC";
?>

and use Options[insert] to get the real value

Also remember to use mysql_real_escape_string or string->real_escape() for security

于 2012-07-16T14:20:40.773 回答
1

The issue is with the HTML. The value for each <option> tag is what's sent in the POST request.

Change the HTML to:

<select name="marke" class="marke">
<option value="Bosch">Bosch</option></select>

<select name="modell" class="modell">
<option value="Nocria 14 LBC">Nocria 14 LBC</option></select>

and you will see the results you desire.

Also, please validate/sanitize your form-input before saving into the database. If you're using mysql functions, try mysql_real_escape_string().

If jQuery needs access to the original ID for each option, I would suggest one of two methods. Either add a separate attribute to each option, such as rel: <option value="Bosch" rel="1">Bosch</option>, or keep your code as-is (<option value="1">Bosch</option>) and have PHP aware of the ID/value combinations.

The actual database-structure could, and possibly should, modified to support the actual IDs instead of textual values.

于 2012-07-16T14:21:17.803 回答
0

That's because you need to set the value of the option to Bosch and Nocria. POST stores what ever the option value to its corresponding <select> name. Try this

<select name="marke" class="marke">
<option value="Bosch">Bosch</option></select>

<select name="modell" class="modell">
<option value="Nocria 14 LBC">Nocria 14 LBC</option></select>
于 2012-07-16T14:21:40.333 回答