I have a select menu with many text-based options. However when I submit the form, instead of actually returning the textual item in the POST variable is it possible to set it to return the ID of the selected item?

For example I have the following form:

<select name="dev_stage" id="dev_stage">
  <option>Initial Brief</option>
  <option>Initial Concept Design</option>
  <option>Design Development</option>
  <option>HTML Coding</option>
  <option>Server Side Coding</option>
  <option>SEO</option>
  <option>Feedback and Tweaking</option>
  <option>Delivery and Installation</option>
  <option>Invoiced</option>
</select>

If the user selected "Initial Brief" can I set the form to submit the ID '0' instead of "Initial Brief"?

Dani AI

Generated

is right — the browser submits the option's value attribute, not the visible text. If you leave value out, the text node is sent. Setting value="0" (or any ID) for the option makes the form POST that ID. confirmed that this was the fix.

A minimal example (values are IDs, labels are what users see):

<select name="dev_stage">
  <option value="0">Stage 0 - Planning</option>
  <option value="1">Stage 1 - Concept</option>
  <option value="2">Stage 2 - Implementation</option>
</select>

On the PHP side validate and treat the posted value as data from the client. Using filter_input is a simple, safe approach:

$stage = filter_input(INPUT_POST, 'dev_stage', FILTER_VALIDATE_INT);
if ($stage === null || $stage === false) {
    // missing or invalid value: handle error
} else {
    // $stage is a validated integer you can use safely
}

Notes and gotchas: a string "0" is truthy only when checked strictly, but empty("0") returns true — avoid empty() if 0 is a valid ID. When the ID maps to a database row, whitelist or verify the ID exists before acting, and always use prepared statements for DB queries. If the options are generated from the database, output the DB id into the value attribute and keep user-facing text separate.

Recommended Answers

All 2 Replies

An option value can be passed like this:

<option value = 1>Initial Brief</option>

Ah thanks a lot fixed it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.