-1

completely new to PHP, so this is a complete first for me. Right now, I want to try dynamically update a drop down list from 1-10 based on a previous drop down list.

The first drop down list is a table selection, which allows you to choose tables 1-35, and the second drop down list is a seat selection from 1-10. I wish to have it so that 1-10 will update depending on the table to say who has booked the seats on the table already.

I've already found a way to populate the list properly, however I'm having some issues when I try to update it upon the first drop down changing.

I've researched lots of Ajax calls to different files of PHP, however my code is dependent on where the PHP is in the index.php file to populate the drop down. I have a function currently which populates the list when it firsts loads, and I have a small Ajax call towards an internal function inside the same file.

This is the PHP code roughly that fully works (There's a bit more that I left out as it's irrelevant)

<?php
    function populateDropdown2(){
        for ($i=1; $i<=10; $i++)
        {
            if ($row[1] == $_POST['TableNum'] && $row[2] == $i)
            {
                $result = $i . ' - ' . $row[0];                              
                break;
            }
        }
?>
<option value="
    <?php echo $result;?>
" 
    <?php if(strlen($result) > 2){echo "Disabled";}?>
>
    <?php echo $result;?>
</option>

And this is my attempt at the Ajax call:

$(document).ready(function(){
        $('#Tabl').change(function(){
            console.log("CLICKED");
            $.ajax({
                url: "index.php",
                type: "post",
                success: function(response){
                    populateDropDown2(response);
                }
            });
        });    
});

I've read quite a lot about Ajax, however is it possible to call a function that exists within the same file as it inside a different code block? Thanks.

Edit: The function I'm calling has to be in the position it is right now in the PHP file as it's just below all the other input fields for the form

Edit2: I'm getting my values from a CSV file for the PHP.

Nobody
  • 3
  • 3
  • Fast answer: Yes, you can. But you are not doing it the right way. You are trying to run php on your browser. You have to send to the php a parameter that you will check on you php (server) and if thats recived then you run the function. – Elanochecer Mar 25 '19 at 07:42
  • Thanks for the fast reply @Elanochecer. Just to clarify, you mean to send a variable to index.php, and then if that variable is true at any point run the function again? – Nobody Mar 25 '19 at 07:46
  • If that variable is set to whatever you want. Or if you want to run the function anytime the php is hit then just call the function. What do you mean by again? – Elanochecer Mar 25 '19 at 07:49
  • In the code I run it initially just to generate the first dropdown list and populate it for table one. Whenever the table number is changed though, I should have Ajax send a variable into index.php, and somewhere in my code checking for that variable, and if it's equal to true then I should run my PHP Function? Sorry, compete beginner and I'm still wrapping my head around server side PHP. – Nobody Mar 25 '19 at 07:52

1 Answers1

0

PHP and JavaScript (your AJAX function) are entirely separate and you can not directly call PHP from within JavaScript:

  • PHP: Runs on the web server and returns data via HTTP.
  • JavaScript: Runs locally in the client's browser.

Your AJAX call will have to call specific PHP code, which will then return data to JavaScript, which you can then use to modify the HTML currently being displayed.

If you create HTML in your PHP code, which is then returned to your JavaScript function, you can inject it into your DOM with $.html().

Preferably you'd want to return the data of interest as JSON (see json_encode()) tho and then use that to manipulate your DOM in JavaScript accordingly (see JSON.parse()).

Be sure to return your JSON wrapped into an object instead of arrays for security, see this post regarding the vulnerability.

Sven Mich
  • 227
  • 2
  • 9