0

Its my first steps on HTML and PHP . Im trying to create a dropdown box populated by MYSQL database.

In my code i can see the options but i want to get selected item via submit button or ONCHANGE function but i dont know how to do that.

Tried to search google for 5 hours. Im just an idiot or blind but can you help me ?

Here is my code :

<form>    
  <select id="choosePatient" name="PatientBox">
    <option value="" selected disabled hidden>Choose</option>

    <?php foreach($users as $user): ?>

    <option value="<?= $user['patient_id']; ?>">
      <?= $user['name']; ?>
        <?= 
                      $user['surname']; ?>
    </option>


    <?php endforeach; ?>

  </select>
</form>

1 Answer 1

1

Provided detail isn't enough, however I've put together the following snippet for you to get some idea.

$("#choosePatient").change(function(){
    var selectedCountry = $(this).children("option:selected").val();
    alert("You have selected the patient - " + selectedCountry);
});

$("form").submit(function(e){
    e.preventDefault();
    var selectedCountry = $('#choosePatient').children("option:selected").val();
    alert("You have selected the patient - " + selectedCountry);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>  
  <select id="choosePatient" name="PatientBox">
    <option value="" selected disabled hidden>Choose</option>   
    <option value="1">User 1</option>
    <option value="2">User 2</option>
    <option value="3">User 3</option>
    <option value="4">User 4</option>
  </select>
  <input type="submit" value="Submit" />
</form>

Sign up to request clarification or add additional context in comments.

1 Comment

Thats really what i wanted to do. I thought the things can go crazy when populating dropdown from mysql but it was so simple like you wrote. I didn't know that i can get values by JS too. Thanks a lot man <3

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.