javascript - When I click on li tag, it get data-value, but when I insert into it has null value
I get the li tag data-value when I on click. Now I need use the value insert back into the database but it shows null value in my database. But I use console.log it shows value.
How to get the value and insert into database?
mymoviemanagement.php :
$movie has value null
if (isset($_POST["btn_adddate"])) {
$s_date2 = $_POST['s_date2'];
$movie = $_POST['movie_id'];
$asd = "sdsd";
if($stmt = $conn->prepare("INSERT INTO movie_date (date,movie_id) VALUES (?,?)")) {
$stmt->bind_param("si", $s_date2, $movie );
$stmt->execute();
$stmt->close();
echo "successful adddate";
echo $movie;
echo $asd;
}
else{
echo "fail adddate";
}
}
Javascript inside mymoviemanagement.php:
<script>
$(".ul_movie2").on('click', 'li', function () {
var movie_id = $(this).attr('data-value');
console.log(movie_id)
if(movie_id !== "") {
$.ajax({
url:"mymoviemanagement.php",
data:{movie_id:movie_id},
type:'POST',
success:function(response) {
}
});
}
});
</script>
html:
<form action="mymoviemanagement.php" name="adddate" role="form" method="post" id="adddate" >
<div id="selectmovie2" data-toggle="dropdown" tabindex="0" ><span >Select Movie</span>
<ul id="m_movie" >
<?php
$sql = "SELECT DISTINCT movie.id,movie.name, movie.image
FROM movie
INNER JOIN movie_genre
ON movie.id = movie_genre.movie_id
INNER JOIN genre ON genre.id = movie_genre.genre_id";
$res = mysqli_query($conn, $sql);
if(mysqli_num_rows($res) > 0) {
while($row = mysqli_fetch_object($res)) {
echo '<li data-value="' . $row->id . '" ><img src="' . $row->image . '" >' . $row->name . '</li>';
}
}
?>
</ul>
</div>
</form>
Answer
Solution:
I guess the problem might be that your
move_id
databse column expects an INTEGER while you are sending a STRING type through the AJAX request.Can you post here your HTML code, because without it, we don't have a complete information about your situation...
Answer
Solution:
Actually I got the value.
But I should change:
into
So at PHP:
will get the value.
I gt mistake myself, just need addon
name="result"
at input tag. So only can get the value$_POST['result']
.