简体   繁体   中英

how to retrieve a variable from jquery to php

In my php file I call a link with 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.

I tried to see if I can have the correct result with jquery so I used alert and it display the right value.

`//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 !

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"

$("#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

<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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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