简体   繁体   中英

Issue passing variables from one section to another PHP

i have this code right here. What it does is: First the user have to submit one index.html form,and by doing this it means that he have to complete all the inputs with his scores (for example 80/100 achievment). After that the user press the submit button and as you can see i have a //foreach statement to display their information on a table..that is working great.. but today i added also a button to convert this table information to document.xls(Excel). Pretty sure i am doing something wrong with passing the php variables from one section to another php section. Also the <table> has to be between <body> </body> and not like echo <table> otherwise the script is not working as far as i can say.


Basically i want the right way to get the variables $name $score1 $score2 etc. from the first php section and place them to the <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> So this is my code:

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

});

Any advise what i am doing wrong? thnx

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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