簡體   English   中英

將變量從一個部分傳遞到另一個部分 PHP

[英]Issue passing variables from one section to another PHP

我這里有這段代碼。 它的作用是:首先用戶必須提交一個index.html表格,這樣做意味着他必須用他的分數完成所有輸入(例如 80/100 成績)。 之后,用戶按下提交按鈕,如您所見,我有一個//foreach statement來在表格上顯示他們的信息..這很好..但今天我還添加了一個按鈕來將此表格信息轉換為文檔。 xls(Excel)。 很確定我將 php 變量從一個部分傳遞到另一個 php 部分做錯了。 <table>也必須在<body> </body>之間,而不是像echo <table>否則腳本無法正常工作。


基本上我想要從第一個 php 部分獲取變量$name $score1 $score2等的正確方法,並將它們放置到<td> <?php $name = $_SESSION['name']; ?>`` </td> <td> <?php $score = $_SESSION['score']; ?> </td> <td> <?php $name = $_SESSION['name']; ?>`` </td> <td> <?php $score = $_SESSION['score']; ?> </td> <td> <?php $name = $_SESSION['name']; ?>`` </td> <td> <?php $score = $_SESSION['score']; ?> </td>所以這是我的代碼:

   <?php session_start();?>
<html>
<head>

</head>
</html>
<body>
<?php


function average(){

$name = $_POST['name'];
$score1 = $_POST['math'];
$score2 = $_POST['chemistry'];
$score3 = $_POST['history'];
$score4 = $_POST['english'];
$score5 = $_POST['language'];
$score6 = $_POST['religious'];
$score7 = $_POST['german'];
$score8 = $_POST['physics'];
$score9 = $_POST['latin'];
$score10 = $_POST['french'];
$score11 = $_POST['music'];

$study = array(
    "Mathimatics"=> $score1,
    "Chemistry"=> $score2,
    "Histroy"=> $score3,
    "English"=> $score4,
    "Language"=> $score5,
    "Religious"=> $score6,
    "German"=> $score7,
    "Physics"=> $score8,
    "Latin"=> $score9,
    "French"=> $score10,
    "Music"=> $score11
);


    $_SESSION['name'] = $name;
    $_SESSION['score1'] = $score1;



    //echo "<div>";
    //echo "<table class=\'tableToExl\'>";
    //echo "    <th>Projects</th>";
    //echo "    <th>Score</th>";

    //foreach ($study as $key=>$values){

    //echo "  <tr>";
    //echo "    <td>$key</td>";
    //echo "    <td>$values</td>";
    //echo "  </tr>";

    //}

}

    average();


?>
 <table class="tableToExl" data-tableName="Test Table 1">
     <th>Projects</th>
     <th>Score</th>
        <tr>
             <!-- For example i placed only the name of the student and the first Project-->
        <td> <?php  $name = $_SESSION['name']; ?> </td>
        <td> <?php  $score = $_SESSION['score']; ?> </td>
       </tr>
</table>

<button class="exportToExcel">Export to XLS</button>

<script>
    $(function() {
       $(".exportToExcel").click(function(e){
        var table = $(this).prev('.tableToExl');
        if(table && table.length){
            var preserveColors = (table.hasClass('table2excel_with_colors') ? true : false);
            $(table).table2excel({
                exclude: ".noExl",
                name: "Excel Document Name",
                filename: "myFileName" + new Date().toISOString().replace(/[\-\:\.]/g, "") + ".xls",
                fileext: ".xls",
                exclude_img: true,
                exclude_links: true,
                exclude_inputs: true,
                preserveColors: preserveColors
            });
        }
    });

});

任何建議我做錯了什么? 謝謝

嘗試:

<td> <?php  echo $_SESSION['name']; ?> </td>
<td> <?php  echo $_SESSION['score']; ?> </td>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM