Skip to content Skip to sidebar Skip to footer

How To Pass A Value From Javascript Function?

Possible Duplicate: How to pass JS variable to php? Passing javascript variables to php? how to pass a valu from javascript function, I have this kind of code function next(c,q)

Solution 1:

To communicate between javascript (which runs on the client machine's browser) and PHP (which runs on your server) you need to use ajax. Since you are already using jQuery, I suggest using their abstraction method $.ajax(). It would look something like this:

// post value of #progressbar id to my php page
$.ajax({
    url: myPHPPage.php,
    data: JSON.stringify({ progressbarID: '#progressbar' }),
    success: function (dataFromServer) {
        alert('it worked!');
    },
    error: function (jqXHR) {
        alert('something went horribly wrong!');
    }
});

Solution 2:

Try this:

$.ajax({
    type: "GET",
    url: "yourphpfile.php",
    data: "texta=" + texta+ "&content=" + content// texta,contentare javascript variablessuccess: function(response){
        if(response != '') {
            //success  do something
        } else {
            // error
        }
    }   
}); 

Solution 3:

So you have two options. Javascript can only pass variables to PHP through ajax. This is because javascript runs on the client browser and PHP runs on the server.

Option 1 - use Ajax. Javascript:

//update progress bar
$.ajax({
   type: "POST",
   url: "some.php",
   data: { num: y } //or use q instead of y. its what you passed in
}).done(function(data) {
   $('#amount').text(data);
});

this is the php file

<?php//some.php$complete = $_POST['num'];
$progress = $complete / $total; //you'll have to set what "total" is.$progress .= '%';

echo$progress;
?>

Option 2 - use PHP when the page loads and then use javascript to update the progress bar. This is like using PHP and Javascript together, but technically you are using PHP to generate javascript code.

functionnext(c,q){
var y=Number(q);
var x=Number(c)+1;

var complete = (c / <?phpecho$total;?>);

//update progress bar//not sure how your progress bar library works.//but maybe like this:
$("#progressbar").progressbar({"value" : complete}); 
}

Post a Comment for "How To Pass A Value From Javascript Function?"