簡體   English   中英

如何從jquery檢索變量到php

[英]how to retrieve a variable from jquery to php

在我的php文件中,我用jquery調用了一個鏈接。 該鏈接包含一個帶有輸入文本的選擇列表。

$(document).ready(function() {

    $(add_button).click(function(e){ //on add input button click
        var linkUsers='<div id="selectUsers"><select name="selectUsers" ><option value="">Select a ...</option><option value="LASTNAME">LASTNAME</option><option value="FIRSTNAME">FIRSTNAME</option><option value="ZIPCODE">ZIPCODE</option><option value="EMAIL">EMAIL</option><option value="STATUS">STATUS</option><option value="LICENSE">LICENSE</option><option value="OTHER">OTHER</option></select><br><br> </div><div><input type="text" id="mytext" name="mytext"> <br><br>';

        $(wrapper).append(linkUsers);

我要檢索的價值selectUsermytext並把它們放在一個數據庫或php變量。

我試着看看我是否可以用jquery得到正確的結果,所以我使用了 alert 並且它顯示了正確的值。

`//when the user choose an option in the select list 
$('#selectUsers select').change(function(){
        tableselect = $(this).val() ;
        alert(tableselect[$i]);`

我想要做的是檢索此值並將其放入數據庫並檢索用戶寫入的輸入文本(我不知道該怎么做?如何檢測該用戶是否已填充空白?)

另一個問題是,我所說的jquery鏈接多達用戶要那么這將是把價值有用selectUsermytext陣列中的變量,但我不知道怎么辦!

如果有人可以幫助我,你將不勝感激。 多謝 !

Note : wrapper not specified as class or id ,replace according to that.i am assuming as id

您可以選擇選擇框的值並像這樣輸入。 對於選擇:注意:在選擇框上添加 id,現在我添加了“selectUsers”

$("#wrapper").on("change","#selectUsers",function(){
     alert($(this).val());
    });

For input : 



 $("#wrapper").on("keyup","#mytext",function(){
     alert($(this).val());
    });

then you can send this response by ajax to php file ands store into db.
you can change event according to need.
Hope this will help you.

這是 jQuery ajax 代碼

<script type="text/javascript">
jQuery(document).ready(function(){

    jQuery('#selectUsers select').change(function(){
        tableselect = jQuery(this).val() ;
            var data_pro = {
                    action: 'update_record',
                    tableselect: tableselect
                    };

                    jQuery.ajax({
                                type: "POST",
                                dataType : 'html',
                                url: ajaxurl,
                                data: data_pro,
                                  success: function(data){

                                    }
                            });

    });
    });
</script>

這是放在你的functions.php文件中的php函數

add_action('wp_ajax_update_record', 'update_record');
add_action('wp_ajax_nopriv_update_record', 'update_record');

         function local_pickup_shipping_options(){
         print_r($_POST['tableselect']);
             }

希望這能解決您的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM