0

i have a select input that get options from database retailer_id, what i need it, when the admin select a retailer on this select input, i'll have a new one with the retailer_slug, and then, when the admin create or update, will sen to database the retailer_id and the retailer_slug on hidden input. my code is:

EDITED

        <select class="textbox2" id="retailer_id" name="retailer_id">
<option value="">--- Please select store ---</option>
<?php
    $sql_retailers = smart_mysql_query("SELECT * FROM cashbackengine_retailers WHERE status='active' ORDER BY title ASC");
    while ($row_retailers = mysql_fetch_array($sql_retailers))
    {
        if ($retailer_id == $row_retailers['retailer_id']) $selected = " selected=\"selected\""; else $selected = "";
        echo "<option data-slug=\"".$row_retailers['slug_title']."\" value=\"".$row_retailers['retailer_id']."\"".$selected.">".$row_retailers['title']."</option>";
    }
?>
</select>

<input type="text" name="slug" id="slug" value=""/>
         <script type="text/javascript">
        $(document).ready(function() {
        $('#retailer_id').on('change', function() {
var $selected = $('#retailer_id option:selected');
$('input[name=slug]').val($selected.data('slug'));
});
});
        </script>
2
  • So, retailer_slug is in the same table (cashbackengine_retailers)? Commented Nov 12, 2013 at 18:59
  • Yes, same table, just different collumn. So, when user select a retailer, i need to grab on the hidden input the retailer_slug, and then on click save, send the hidden input togheter with retailer_id Commented Nov 12, 2013 at 19:06

1 Answer 1

1

If I understand correctly, this is one thing you could do:

PHP

<select class="textbox2" id="retailer_id" name="retailer_id">
    <option value="">--- Please select store ---</option>
    <?php
        $sql_retailers = smart_mysql_query("SELECT * FROM cashbackengine_retailers WHERE status='active' ORDER BY title ASC");
        while ($row_retailers = mysql_fetch_array($sql_retailers))
        {
            if ($retailer_id == $row_retailers['retailer_id']) $selected = " selected=\"selected\""; else $selected = "";
            echo "<option data-slug=\"".$row_retailers['retail_slug']."\" value=\"".$row_retailers['retailer_id']."\"".$selected.">".$row_retailers['title']."</option>";
        }
    ?>
</select>
<input type="hidden" name="retailer_slug"/>

jQuery

$('#retailer_id').on('change', function() {
    var $selected = $('#retailer_id option:selected');
    $('input[name=retailer_slug]').val($selected.data('slug'));
});

So basically, the idea here is that you save the retailer_slug value as a data- attribute on each <option>. Then when the selected option is changed, the retailer_slug value is copied over to the hidden input.

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

6 Comments

I'll test it right now, lets see if solves the problem, i'll get in touch, thanks :)
Let me ask, how can i test, like, output on the page to check if the hidden input its beeing populated with the slug?
If you have Chrome or Firefox you can right-click on use "Inspect Element." Or you could change the input type to text and then it'll be visible.
Just did and returned that error: Uncaught TypeError: Object #<Object> has no method 'on'
Ok, changed to $('#retailer_id').change(function() { var $selected = $('#retailer_id option:selected'); $('input[name=slug_title]').val($selected.data('slug')); }); But still dont return the slug value, what i'm doing wrong?
|

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.