简体   繁体   English

如何从jquery检索变量到php

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

In my php file I call a link with jquery .在我的php文件中,我用jquery调用了一个链接。 That link contains a select list with an input text.该链接包含一个带有输入文本的选择列表。

$(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);

I want to retrieve the value of selectUser and mytext and put them in a database or php variable.我要检索的价值selectUsermytext并把它们放在一个数据库或php变量。

I tried to see if I can have the correct result with jquery so I used alert and it display the right value.我试着看看我是否可以用jquery得到正确的结果,所以我使用了 alert 并且它显示了正确的值。

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

What I want to do is to retrieve this value and to put it in a database and to retrieve the input text written by the user (I have no idea how to do that ? how to detect that user has filled the blank ?)我想要做的是检索此值并将其放入数据库并检索用户写入的输入文本(我不知道该怎么做?如何检测该用户是否已填充空白?)

The other problem is that I call the jquery link as many as the user want so it will be useful to put the value of selectUser and mytext in an array variable but I don't know how !另一个问题是,我所说的jquery链接多达用户要那么这将是把价值有用selectUsermytext阵列中的变量,但我不知道怎么办!

If someone can help me U will be grateful.如果有人可以帮助我,你将不胜感激。 THANKS A LOT !多谢 !

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

you can pick the value of select box and input like this .您可以选择选择框的值并像这样输入。 For select : Note : add id on select box ,for now i have added "selectUsers"对于选择:注意:在选择框上添加 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.

This is the jQuery ajax code这是 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>

this is the php function put it in your functions.php file这是放在你的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']);
             }

Hope this will resolve your issue希望这能解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM