0

The issue is i can only retrieve one value from the select dropdown even i have selected 2, have tried to look at similar question here,but none seems to work for me. Any thoughts? Thanks

if (isset($_POST['submit'])){

$smsorcall = $_POST['smsorcall'];

foreach($smsorcall AS $index => $smsorcall ) {

echo "$smsorcall";}

}
  <form action="newpatient.php" method="post">
  
   <p>Reminder Preference: *</p> 
		  <select name="smsorcall[]" style="width: 250px" class="form-control" multiple>
		
          <option value="SMS">SMS</option>
          <option value="Call">Call</option>
          <option value="Email">Email</option>
          
          </select>
          
          
           <button type="submit" name="submit">Submit</button>

          </form>

My code

   <?php ob_start();
   session_start();

  include('connect-db.php');

  if (isset($_POST['submit']))
  {


  $patientid = $_POST['patientid'];

  $smsorcall = $_POST['smsorcall'];

  foreach($smsorcall AS $index => $smsorcall ) {

  echo "$smsorcall";}



 $_SESSION['smsorcall'] = $smsorcall;

In another html page, i echo the $_SESSION['smsorcall'] to display the result

5
  • Your code works. Why do you think you are only getting one value? Commented Oct 17, 2017 at 2:30
  • because i have tried, every time i pick 2 options, only one is shown, i dont know why Commented Oct 17, 2017 at 2:34
  • Do you mean shown in your echo, or do you show them some other way in your own code? In the code you posted, it looks like there is only one option because you print out the two without a space, so you I'm seeing "SMSEmail" instead of "SMS Email". Commented Oct 17, 2017 at 2:39
  • both SMSEmail and SMS Email will be fine, but the probem is cant get them Commented Oct 17, 2017 at 2:41
  • Can you post the exact code you are using to display the posted values? Because none of the code here produces the results you are describing, so you must have something different. What you describe is what would happen if the foreach loop wasn't working as expected. Commented Oct 17, 2017 at 2:43

1 Answer 1

1

You are only saving the last value to the session - you should save the whole array from the POST, then when you want to get the values loop through the array from the session, e.g.:

if (isset($_POST['submit']))
{
    // save the WHOLE ARRAY of selected options to the session
    $_SESSION['smsorcall'] = $_POST['smsorcall'];

    /* Any more code here... */
}

On your other page:

if (isset($_SESSION['smsorcall']))
{
    // Get the array of all selected options from the session
    $smsorcall = $_SESSION['smsorcall'];

    // loop through the array to process each option one at a time
    foreach($smsorcall AS $index => $option ) {
        // Do whatever you want with each option here, e.g. to display it:
         echo $option;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

still cant... so $option contains the value of the whole array eg SMSEmail, can i store that in a session, so in another page i can access it?,
@tam No, $option is just one of the options, which we get by looping through the array. $smsorcall is the whole array, so we save that to the session. Then in your other page, use the code below the heading On your other page
@tam I've added some more comments to the code to explain
@tam Glad I could help! In future, to make sure you get helpful answers, make sure the code you post in your question replicated the problem so we can see what's happening :)

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.