I HAVE ONE FORM IN THIS I AM INSERTING AND UPDATIONG BOTH, I WANT TO RETAIN the value of combox box when php validations occurs

<td>Page Id</td>
      <td><select name='pageID' class="select">
          <option  value='0' >select any page</option>
          <?php

$sql10 = "SELECT ID,Title FROM page";
$val=$PageId?:'';
$rs = mysql_query($sql10) or die(mysql_error());
while($row = mysql_fetch_array($rs)){
$selected = ($val == $row['ID'] ? 'selected="selected"' : '');
echo '<option value ="' . $row['ID'] . '" '. $selected .'>' . $row['Title'] . '</option>';
}

want that if php validation ouccur the value of the combox still not changed

Dani AI

Generated

Server-side validation will reset a <select> unless the form is explicitly re-rendered using the submitted value (or the existing record value when editing). The common pattern is: prefer the submitted value when validation fails, otherwise fall back to the stored DB value. In ’s post the variable used for the selected option may be unset; ’s reply mixes field names and sets an invalid fallback for the selected attribute, which will prevent the option from being marked correctly.

A simple, robust pattern:

  • Decide the source for the current selection: first check submitted input, then the existing record, then a default.
  • When generating options, compare IDs as integers and escape titles for HTML.
  • If using Post-Redirect-Get, persist old input (flash in session) before redirecting back.

Example (modern, safe approach):

<?php
// choose submitted value first, else existing record, else 0
$currentPageID = isset($_POST['pageID']) ? (int)$_POST['pageID'] : (int)($record['pageID'] ?? 0);

$stmt = $pdo->query('SELECT ID, Title FROM page');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $id = (int)$row['ID'];
    $title = htmlspecialchars($row['Title'], ENT_QUOTES, 'UTF-8');
    $selected = ($id === $currentPageID) ? ' selected' : '';
    echo "<option value=\"$id\"$selected>$title</option>\n";
}
?>

If the form uses a redirect after validation, store submitted values in session before redirecting and restore them when the form is shown again:

// before redirect:
$_SESSION['old'] = $_POST;
header('Location: form.php');
exit;
// on form.php:
$old = $_SESSION['old'] ?? [];
$currentPageID = (int)($old['pageID'] ?? $record['pageID'] ?? 0);

Troubleshooting notes: ensure the form name exactly matches the key read on the server (case matters), confirm the form method (POST vs GET), avoid mysql_* (use PDO or mysqli), and always escape output to prevent XSS.

You can use the code below. Replace the respective values with the ones that apply to your code.

<select name="pageId">
                <?php
                    // I used the $_GET method. You could be using $_POST
                    $subject_id = $_GET['subject_id'];

                    // display a drop down of all categories with the chosen category preselected
                    $query = mysql_query("SELECT * FROM subjects WHERE subject_id = '$subject_id'");
                    while ($row = mysql_fetch_assoc($query)) 
                    {
                        $sub_id = $row['subject_id'];
                        $subject_name = $row['subject'];

                        if ($sub_id == $subject_id) 
                        {
                            $selected = "selected='selected'";
                        }
                        else
                        {
                            $selected = "selected = 'none'";
                        }

                        echo "<option value='$subject_name' $selected>$subject_name</option>\n";
                    }

                ?>
                </select>
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.