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 ?
asked Mar 25, 2016 at 13:31
Himanshu Upadhyay
7437 silver badges31 bronze badges
2 Answers 2
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;
?>
Sign up to request clarification or add additional context in comments.
Comments
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 ;
?>
Comments
default
contentDetails.duration. You can echo that anywhere you need the video length.