简体   繁体   English

我可以在jquery中使用php变量吗

[英]Can I use php variable in jquery

In my existing code, I created a new to place the data from CSV file. 在现有代码中,我创建了一个新文件来放置CSV文件中的数据。 This works well. 这很好。 What I need to do is remove items from box1 and replace them from this code. 我需要做的是从box1中删除项目,然后从此代码中替换它们。 I can do this if I was using jquery, but not sure how to link my code with jquery. 如果使用的是jquery,则可以执行此操作,但不确定如何将代码与jquery链接。

Is it possible to use php variable with jquery? 是否可以在jQuery中使用php变量? You will see in the code that I have tried some js code to loop through var and get the values. 您将在代码中看到我尝试了一些js代码来遍历var并获取值。 However, This only shows 1 item and not all items. 但是,这仅显示1个项目,而不是所有项目。

So, in essence I need to remove the <select> and <option> from my existing PHP code and place the items into my existing <select> #box1. 因此,从本质上讲,我需要从现有的PHP代码中删除<select><option>并将这些项放入现有的<select> #box1中。 Many thanks 非常感谢

    <?php

    if (isset($_FILES['csvfile']) &&
        is_uploaded_file($_FILES['csvfile']['tmp_name']) &&
        $_FILES['csvfile']['size'] > 0) {
        echo "<h3>".
        "File ".$_FILES['filename']['name'].
        " uploaded successfully.".
        "</h3>";
        //get the csv file
        $file = $_FILES['csvfile']['tmp_name'];
        $handle = fopen($file, "r");

        //loop through the csv file
        echo '<select name="box" size="7" multiple="multiple">';
        while (($line = fgetcsv($handle, 1000, ',')) !== false) {
            foreach($line as $cell) {
                $clean = htmlspecialchars($cell);
                echo << < OPTION <
                    option value = "{$clean}" > {
                        $clean
                    } < /option>
                OPTION;
            }
        }
        echo "</select>";
    } else {
        echo 'no reults';
    }

    ?>
<select id="box1">
    <option></option>
</select>

<script>
    var data = <?php echo json_encode($clean); ?>;
    //var len = data.length;
    //console.log(len);

    for (var i=0; i < data.length; i++) {
        console.log(data);
    }
    console.log(data);
</script>

EDIT: Update code based on Alejandro answer 编辑:根据亚历杭德罗答案更新代码

<?php

if (isset($_FILES['csvfile']) && 
    is_uploaded_file($_FILES['csvfile']['tmp_name']) && 
    $_FILES['csvfile']['size'] > 0) {

    //get the csv file
    $file = $_FILES['csvfile']['tmp_name'];
    $handle = fopen($file,"r");

    //loop through the csv file

    //echo '<select name="box" size="7" multiple="multiple">';
        $file_lines = array(); // Global array
    while (($line = fgetcsv($handle, 1000, ',')) !== false) {
            $strings = array();
        foreach($line as $cell) {
            $clean = htmlspecialchars($cell);
            array_push($strings, $cell); // Add the string to the array
//            echo <<< OPTION
//                option value = "{$clean}" > {
//                    $clean
//                } < /option>
//            OPTION;
        }
            array_push($file_lines, $strings); // Add the line data to the global array
    }
    //echo "</select>";
}
else {
    echo 'no reults';
}
?>

<select id="box1">
    <option></option>
</select>

<script>
    var phpString = "<?php echo json_encode($file_lines); ?>";
    var data = JSON.parse(phpString);

    for (var i=0; i < data.length; i++) {
        console.log(data);
    }

</script>

You can, but the PHP output is a string, so you must enclose it in double quotes: 可以,但是PHP输出是字符串,因此必须将其用双引号引起来:

var phpString = "<?php echo json_encode($clean); ?>";

As it is a string, you have to parse it with JSON.parse() , so it becomes a Javascript object/array you can work with: 由于它是一个字符串,因此必须使用JSON.parse()进行解析,因此它成为可以使用的Javascript对象/数组:

var data = JSON.parse(phpString);

for ( var i = 0; i < data.length; i++ ) {
    console.log(data);
}

EDIT: $clean is a string, so there's no need to json_encode() it. 编辑: $clean是一个字符串,所以不需要json_encode() I worked the answer as if it was an array, so we'll create one (two actually: one array for the data of each line and another global one that will save the data of the whole file -aka array of arrays-). 我将答案当作一个数组来工作,因此我们将创建一个(实际上是两个:一个数组用于存储每一行​​的数据,另一个全局数组将保存整个文件的数据-aka数组数组-)。 Add an array just before your echo '<select...>'; echo '<select...>';之前添加一个数组echo '<select...>'; and we'll add every string read in the file to it: 然后将文件中读取的每个字符串添加到其中:

//loop through the csv file

    echo '<select name="box" size="7" multiple="multiple">';
    $file_lines = array(); // Global array
    while (($line = fgetcsv($handle, 1000, ',')) !== false) {
        $strings = array(); // Here, create an array for this particular line
        foreach($line as $cell) {
            $clean = htmlspecialchars($cell);
            array_push($strings, $cell); // Add the string to the array
            echo << < OPTION <
                option value = "{$clean}" > {
                    $clean
                } < /option>
            OPTION;
        }
        array_push($file_lines, $strings); // Add the line data to the global array
    }

Then below, instead of var phpString = "<?php echo json_encode($clean); ?>"; 然后在下面,而不是var phpString = "<?php echo json_encode($clean); ?>"; , you should use: ,您应该使用:

var phpString = "<?php echo json_encode($file_lines); ?>";

And the rest of the answer should work well. 其余的答案应该很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM