比较JavaScript和PHP变量

时间:2015-08-15 16:30:09

标签: javascript php

我需要将javascript变量与php变量进行比较。

var other = <?php echo $element; ?>;
        if(exam == other){

这是比较这些变量的正确方法吗?它不起作用,但也许问题是别的。 该代码用于下面的函数excamTables(),

function examTables(){
    var exam = this.value;
    console.log(exam);
    var resultArray = [];
    <?php foreach($exams as $element): ?>
    var other = <?php echo $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];/*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);
}

var checkboxes = document.getElementsByClassName('year');
    for(var index in checkboxes){
        //bind event to each checkbox
        checkboxes[index].onchange = examTables;
    }

这篇文章post对此进行了讨论并使用了

echo json_encode($element);

所以我试过了,但它没有用,

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);
}

谢谢,

2 个答案:

答案 0 :(得分:0)

如果你想比较两个JSON对象,你应该考虑用公共属性值来评估相等性,否则你必须JSON.stringify它们两者并比较结果。

<强>慢

<?php $element = json_encode($anObject); ?>
var other = <?php echo $element; ?>;
if(JSON.stringify(exam) == JSON.stringify(other)) {
    // ...
}

<强>快速

<?php $element = json_encode($anObject); ?>
var other = <?php echo $element; ?>;
if(exam.prop == other.prop) {
    // ...
}

另一件事:您应该小心如何回显PHP中的值,然后由JavaScript解释。例如:

这个PHP ...

$element = "elementValue";
var other = <?php echo $element; ?>;

...生成此JavaScript

var other = elementValue;

如您所见,PHP只是将字符串“elementValue”插入到页面中。但是当JavaScript来解释表达式var other = elementValue;时,它会期望elementValue是一个预定义的变量。为了避免这种情况,请在引号中包含$element在PHP中的值,因此我们有:

这个PHP ...

$element = "elementValue";
var other = "<? echo $element; ?>";

...生成此JavaScript

var other = "elementValue";

答案 1 :(得分:0)

使用

var other = "<?php echo $element; ?>";

而不是

var other = <?php echo $element; ?>;

要检查您是否在其他地方获得$ element的值,请使用alert(other);