【问题标题】:Issue passing variables from one section to another PHP将变量从一个部分传递到另一个 PHP 的问题
【发布时间】:2020-09-22 06:10:31
【问题描述】:

我这里有这段代码。它的作用是: 首先,用户必须提交一个index.html 表单,这样做意味着他必须用他的分数完成所有输入(例如80/100 成绩)。之后,用户按下提交按钮,如您所见,我有一个//foreach statement 在表格上显示他们的信息.. 效果很好.. 但是今天我还添加了一个按钮来将此表格信息转换为 document.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> 所以这是我的代码:

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

});

任何建议我做错了什么?谢谢

【问题讨论】:

  • 添加此代码时,您实际上并没有告诉我们出了什么问题
  • 我更新了我上面的问题

标签: php html html-table


【解决方案1】:

试试:

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

【讨论】:

  • 不敢相信我错过了那个:P 你是对的,但现在脚本不起作用,不知道为什么.. 这是另一项任务,无论如何你找到了我要求的内容跨度>
  • 乐于助人。可能是错误来自 $_SESSION['score']$_SESSION['score1'] 的不同变量名。
猜你喜欢
  • 1970-01-01
  • 2018-01-14
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多