0

I have an initial form with a single input and a submit. The options are gotten from a DB column 'clinics'. The user selects a clinic and submits by hitting NEXT.

  1. The submitted $_POST value goes into the variable $clinic,
  2. The $clinic variable is used to query the DB and get the $clinic_hours variable.
  3. The user is then redirected to a new form with a text input prefilled with the $clinic value and with a label that says "clinic: General Hospital available hours: 7.4", which is $clinic and $clinic_hours.
  4. The user fills the rest of the form and submits, but this causes the 2 variables to become undefined, and the form cannot be submitted. Does anyone know how to retain the $_POST values so I can submit them in a form?

This is my code:

form 1:

<form action="session.php" method="POST">      

    <select id="clinic" name="session_clinic">      

          <?php              
          while ( $row = mysqli_fetch_assoc($get_org_query) )                           
            {                      
               echo '<option 
       value="'.$row['name'].'">'.$row['name'].'</option>';                                
            }                                                 
          ?> 
       </select>

          <input type="submit" id="clinic_submit" 
          name="clinic_submit" value="Next">                         
</form>  

form 2:

 <form action="session.php" method="POST">  

    <label for="session_clinic" class="clinic_label">Clinic: <?php echo 
     $clinic ?> (Available: <?php echo $clinic_hours ?> hours)</label>
          <br>            

          <input type="text" id="session_clinic" 
       class="session_clinic" name="session_clinic" value="<?php 
        echo $clinic ?>">              

          <input type="date" name="session_date">

          <input type="text" id="session_duration" 
          class="duration" name="session_duration">

          <textarea name="session_note" id="session_note" cols="56" rows="7"></textarea>                          

          <input type="radio" name="status" value="Billable"> Billable<br>
          <input type="radio" name="status" value="Not Billable"> Not Billable<br>

          <input type="submit" name="session_submit" value="Submit">
</form>      
Barmar
  • 741,623
  • 53
  • 500
  • 612
Argut
  • 1
  • 5
  • Why don't you use session variables to retain variables between scripts? – Barmar Sep 06 '17 at 21:06
  • 1
    I believe you'll find it here: https://stackoverflow.com/questions/871858/php-pass-variable-to-next-page – Abu Nooh Sep 06 '17 at 21:13
  • Be aware of the security issues when working with the suggested hidden inputs! Storing them in session is probably the safest. The link provided by @AbuNooh contains all the info you need! – Pevara Sep 06 '17 at 21:38
  • Possible duplicate of [PHP Pass variable to next page](https://stackoverflow.com/questions/871858/php-pass-variable-to-next-page) – Pevara Sep 06 '17 at 21:39
  • I tried that but it didn't work. – Argut Sep 06 '17 at 22:19

2 Answers2

0

You can use hidden inputs to pass the values in the second form:

<input type="hidden" name="session_clinic" value="<?php echo $clinic; ?>">

As long as they have the same name as the first form then you should have access to the values again in the $_POST array.

Mike S
  • 1,636
  • 7
  • 11
  • The thing about hidden inputs is it defeats the purpose because the point is for the user to see the clinic selected, along with its available training time before submitting and billing for a training session (the clinic must have pre-paid time available for the trainer to train it). If it were about simply submitting the data, I would use just one form. – Argut Sep 06 '17 at 21:43
  • You can both store the data in a hidden input AND display the data for the user if that is what you require. – Mike S Sep 06 '17 at 22:27
0

In the second form you should create hidden fields and send $clinic and $clinic_hours.

 <input type="hidden" name="clinic" value="<?php echo $clinic; ?>">
 <input type="hidden" name="clinic_hours" value="<?php echo $clinic_hours; ?>">
Tomasz W.
  • 412
  • 3
  • 8