0

really struggling to pass a JS variable to PHP variable. I understand that this cannot be done without passing from client to server and so I have tried to pass the variable via an AJAX call.

The code does not seem to be received by the PHP file as the passed variable does not echo after the AJAX call.

Here is my code although the most important element is right at the end. I am trying to pass JS variable 'chosenStudent' back to the PHP file.

            $Content3 = <<<EOD

            <form id="myGroupSelectForm" method="post">
              <select id="selectGroup">
                <option>Choose a Group</option>
              </select>
                <select id="selectStudent">
                <option>Choose a Student</option>
              </select>
            </form>

            <script type="text/javascript">

            var select = document.getElementById("selectGroup"); 
            var options = {$js_array_leadersGroupsName}; 
            var i;

            for(i = 0; i < options.length; i++) {
                var opt = options[i];
                var el = document.createElement("option");
                el.textContent = opt;
                el.value = opt;
                select.appendChild(el);
            }

            </script>

            <script>

            var studentList = {$js_array_students_lists}; 
            var select2 = document.getElementById("selectStudent");

            var a = document.getElementById('selectGroup');
            a.addEventListener('change', function() {

                var i;
                for(i = 0; i < options.length; i++) {
                        if ((this.value) == options[i]) {
                            var chosenStudentList = studentList[i];
                        }
                    }

                var select = document.getElementById("selectStudent");
                var length = select.options.length;
                for (i = length-1; i >= 0; i--) {
                  select.options[i] = null;
                }

                var i;
                for(i = 0; i < chosenStudentList.length; i++) {
                    var opt = chosenStudentList[i][0];
                    var el = document.createElement("option");
                    el.textContent = opt;
                    el.value = opt;
                    select2.appendChild(el);
                }

                }, false);

                var b = document.getElementById('selectStudent');
                b.addEventListener('change', function() {
                    var chosenSudent = this.value;
                    $.post('WickCustomLD.php', {variable: chosenSudent});
                    }, false);

            </script>



EOD;


            $Content3 .="\n";
            if(!empty($_POST['variable'])) {
               $chosenStudent = $_POST['variable'];
               echo "<br>";
               echo "<br>";
               echo "<br>";
               echo "<br>";
               echo $chosenStudent;
            }

            return $Content3;  

If anyone can help I would be very grateful.

6
  • I'm confused, is this all in one file, exactly as is, or are there multiple files? I assume the latter because of the return but it would help to show that. Commented Jun 12, 2020 at 13:26
  • You are returning $Content3 before you access the variable. Move the fragment at the end to the top of your file, or try to perform the AJAX request to a different PHP file altogether where you access the variable. Commented Jun 12, 2020 at 13:29
  • OK, that is certainly I stupid mistake on my part. As the request was under the return statement it was not executed. However, moving it higher produces a variable undefined message. Commented Jun 12, 2020 at 13:35
  • @ChrisHaas yes this is actually one PHP file. For now I would like it keep it that way. Commented Jun 12, 2020 at 13:35
  • I'm still not sure what you are trying to do. Are you trying to create a page that has some HTML and JS on it, and when someone does something with the form (submits, changes something, doesn't really matter what) the data gets sent to your PHP files to interpret? Commented Jun 12, 2020 at 14:17

1 Answer 1

1

This is what is happening:

  1. You type a URL into the browser's address bar to make a request to the server
  2. The server runs a PHP program to respond to that request
  3. The browser loads the page in that response and executes the JS in it
  4. The JS makes an Ajax request to a URL
  5. The server runs a PHP program to respond to that request
  6. The browser makes the response available to the JavaScript
  7. Your JavaScript ignores it

The request from step 4 doesn't travel back in time and make data available to the execution of the PHP program at step 2, not even if the URLs are the same.

If you want to load a whole new page and display it in the browser window, then don't use Ajax. Use a regular form submission.

If you want to use Ajax, then design your API so that it returns only the relevant data (and typically you would return JSON), and then write JavaScript to do something with the response to the request. You said you wanted to keep everything in one PHP file, but it is easier to manage code when you seperate things out into discrete units.

$.post('WickCustomLD.php', {variable: chosenSudent}).then(function (data) {
    // do something with the data
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much @Quentin for your help. Thanks to you I am beginning to understand the issue. I personally don't want a new page to load, in this case I want a graph to be rendered in JavaScript based on the userID of the selected student. So, I suppose the best way around this is to add the JS code to render the graph in this same JS script. I will give this a go and yes, I absolutely should be separating my code out. You are right! Thanks again!!
On second thoughts, I do need the chosenStudent to be sent back to the PHP script. Hmm...
Ok, maybe a solution is to do drop down lists in PHP?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.