• PHP Help
  • POST Variable in AJAX call not showing in db call

The id and value I'm trying to pass is lessonnum6 to AJAX url updatesaved.php. Do I need to specify something below in AJAX for a hidden field type?

The php code

<?echo '<input type="hidden" id="lessonnum6" value='.$a3['lessonid'].'>';?>
echo "<button type=\"submit\" id=\"updatestar6"\" class=\"star0\">";

AJAX Call

$("#updatestar6").click(function(){
                    var lessonnum6=$("#lessonnum6").val();
                    var static6=$("#static6").val();

                $.ajax({
                    url:'/updatesaved.php',
                    method:'POST',
                    data:{
                        lessonnum6:lessonnum6,
                        static6:static6

                    },
                    success:function(response){
                        if ($('#updatestar6').hasClass('star0')) {
                            $('#updatestar6').removeClass('star0').addClass('star');
                            }
                            alert(response);
                    }
                });
            });

Below is updatesaved.php. I cannot echo or read the post value for lessonnum6 and as a result the query field idlesson below is showing a 0 in db table.

updatesaved.php - db

<? if (!empty($_POST["lessonnum6"])) {
$p=$_POST["lessonnum6"];
} elseif (!empty($_POST["lessonnum7"])) {
$p.=$_POST["lessonnum7"];
} elseif (!empty($_POST["lessonnum8"])) {
$p.=$_POST["lessonnum8"];
} elseif (!empty($_POST["lessonnum9"])) {
$p.=$_POST["lessonnum9"];
} elseif (!empty($_POST["lessonnum10"])) {
$p.=$_POST["lessonnum10"];
} elseif (!empty($_POST["lessonnum11"])) {
$p.=$_POST["lessonnum11"];
}

$sql = "INSERT into steam_lessons_shared SET idlesson = '".$p."', memberid = '".$_SESSION['memberid']."', stage = 0";
//mysqli_query($con,$sql);
if($con->query($sql)===TRUE){
    echo "DATA updated";
}?>

    You're using the short PHP tags <? which are deprecated. That code might not be executing at all. I'd strongly suggest using the longer <?php tag instead.

    Also, it doesn't look like you have any quotes around the value of your lessonnum6 input:

    <?echo '<input type="hidden" id="lessonnum6" value='.$a3['lessonid'].'>';?>

    Your HTML input should have double quotes around its value. Also, $a3['lessonid'] might contain characters that break your HTML code. Maybe change that to this:

    <?php echo '<input type="hidden" id="lessonnum6" value="' .  htmlspecialchars($a3['lessonid']) . '"">'; ?>

    If you are still having trouble, you'll need to debut your PHP and/or Javascript to see what value is showing up in your HTML for lessonnum6.

      the lessonnum6 id value is still not appearing in my db table. The query is performing and executing as the session id has a recorded value within the row.

      The result of a query row after execution is: id:1 idlesson:0 memberid:82 stage:0

        I suggest you try looking at the HTML of your hidden input to see if there's a zero there or some value. I also suggest you look at the SQL that's getting executed to see what that looks like. Also, I see now that it looks like your hidden input has no name attribute. You might want to review the fundamentals of form inputs. The ID of a form input is not the same as its NAME. Your GET and POST values will be assigned using NAME.

          For further debugging you can actually look at the contents of the $_POST array to see what the server is receiving from the browser. Your browser will also have developer tools to allow seeing what the browser is sending to the server.

            I changed the AJAX call to 'GET' instead of POST and also changed the POSTS to GET in the database update page. That works.

              So your next challenge is to answer why.

                13 days later
                Write a Reply...