简体   繁体   中英

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

Here is my Javascript for fetching value to input box,

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

I want this value to use for SQL query in modal window which is on same page. I fetched value in modal window but unable to use it. What the format to use it.

You can pass js variable into php page using ajax.

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

    });  

});

No you can get this variable into your page.php (php page) using

$fromjs=$_GET['varabletophp'];
echo $fromjs;

you can use input hidden and set userid value in on this input so , post form

Varying modal content based on trigger button : 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) {

            }
        });

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