简体   繁体   中英

PHP/MYSQLI how can I return a variable from a while loop inside a form to use in a $_GET request in that form's action/method?

I have a html form which uses a PHP while loop to populate a select menu from a MYSQLi database. Code below;

$query = "SELECT on_programme, last_name, first_name, middle_name, id FROM constant_client WHERE on_programme=1 ";
$result = mysqli_query($conn, $query);

echo '<p><label for="current_clients">Select Client by name: </label>
        <select id="current_clients" name="current_clients">
        <option> -- Select Client -- </option>';

        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
          echo '<option>'.ucwords($row['last_name']).', '.ucwords($row['first_name']).' '.ucwords($row['middle_name']).'</option>';
        }
echo '</select>'; 

The above works, the form populates with the required client records. However, I am then looking to use this data to link to an individual client page, which I tried using

echo '&emsp;<a href="selected_client.php?id='.$row['id'].'" onClick="this.form.submit()">Go!</a>';

which did not work, so I tried setting the form action to

action="selected_client.php?id='.$row['id'].'"

with the method as "GET", which also did not work. As you can see in the query I am selecting 'id' from the table as well so I can call on that part of the array later, I just don't particularly need or want to echo it. Using the form action method, the $row['id'] variable is not set, and using the onClick method, I get a "Trying to access array offset on value of type null" error.

I imagine that the variable needs to be set before the form action can take place, but I can't think how I would be able to do that without moving the while loop or at least returning a value from it. Is it possible to call the form method and action at submission, rather than at the top of the form? Would that make any difference at all?

Edit; as requested, my attempt at using a hidden method for the form

echo '<input type="submit" name="submit" value="Go!">
<input type="hidden" name="id" value="'.$row['id'].'">';

Which still returns the value of type null error.

Edit #2 - all code inline.

$query = "SELECT on_programme, last_name, first_name, middle_name, id FROM constant_client WHERE on_programme=1 ";
$result = mysqli_query($conn, $query);
echo  '<form action="selected_client.php" method="get">
    <fieldset>';

echo '<p><label for="current_clients">Select Client by name: </label>
        <select id="current_clients" name="current_clients">
        <option> -- Select Client -- </option>';

        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
          echo '<option>'.ucwords($row['last_name']).', '.ucwords($row['first_name']).' '.ucwords($row['middle_name']).'</option>';
        }
echo '</select>';
echo '<input type="submit" name="submit" value="Go!">
<input type="hidden" name="id" value="'.$row['id'].'">';

echo '</fieldset></form>';

Add onChange event and pass the selected value to some function, in that function set the hidden field with this selected value.Now you can submit the form and your selected value can be received.Below is solution.

$query = "SELECT on_programme, last_name, first_name, middle_name, id FROM constant_client WHERE on_programme=1";
$result = mysqli_query($conn, $query);
echo  '<form action="selected_client.php" method="get">
    <fieldset>';

echo '<p><label for="current_clients">Select Client by name: </label>
        <select id="current_clients" name="current_clients" onChange="setId(this.value);">
        <option value=""> -- Select Client -- </option>';

        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
          echo '<option value="'.$row['id'].'">'.ucwords($row['last_name']).', '.ucwords($row['first_name']).' '.ucwords($row['middle_name']).'</option>';
        }
echo '</select>';
echo '<input type="submit" name="submit" value="Go!">
<input type="hidden" name="id" id="hidden_field" value="">';

echo '</fieldset></form>';

Add this script to set value in hidden field.

<script>
function setId(rowValue){
   document.getElementById("hidden_field").value=rowValue;
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM