Home  >  Q&A  >  body text

PHP AJAX not updating dynamically even though consolelog has correct value

I'm trying to update $_POST['category'] by clicking a button. I'm trying to achieve this using AJAX (for the first time), as read on several other pages here. My JS function currently updates the correct value in the console, but it cannot actually update the POST to the correct value, or at least that's what I assume. I need help trying to get the value of a POST so that I can use it as a value in PHP.

Main PHP File

<div class="subcategory text_markup_subtitle">
    <ul>
        <li class="subhead" value="mobiliteit" onClick="subCat(event)">Mobiliteit</li></a>
        <li class="subhead" value="kracht" onClick="subCat(event)">Kracht</li>
        <li class="subhead" value="uithouding" onClick="subCat(event)">Uithouding</li>
        <li class="subhead" value="stretching" onClick="subCat(event)">Stretching</li>
    </ul>
</div>

Contains PHP files

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<?php  
        $_POST['category'] = '';
        var_dump($_POST['category'])
?>

JS file

function cat(event){
    var values = $(event.target).attr('value')
    console.log(values);

    $.ajax({
        url: "Videos.php",
        type: "post",
        data: { category: values },
        success: function (response) {
            console.log('succes');
        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus, errorThrown);
        }
    });
}

P粉550257856P粉550257856173 days ago386

reply all(1)I'll reply

  • P粉136356287

    P粉1363562872024-04-03 14:25:06

    Solved the problem with the help of ADyson. (Thanks my man). I added the following code $("#showmessageID").show().HTML(result) so that the div with ID showmessageID updates every time in succession.

    The div on my main PHP file (containing videos.php) receives the ID showmessageID so that it updates every time the AJAX call succeeds.

    Main PHP File

    JS file

    $.ajax({
        url: "http://localhost/exercise/Videos.php",
        type: "post",
        data: { category: category_SQL },
        success: function (data) {
            $("#showmessageID").show().html(data)
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
        }
    });

    Video.php

    echo $_POST['category']

    reply
    0
  • Cancelreply