Hi!
Just wanted to know if there is any special syntax to get values in <option> with a space between words.
For example:
<form action="abc.php" method="post">
<select name="study_class">
<option value="Class 1">CLASS 1</option>
<option value="Class 2">CLASS 2</option>
</select>
</form>

Can php recognize "Class 1" or "Class 2" or will it recognize only "Class" or "Class".

Thanks!

Dani AI

Generated

Short answer: values that contain spaces are fine — the browser sends the exact string inside the value quotes and PHP will receive that whole string. 's point was correct, and 's removal of the debug call fixed the visible “string(…)” output because that line came from a debugging function, not from the form processing itself.

A few practical points and safety tips that add to the thread:

  • Debugging: functions that dump type/length (what showed up as string(31) "...") are for development only. Remove them before rendering HTML. For safe, non-visual debugging, write to the server log instead so page output stays clean.
  • Multiple selections: a multi-select is delivered to PHP as an array when the form field uses array-style naming. Check that the incoming value exists and is an array before processing.
  • Output and mail: sanitize and escape values before echoing into HTML or including in an email. Reject CRLFs in mail headers to prevent header injection. Prefer building a safe, joined list server-side (escape each item with htmlspecialchars for HTML output) and log the joined result rather than dumping raw internals to the page.

Example (processing + logging, no raw dumps):

$items = filter_input(INPUT_POST, 'study_class', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
$clean = [];
if ($items && is_array($items)) {
  foreach ($items as $v) {
    $clean[] = htmlspecialchars(trim($v), ENT_QUOTES, 'UTF-8');
  }
  $list = '';
  foreach ($clean as $i => $v) {
    $list .= ($i ? ', ' : '') . $v;
  }
  error_log('Selected classes: ' . $list);
  $message = "Study Classes Chosen are: " . $list;
  // validate headers and send via a safe mail library
}

For production, consider using stable values (IDs) in value and mapping to labels server‑side, and use a mail library (PHPMailer, etc.) that helps prevent header problems and handles encoding.

Recommended Answers

All 4 Replies

No, it should recognise "Class 1", "Class 2"..

<?php

  $study = $_POST['study_class'];

  var_dump($study);
?>

Then depending on whichever value is entered, should assign this.

I haven't tested it though, might be wrong!

Perfect!!!!! worked :)
Thanks!

Well..phorce..it works. But now it outputs the crude values like:
string(31)"Class 1, Class 2"
I don't want var_dump($study); to give any output on its own except to pass on all data

<form action="abc.php" method="post">
    <select name="study_class[]" multiple>
        <option value="Class 1">CLASS 1</option>
        <option value="Class 2">CLASS 2</option>
        <option value="Class 3">CLASS 1</option>
        <option value="Class 4">CLASS 2</option>
    </select>
</form>

abc.php
<?php
#get data
$study_class=implode(', ', $_POST['study_class']);
var_dump($study_class);
?>
<html>
.
.
<P> Study Classes Chosen are: <?php echo $study_class; ?></P>
.
</html>

OUTPUT:
string(31)"Class 1, Class 2"

Study Classes Chosen are: Class 1, Class 2

So, I do not want the 'string....' line to come up.
Moreover, in the php i would also be passing the multiple choices via mail.

answer is to remove var_dump($study_class); and it will work!

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.