3

I created a table in SQL via PHP that has 3 columns. I'm trying to put the values inside a SELECT dropdown in this way:

  • The first column will be in the value of the OPTION (for passing in the POST).
  • The second column will be in the display of the OPTION.
  • The third column will be hidden and sticks to each row in the dropdown, so when the user chooses one option from the dropdown, the hidden column will present in other div.

For example, if the table is:

col1  col2  col3
-----------------
1     Joe   Key st.
2     Matt  Link st.

When the user chooses Joe, Key st. will display in a separate div, and after the user presses enter, the value 1 will be sent via POST.

This is what I got so far:

$('.foo3').on('change', function() {
  var add = $(this).parent().find('.foo3').value();
  $('.foo2').html(add);
});
<select class="foo3">
  <?php while ($curRow=mysqli_fetch_array($TableOutput)) {                                                      
    echo "<input class='foo' type='hidden' value='{$curRow['col3']}'><option value='{$curRow['col1']}'>{$curRow['col2']}</option>"; 
  } ?>
</select>
<div class="foo2"></div>

Thanks!

6
  • Can you please attach your HTML output? Commented Nov 21, 2016 at 11:15
  • It doesn't work. the dropdown was empty. Commented Nov 21, 2016 at 11:16
  • @RuchishParikh Why did you edit the HTML of my answer in to this question? Commented Nov 21, 2016 at 11:22
  • I have edited to convert it into code snippet. sorry if I am wrong. @RoryMcCrossan Commented Nov 21, 2016 at 11:24
  • It's fine to edit the question to include a snippet, but the problem was you used the HTML from my answer, not the OPs original code. Please take care in future. Commented Nov 21, 2016 at 11:25

1 Answer 1

3

You can store your own metadata in an element by using data attribute. You can then read that back out when the option is chosen. Try this:

$('.foo3').change(function() {
  $('.foo2').text($(this).find('option:selected').data('location'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="foo3">
  <option>Please select...</option>
  <option value="1" data-location="Key St.">Joe</option>
  <option value="2" data-location="Link St.">Matt</option>
</select>
<div class="foo2"></div>

To create that HTML output, your PHP would look like this:

echo "<option value=\"{$curRow['col1']}\" data-location=\"{$curRow['col3']}\">{$curRow['col2']}</option>";
Sign up to request clarification or add additional context in comments.

8 Comments

@RoryMcCrossan is it good to store data in html element ? will it cause slow to load ??
It's absolutely fine, and it won't affect performance in any noticable way.
There is no real difference between having the data show on the page, and load it into a data-attribute. There is a very slight overhang (perhaps) of HTML-code, but it should be (in most cases) minimal, and not provide any difference in loading time.
@junkfoodjunkie there's also the overhead of jQuery building the cache object, but again the delay is infinitesimal. If performance is of that high a concern you shouldn't be using jQuery at all.
@DvirNaim the same technique works, just add another data-XXX attribute and use data('XXX') to retrieve it in jQuery
|

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.