javascript - When I click on li tag, it get data-value, but when I insert into it has null value

100

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>
833

Answer

Solution:

I guess the problem might be that yourmove_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...

997

Answer

Solution:

Actually I got the value.

But I should change:

<input type="text" value="" id="result">;

into

<input type="text" value="" id="result" name="result">;

So at PHP:

 $movie = $_POST['result'];

will get the value.

I gt mistake myself, just need addonname="result" at input tag. So only can get the value$_POST['result'].

People are also looking for solutions to the problem: Get variable data outside from the Switch Statement in PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.