我如何通过JavaScript变量值在php使用模态窗口的同一页面


How can I pass JavaScript variable value in php with same page using modal window

这是我的Javascript获取值输入框,

$('#edituserModal').on('show.bs.modal', function(e) {
    var userid = $(e.relatedTarget).data('userid');
    var u_id = document.getElementById('hdn_user_id').value = userid;
    alert(userid);
});

我想要这个值用于SQL查询在模态窗口这是在同一页上。我在模态窗口中获取值,但无法使用它。使用什么格式

您可以使用ajax将js变量传递到php页面。

$('#edituserModal').on('show.bs.modal', function(e) {
var userid = $(e.relatedTarget).data('userid');
var u_id=document.getElementById('hdn_user_id').value=userid;
 $.ajax({    //create an ajax request to load page.php
        type: "GET",
        url: "page.php",
        data:"varabletophp="+u_id,    //Here is the value you wish to pass in to php page        
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
          alert(response);
        }
    });  
});

不,你可以使用

将这个变量放入你的page.php (php page)中
$fromjs=$_GET['varabletophp'];
echo $fromjs;

您可以使用隐藏输入并设置userid值在这个输入所以,post表单

根据触发按钮改变模态内容:http://getbootstrap.com/javascript/modals-related-target

        var data = new FormData($('form#' + formId)[0]);
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            cache: false,
            processData: false,
            contentType: false,
            beforeSend: function () {
            },
            success: function (response) {

            },
            error: function (response) {
            }
        });