Howto set a php variable equal to a JS variable

Hi,
How can I set a PHP variable equal to a JS variable?

function examTables(){
    var exam = this.value;
    var resultArray = [];
    examType = <?php echo json_encode($exams, JSON_PRETTY_PRINT); ?>;
    examType.foreach(function(item){
        if(exam == item){
            <?php $element ?> = item;
            resultArray = <?php echo json_encode($subs[$element], JSON_PRETTY_PRINT); ?>;
        }
    });

Maybe I am doing this a very long way around by converting a php array $exams to a JS object examType only to use the elements of that object and try to convert them back to php.
I think I should try to put the JS inside the php.
Perhaps it should work this way,

var exam = this.value;
    var resultArray = [];
    <?php foreach($exams as $element): ?>
        if(exam == <?php $element ?>){
            resultArray = <?php echo json_encode($subs[$element], JSON_PRETTY_PRINT); ?>;
        }
    <?php endforeach; ?>
    });

Thanks

I tried improving the function using json_encode() but code doesn’t seem to be able to compare the two variables.

function examTables(){
    var exam = this.value;
    console.log(exam);
    var resultArray = [];
    <?php foreach($exams as $element): ?>
    var other = <?php echo json_encode($element); ?>;
        if(exam == other){
            resultArray = <?php echo json_encode($subs[$element], JSON_PRETTY_PRINT); ?>;
        }else{
            resultArray = <?php echo json_encode($subs['AP'], JSON_PRETTY_PRINT); ?>;
        }
    <?php endforeach; ?>

        var strOption = "";
        strOption = "<table>";
        var keys1 = Object.keys(resultArray);
            keys1.forEach(function(item1){
                var keys2 = Object.keys(resultArray[item1]);
                keys2.forEach(function(items2){
                    var keys3 = Object.keys(resultArray[item1][items2]);
                        value = resultArray[item1][items2];/*Need to not repeat subjects. This should be done in php file when selecting from DB*/
                        strOption += "<tr><td>" + value + "</td></tr>";
                });
            });
        strOption += "</table>";

    var table = document.getElementById("examTables");
    var row = document.createElement("tr");
    var cell = document.createElement("td");
    cell.innerHTML = strOption;
    row.appendChild(cell);
    table.appendChild(row);
}

Thanks

Got it, the key line is,

    other = <?php echo json_encode($element);?>;

The full function is,

function examTables(){
    var exam = this.value;
    var resultArray = [];
    <?php foreach($exams as $element): ?>
    other = <?php echo json_encode($element);?>;
    if(exam == other)
    {
         resultArray = <?php echo json_encode($subs[$element], JSON_PRETTY_PRINT); ?>;
    }    
    <?php endforeach; ?>
        var strOption = "";
        strOption = "<table>";
        var keys1 = Object.keys(resultArray);
            keys1.forEach(function(item1){
                var keys2 = Object.keys(resultArray[item1]);
                keys2.forEach(function(items2){
                    var keys3 = Object.keys(resultArray[item1][items2]);
                        value = resultArray[item1][items2];
                        strOption += "<tr><td>" + value + "</td></tr>";
                });
            });
        strOption += "</table>";
        console.log(rows);
    var table = document.getElementById("examTables");
    var row = document.createElement("tr");
    var cell = document.createElement("td");
    var tableinner = document.createElement("table");
    var rowinner = document.createElement("tr");
    var cellinner = document.createElement("td");
    cellinner.innerHTML = strOption;
    rowinner.appendChild(cellinner);
    tableinner.appendChild(rowinner);
    cell.appendChild(tableinner);
    row.appendChild(cell);
    table.appendChild(row);
    rows++;
}

Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.