当我点击li标签时,它会获得数据值,但是当我插入它时它具有空值 [英] When I click on li tag, it get data-value, but when I insert into it has null value
问题描述
点击时,我获得了li标签数据值。现在我需要将值insert插回到数据库中,但它在我的数据库中显示null值。但我使用console.log显示值。
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:
mymoviemanagement.php :
$ movie的值为null
$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";
}
}
mymoviemanagement.php中的Javascript:
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" class="">
<div class="nice-select dropdown-toggle" id="selectmovie2" data-toggle="dropdown" tabindex="0" style="margin-left: 200px; margin-top: 29px;"><span class="current">Select Movie</span>
<ul class="dropdown-menu scrollable-menu ul_movie2" 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 . '"class="option" style="margin-top:10px; margin-bottom:10px;"><img src="' . $row->image . '" style="width:50px; height:80px; margin-right:10px;">' . $row->name . '</li>';
}
}
?>
</ul>
</div>
</form>
推荐答案
实际上我得到了价值。
但我应该改变:
<input type="text" value="" id="result">;
进入
<input type="text" value="" id="result" name="result">;
所以在PHP:
$movie = $_POST['result'];
将获得该值。
I gt错了我自己,只需要在输入标签处添加 name =result
。所以只能获得值 $ _ POST ['result']
。
I gt mistake myself, just need addon name="result"
at input tag. So only can get the value $_POST['result']
.
这篇关于当我点击li标签时,它会获得数据值,但是当我插入它时它具有空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!