0

I am trying to pass a JavaScript variable to a PHP variable but I am not certain how this can be done. My JavaScript is within a EOD tag. In particular I want my PHP code to access the chosenStudent variable from the javascript code.

Here is my code:

            $Content3 = <<<EOD

            <form id="myGroupSelectForm">
              <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);

            </script>


            <script>

                var b = document.getElementById('selectStudent');
                b.addEventListener('change', function() {
                    var chosenSudent = this.value;
                    }, false);

            </script>

EOD;
            $Content3 .="\n";
            return $Content3;

Any pointers on how this could be done would be gratefully received.

****EDIT****

I have edited the code in an attempt to pass the variable to PHP via an AJAX call but I am still having issues.

            <script src="jquery-3.5.1.min.js">

                var b = document.getElementById('selectStudent');
                b.addEventListener('change', function() {
                    var chosenSudent = this.value;
                    }, false);


                $.post('WickCustomLD.php', {variable: chosenSudent});


            </script>



EOD;
            $Content3 .="\n";
            return $Content3;

            $chosenStudent = $_POST['variable'];
            echo $chosenStudent;

Again, any pointers to help me get this working would be gratefully receieved.

sw123456
  • 3,339
  • 1
  • 24
  • 42
  • 1
    The JS variable does not exist at the time your PHP code executes. You will need to make a new request from the client to the server, if you want to send anything from JS, to PHP. If you are not aware of such basics yet, then you should have a good, thorough read of https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming first of all. – CBroe Jun 12 '20 at 12:09

1 Answers1

0

you cannot pass JS variables to PHP since PHP is a server-side language and JS is a client-side language. your best option is: after running that JS code make an AJAX call to your server passing the chosenStudent variable to a PHP script

ddruganov
  • 1,263
  • 13
  • 15
  • Hi so, I have edited my code (see above) trying to add an AJAX call but I still cannot get this to work. Are you able to point me in the right direction? Thanks again for your help. – sw123456 Jun 12 '20 at 12:52
  • I looked at your edit and have a couple of tips: 1. put POST processing logic in the beginning of your WickCustomLD.php so that you don't process the whole file when POSTing and not GETting 2. in your JS you declare 'chosenStudent' inside of a function, that means that this variable doesn't exist outside of this function therefore you can't use it in jQuery's post method. 3. you place your 'echo variable' after 'return $content3', place it before that – ddruganov Jun 15 '20 at 06:42