0

Been looking for a few days for the answer and even tho I think this should be simple, I just can't seem to make it work.

First I populate a "select" with values from my database with a while loop.

<select class="admin-content-select" name="sale">
    <?php try {
        $stmt = $db->prepare('SELECT saleID, saleKal, saleNr FROM wcms_sale');
        $stmt->execute(array(':saleKal' => $row['saleKal']));
        while($sale = $stmt->fetch()){
            echo '<option value="'.$sale['saleKal'].'">'.$sale['saleKal'].'</option>';
        } 
    }catch(PDOException $e) {
        echo $e->getMessage();
    }
    ?>
</select>

then I have an input field which I need to get populated with a value based on what is selected. The values are on the same row in the db so my initial thought was that it would be easy to say: "when X row is selected, echo Y" but since it's a while loop it will simply echo a lot of input fields.

Any work arounds or am I totally in the wrong here with the while loop?

Hope I explained this right and thanks beforehand for any answer :)

Answer:

<select id="saleName" class="admin-content-select" onchange="changeFunc()">

<option value="'.$sale['saleNr'].'">'.$sale['saleKal'].'</option>';

Then added a function as suggested:

function changeFunc(){
    var x = document.getElementById("selectID").value;
    document.getElementById("inputID").value = x;
}

Thanks for all the answers!

5
  • How do you know if value is selected from wcms_sale? Commented Jul 13, 2016 at 12:14
  • Do you need to show the saleKal value when click on the respective select value? Commented Jul 13, 2016 at 12:18
  • I only select values from that DB: "FROM wcms_sale". Or is that not what you mean? Commented Jul 13, 2016 at 12:19
  • @Arun Sry I updated to make it clear. I need to show the "saleNr" in an input field based on what "saleKal" i've selected. Commented Jul 13, 2016 at 12:23
  • @christopherlarsen, check my updated answer Commented Jul 13, 2016 at 12:39

3 Answers 3

1

If you don't want to do an ajax call, then do like below using jquery

Change your echo to

echo '<option value="'.$sale['saleKal'].'" data-value="'.$sale['saleNr'].'">'.$sale['saleKal'].'</option>';

Add this jquery to the js file

$('.admin-content-select').on('change', function(){
   var dataValue = $('option:selected', this).attr('data-value');//getting the value from the attribute
   $('#your_input_id').val(dataValue);//add the value to input box
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer aswell :)
@christopherlarsen, With pleasure
0

You have to decide which part of your application will be in charge of retrieving the matching value. There are three options to achieve what you want to do:

  1. Send the selected value back to the server and have the server retrieve the corresponding value and render it on a new page.
  2. Render all possible values into a JavaScript array and put a JavaScript snippet into your application that retrieves the corresponding value from this array and puts it into the text field.
  3. Build an AJAX application that will send the selected value back to the server in the background, retrieve the value and then show it to the user without reloading the page.

In all three situations you will need some way to trigger the action. Either by adding a button next to the select box or by adding an onChange event via JavaScript.

Comments

0

You can use jQuery

$('.admin-content-select').change(function () {
        var content = $(this).val();
        if(content == 'A')
        {
            $('#id_of_input').val('A is selected');
        }
        else if(content == 'B')
        {
            $('#id_of_input').val('B is selected');
        }...
    });

1 Comment

Thank you for leading me to the answer!

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.