1

When I select one of the year from drop-down list so, how to show the title that related to year into input fields of the form below.

$formationSQL = "SELECT title FROM formationacademique";
$result =  $connection->query($formationSQL);




 <form method="post">
     <label>Select year:</label>
                    <select>
      <?php foreach($result as $formation): ?>
                        <option id="formationID" name="formationID" value="<?= $formation['ID_Formation']; ?>"><?= $formation['year']; ?></option>
                    <?php endforeach; ?>
                    </select>

              <label>title:</label>
                    <input type="text value="">
        </form>
4
  • 1
    are you looking for php loading a value or javascript that changes on the page? Commented Jun 29, 2019 at 21:36
  • @RudyLister only php loading year record from mysql database into drop down list and then when I select one of the year from drop down so a title related to year must display into title input field. Commented Jun 29, 2019 at 21:39
  • @RudyLister my drop down list is working correctly just want to display title into input field related to year. Commented Jun 29, 2019 at 21:42
  • I am not sure if your data has a title value or if you are using the ID_Formation as a title. Commented Jun 29, 2019 at 22:03

1 Answer 1

2

Here are some changes that are needed for your html formatting. Would need to know if you are wanting this change to happen with javascript or in php? If you don't know the difference, it is that php is server side that will only parse on page load and javascript can do it without a page refresh or reload.

<?php
$formationSQL = "SELECT title FROM formationacademique";
$result =  $connection->query($formationSQL);
?>
<form method="post">
    <label for="formationID">Select year:</label>
    <select id="formationID" name="formationID">
      <?php foreach($result as $formation): ?>
        <option value="<?= $formation['ID_Formation']; ?>">
          <?= $formation['year']; ?>
        </option>
      <?php endforeach; ?>
    </select>
    <label for="input_title">title:</label>
    <input type="text" id="input_title" name="input_title" value="<?=$formation['ID_Formation']; ?>">
</form>

Add the following script contents to your js file or add the script after the form above for the javascript verse of changing the inputs value.

<script>
var year_select = document.getElementById( 'formationID' );
var year_title = document.getElementById( 'input_title' );
year_select.addEventListener( 'change', function( e ) {
  year_title.value = year_select.selectedIndex.value;
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

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.