Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 743

use javascript variable in php in jquery function

I want to use javascript variable in php function.. my code is as below

$(document).ready(function() { 
    $("#btn_url").on('click', function() {
        var video_val = $("#video_url").val(); alert(video_val);
        alert('<?php echo getYoutubeDurationV3("<script>document.write(video_val);</script>"); ?>');
    });
 }); 

I want to use video_val variable in php function.. how can i do that ?

Upvotes: 4

Views: 504

Answers (2)

patel parth
patel parth

Reputation: 36

yes, u can do that by ajax post method..

$(document).ready(function() { 
$("#btn_url").on('click', function() {
    var video_val = $("#video_url").val(); alert(video_val);
   $.ajax({
      url: 'ajax_file.php',
      data: {duration: video_val},
      type: 'POST',
      success: function(data) {
          alert(data);
      }
   });
});
}); 

And in ajax_file.php write below code..

<?php
$duration = $_POST['video_val'];
return $duration ;
?>

Upvotes: 1

Jasper
Jasper

Reputation: 833

You can use Ajax for this solution. When you place your PHP getYoutubeDurationV3 function in a .php file.

$(document).ready(function() { 
$("#btn_url").on('click', function() {
    var video_val = $("#video_url").val(); alert(video_val);
   $.ajax({
      url: 'yourphpscript.php',
      data: {duration: video_val},
      type: 'POST',
      success: function(data) {
          alert(data);
      }
   });
});
}); 

And in your PHP file you can get the value like this:

<?php
$duration = $_POST['video_val'];

//rest of your logic, in your case your function

//return your response back to your webpage.
return $response;
?>

Upvotes: 2

Related Questions