0

I have a autocomplete combobox which holds the list of the suppliers and when an item is selected it will store the ID inside a session and a hidden field.

Here's my current code:

    <script>
        (function( $ ) {
            $.widget( "ui.combobox", {
                _create: function() {
                    var self = this,
                        select = this.element.hide(),
                        selected = select.children( ":selected" ),
                        value = selected.val() ? selected.text() : "";
                    var input = this.input = $( "<input>" )
                        .insertAfter( select )
                        .val( value )
                        .autocomplete({
                            delay: 0,
                            minLength: 0,
                            source: function( request, response ) {
                                var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
                                response( select.children( "option" ).map(function() {
                                    var text = $( this ).text();
                                    if ( this.value && ( !request.term || matcher.test(text) ) )
                                        return {
                                            label: text.replace(
                                                new RegExp(
                                                    "(?![^&;]+;)(?!<[^<>]*)(" +
                                                    $.ui.autocomplete.escapeRegex(request.term) +
                                                    ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                                ), "<strong>$1</strong>" ),
                                            value: text,
                                            option: this
                                        };
                                }) );
                            },
                            select: function( event, ui ) {
                                ui.item.option.selected = true;
                                self._trigger( "selected", event, {
                                    item: ui.item.option
                                });
                            },
                            change: function( event, ui ) {
                                if ( !ui.item ) {
                                    var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
                                        valid = false;
                                    select.children( "option" ).each(function() {
                                        if ( $( this ).text().match( matcher ) ) {
                                            this.selected = valid = true;
                                            return false;
                                        }
                                    });
                                    if ( !valid ) {
                                        // remove invalid value, as it didn't match anything
                                        $( this ).val( "" );
                                        select.val( "" );
                                        input.data( "autocomplete" ).term = "";
                                        return false;
                                    }
                                }
                            }
                        })
                        .addClass( "ui-widget ui-widget-content ui-corner-left" );

                    input.data( "autocomplete" )._renderItem = function( ul, item ) {
                        return $( "<li></li>" )
                            .data( "item.autocomplete", item )
                            .append( "<a>" + item.label + "</a>" )
                            .appendTo( ul );
                    };

                    this.button = $( "<button type='button'>&nbsp;</button>" )
                        .attr( "tabIndex", -1 )
                        .attr( "title", "Show All Items" )
                        .insertAfter( input )
                        .button({
                            icons: {
                                primary: "ui-icon-triangle-1-s"
                            },
                            text: false
                        })
                        .removeClass( "ui-corner-all" )
                        .addClass( "ui-corner-right ui-button-icon" )
                        .click(function() {
                            // close if already visible
                            if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
                                input.autocomplete( "close" );
                                return;
                            }

                            // work around a bug (likely same cause as #5265)
                            $( this ).blur();

                            // pass empty string as value to search for, displaying all results
                            input.autocomplete( "search", "" );
                            input.focus();
                        });
                },

                destroy: function() {
                    this.input.remove();
                    this.button.remove();
                    this.element.show();
                    $.Widget.prototype.destroy.call( this );
                }
            });
        })( jQuery );

        $(function() {
            $( "#combobox" ).combobox();
            $( "#toggle" ).click(function() {
                $( "#combobox" ).toggle();
            });
        });
        </script>




<form method="post">
        <p>Choose Supplier:<select name="SupplierDDL" id="combobox">
        <input type="hidden" name="SupplierId">
        <option value=""></option>

        <?php include 'loadSuppliers.php'; ?>
    </select><p> 

    <p>Number of Items:</label><input type="text" name="ItemsQty"/></p>
        <input name="SupplierSubmit" type="submit" value='Submit'>
        </form>



<script>
    $('input[placeholder], textarea[placeholder]').placeholder();
</script>

Also if you could give examples on how to postback using jquery because I might use it in the next part of the application that I'm trying to create.

Sir/Ma'am your answers would be of great help and be very much appreciated.

1 Answer 1

1

I understand what you're trying to achieve, but where exactly do you get stuck? Don't you know how to assign session variables in php?

Sessions are stored at server level by php, jquery can't access them (though javascript can access cookies). So just send your form and save the data using the $_SESSION var

In the change event on the combobox, you should do two things. First create a hidden field to add the value, second send the value to a php script where you save your session var;

Add hidden field:

$('#yourform').append('<input type="hidden" name="supplier_id" value="' + $(this).val + '" />'); // add hidden field

Send data to your session save script

$.get('save_session_var.php', {supplier_id: $(this).val});

And finally in save_session_var.php:

$_SESSION['supplier_id'] = $_GET['supplier_id'];
Sign up to request clarification or add additional context in comments.

6 Comments

In php I can write something like $_SESSION['name'] = "randel", now what I want to do is have the id or value selected in the autocomplete combobox stored in a $_SESSION['SupplierID'] = The value selected and at the same time store the value inside a hidden field. Thank you for your immediate reply sir.
ah now i understand your problem a bit better :) I'll edit my answer in a moment
For some reason it's not working, I created the php file and placed the $.get in the change event of the script.
could you please use developer tools in Chrome (shift+ctrl+i) to checkout if and if so what data is sent to the php file? check out the 'network' tab to check if something sent and the 'console' tab to checkout errors
$.get('save_session_var.php', {supplier_id: $(this).val}); gave a blank result in the console tab while $.get('save_session_var.php', {supplier_id: document.getElementById("comboxbox").value}); gave Uncaught TypeError: Cannot read property 'value' of null
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.