-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!

7
  • What you have written makes no sense. Jul 16, 2012 at 14:19
  • 2
    If you remove the value attribute from your html options, it'll post the text content of the selected option by default.
    – Sam Dufel
    Jul 16, 2012 at 14:20
  • your HTML structure is invalid and the php isnt much clearer.. Jul 16, 2012 at 14:20
  • 1
    <option value="Nocria 14 LBC">Nocria 14 LBC</option>, and let me introduce you to a friend of mine called Bobby Tables
    – DaveRandom
    Jul 16, 2012 at 14:20
  • 1
    Do not build SQL by mashing strings together.
    – Quentin
    Jul 16, 2012 at 14:20

4 Answers 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.

1
  • @oBo - That is a detail that you should have mentioned in your question.
    – nickb
    Jul 16, 2012 at 14:21
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.

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

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.