[英]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.