如何使用Jquery和PHP分配会话变量?

如何使用Jquery和PHP分配会话变量?,php,jquery,Php,Jquery,我有一个自动完成的组合框,其中包含供应商列表,当选择一个项目时,它会将ID存储在会话和隐藏字段中 以下是我当前的代码: <script> (function( $ ) { $.widget( "ui.combobox", { _create: function() { var self = this, select =

我有一个自动完成的组合框,其中包含供应商列表,当选择一个项目时,它会将ID存储在会话和隐藏字段中

以下是我当前的代码:

    <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>

(函数($){
$.widget(“ui.combobox”{
_创建:函数(){
var self=这个,
select=this.element.hide(),
selected=select.children(“:selected”),
value=selected.val()?selected.text():“”;
var input=this.input=$(“”)
.insertAfter(选择)
.val(值)
.自动完成({
延迟:0,
最小长度:0,
来源:功能(请求、响应){
var matcher=newregexp($.ui.autocomplete.escapeRegex(request.term),“i”);
响应(select.children(“option”).map(函数(){
var text=$(this.text();
if(this.value&(!request.term | | matcher.test(text)))
返回{
标签:text.replace(
新正则表达式(
(?![^&;]+;)(?!)(?![^&;]+;),“gi”
)“$1”,
值:文本,
选项:这个
};
}) );
},
选择:功能(事件、用户界面){
ui.item.option.selected=true;
自触发(“选定”,事件{
项目:ui.item.option
});
},
更改:功能(事件、用户界面){
如果(!ui.item){
var matcher=new RegExp(“^”+$.ui.autocomplete.escapeRegex($(this.val())+“$”,“i”),
有效=错误;
select.children(“选项”).each(函数(){
if($(this).text().match(matcher)){
this.selected=valid=true;
返回false;
}
});
如果(!有效){
//删除无效值,因为它与任何内容都不匹配
$(此).val(“”);
选择.val(“”);
输入数据(“自动完成”)。术语=”;
返回false;
}
}
}
})
.addClass(“ui小部件ui小部件内容ui左角”);
输入数据(“自动完成”)。\u renderItem=功能(ul,项目){
返回$(“
  • ”) .data(“item.autocomplete”,item) .append(“+item.label+”) .附录(ul); }; this.button=$(“”) .attr(“tabIndex”,-1) .attr(“标题”、“显示所有项目”) .insertAfter(输入) .按钮({ 图标:{ 主要:“ui-icon-triangle-1-s” }, 文本:false }) .removeClass(“用户界面角全部”) .addClass(“ui右角ui按钮图标”) 。单击(函数(){ //如果已经可见,请关闭 如果(input.autocomplete(“小部件”)为(“:可见”)){ 输入。自动完成(“关闭”); 返回; } //解决bug(可能与#5265的原因相同) $(this.blur(); //将空字符串作为要搜索的值传递,显示所有结果 input.autocomplete(“搜索”和“); input.focus(); }); }, 销毁:函数(){ this.input.remove(); 此.button.remove(); this.element.show(); $.Widget.prototype.destroy.call(this); } }); })(jQuery); $(函数(){ $(“#组合框”).combobox(); $(“#切换”)。单击(函数(){ $(“#组合框”).toggle(); }); }); 选择供应商: 项目数量:

    $('input[placeholder],textarea[placeholder]).placeholder();
    另外,如果您能举例说明如何使用jquery进行回发,因为我可能会在我尝试创建的应用程序的下一部分中使用它


    先生/女士,您的回答将非常有帮助,我们将不胜感激。

    我理解您的目标,但您到底在哪里遇到了困难?你不知道h吗
    $('#yourform').append('<input type="hidden" name="supplier_id" value="' + $(this).val + '" />'); // add hidden field
    
    $.get('save_session_var.php', {supplier_id: $(this).val});
    
    $_SESSION['supplier_id'] = $_GET['supplier_id'];