1

I have a form with a select box where the user can select pre-existing fields from a database:

<form action='' method='POST' autocomplete='off' enctype="multipart/form-data">
  <p><strong>Title and Description:</strong></p>
  <select name='uidtitle' class='chosen-select'>
  <option value="0" ></option>
    <?php 
      $result = $mysqli->query("SELECT uid, title, description FROM tblFacilityHrs") or die($mysqli->error);
      while ($row = $result->fetch_array(MYSQLI_ASSOC)){
        echo "<option value=\"1\">" . $row['title'] ." - " . $row['description'] . "</option>";
        $uid = $row['uid'];
        $title = $row['title'];
        $desc = $row['description'];
      }
    ?>
    </select>
...

How can I send all three of those values (separately) to my postback for SQL?

if (isset($_POST['submitted'])) { 
  //Get params for prepared statements
  $startDatec= date("Y-m-d H:i:s", strtotime($_POST['startEventDate']));
  $endDatec= date("Y-m-d H:i:s", strtotime($_POST['endEventDate']));
  $startTimec=$_POST['startTime'];
  $endTimec=$_POST['endTime'];
  $recurc=$_POST['recurrence'];
  $finalc= date("Y-m-d H:i:s", strtotime($_POST['finalDate']));
  ...
1
  • Are you referring to uid, title, and description? Commented May 22, 2012 at 14:05

1 Answer 1

1

I have no idea why you would need to send all three values back. Database Keys exist for the reason of being able to identify ALL fields in a record given just a single field, in this case I'm assuming uid. Passing that field alone would allow you to select the other fields in your postback before performing the operation that you intend.

However, it is possible using hidden form fields, although I don't advocate this approach.

<select name='uidtitle' class='chosen-select'>
  <option value="0" ></option>
    <?php 
      $result = $mysqli->query("SELECT uid, title, description FROM tblFacilityHrs") or die($mysqli->error);
      $cacheArray = array(); // Used to store the information to be used below
      while ($row = $result->fetch_array(MYSQLI_ASSOC)){
        echo "<option value=\"" . $row['uid'] . "\">" . $row['title'] ." - " . $row['description'] . "</option>";
          $cacheArray[] = $row;
      }
    ?>
    </select>

<?php

    foreach($cacheArray as $k => $v) {
        echo '<input type = "hidden" name = "title-' . $v['uid'] . '" value = "' . $v['title'] . '">';
        echo '<input type = "hidden" name = "description' . $v['uid'] . '" value = "' . $v['description'] . '">';
    }

?>

The hidden form fields will be present for all records in your tblFacilityHrs table. The names are made distinct by appending the uid to the name. You can determine what fields you are interested in in your postback by:

$_POST['title-'.$_POST['uidtitle']]
$_POST['description-'.$_POST['uidtitle']]
Sign up to request clarification or add additional context in comments.

1 Comment

You're absolutely right I didn't think of that. I guess I do only need the UID.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.