prachi
prachi

Reputation: 39

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.

Upvotes: 0

Views: 3454

Answers (2)

Muhammad Shahabipour
Muhammad Shahabipour

Reputation: 26

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) {

            }
        });

Upvotes: 0

R. Sharma
R. Sharma

Reputation: 58

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;

Upvotes: 1

Related Questions