2012-02-20 27 views
0

我有一個自動完成組合框,其中包含供應商列表,當選擇一個項目時,它會將該ID存儲在會話和隱藏字段中。如何使用Jquery和PHP分配會話變量?

這裏是我當前的代碼:

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

此外,如果你能就如何使用jQuery回發,因爲我可能會在我試圖創建應用程序的下一部分使用它的例子。

先生/女士您的答案將是非常有益的,非常感謝。

回答

1

我明白你想要達到什麼目的,但究竟哪裏會卡住?你不知道如何在PHP中分配會話變量?

會話通過php存儲在服務器級別,jquery無法訪問它們(儘管javascript可以訪問cookie)。因此,只需發送表單並使用$_SESSION var

保存數據在組合框中的change事件中,您應該執行兩項操作。首先創建一個隱藏字段來添加值,然後將值發送到一個PHP腳本,您可以在其中保存會話變量;

添加隱藏字段:

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

將數據發送到您的會話保存腳本

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

最後在save_session_var.php:

$_SESSION['supplier_id'] = $_GET['supplier_id']; 
+0

在PHP我可以寫類似$ _SESSION ['name'] =「randel」,現在我想要做的是將自動完成組合框中選擇的id或值存儲在$ _SESSION ['Suppl ierID'] =所選值,同時將值存儲在隱藏字段中。謝謝您的即時答覆,先生。 – 2012-02-20 11:25:39

+0

啊現在我明白你的問題了:)稍後我會編輯我的答案 – giorgio 2012-02-20 11:31:05

+0

由於某種原因,它不工作,我創建了php文件並將$ .get放在腳本的change事件中。 – 2012-02-20 14:52:04