Using this ajax code i am passing data speedMbps from my javascript to php. however when i tried to use the speedMbps in php i am getting a Undefined index: speedMbps. what should i do so i can use speedMbps in php?
Edited: video_id is send using html form post.
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {speedMbps: speedMbps },
cache: false
}).done(function( html ) {
$( "#speed" ).val( html );
});
php file
if(isset($_POST['video_id']) && ( $_POST['speedMbps'] )){
$speed = $_POST['speedMbps'];
if ($speed < 100) {
}
Try something like this :
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {"speedMbps" : speedMbps,"video_id":video_id },
cache: false
}).done(function( html ) {
$( "#speed" ).val( html );
});
try this:
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {speedMbps: "speedMbps" },
cache: false
}).done(function( html ) {
$( "#speed" ).val( html );
});
here you checking two varibales with AND
condition but passing only one variable. so you can change AND
condition to OR
and try it.
if(isset($_POST['video_id']) || isset( $_POST['speedMbps'] )){ //missing isset function here. also change AND to OR
Or you have to pass video_id also in ajax function like this:
data: {speedMbps: speedMbps, video_id:video_id },
you need to put speedMbps in single quotes.
data: {'speedMbps': speedMbps },
You either need to send video_id
with AJAX request or you can remove the condition in php and check since in php you are checking if video_id
is set or not, if(isset($_POST['video_id']) && ( $_POST['speedMbps'] )){
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {
speedMbps: speedMbps,
video_id: VIDEO_ID_PUT_HERE //`video_id: $('[name="video_id"').val()`
},
cache: false
}).done(function( html ) {
$( "#speed" ).val( html );
});
in your php:
if(isset($_POST['video_id']) && isset($_POST['speedMbps'] )){
$speed = $_POST['speedMbps'];
if ($speed < 100) {
//do your stuff
}
}
var speedMbps ='' //here is Your value for speedMbps
var videoid = //here is Your value for video id
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {speedMbps: speedMbps ,videoid:videoid},
cache: false,
success:(function( html ) {
$( "#speed" ).val( html );
});