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