【问题标题】:how to retrieve a variable from jquery to php如何从jquery检索变量到php
【发布时间】:2016-05-30 12:48:13
【问题描述】:

在我的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 的值放入数组变量中会很有用,但我不知道怎么做!

如果有人可以帮助我,你将不胜感激。非常感谢!

【问题讨论】:

  • 用户所做的事情发生在客户端,因此您需要将用户的数据发送回您的服务器。这可能以多种方式发生 - 最常见的是:提交表单、AJAX 调用、WebSocket。去选择你想要的,然后从那里开始。
  • 使用ajax在php中传递数据进行更新记录。

标签: javascript php jquery wordpress


【解决方案1】:

这是 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']);
             }

希望这能解决您的问题

【讨论】:

    【解决方案2】:
    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.
    

    【讨论】:

    • 尝试将文本移到代码块之外,这样更容易阅读,并且只将代码放在代码块中
    猜你喜欢
    • 2020-04-30
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 2019-01-01
    相关资源
    最近更新 更多