0

我有一个 PHP 页面,显示带有单选按钮的 MySQL 查询的总数。这是在一个表格里面。用户应该选择一个单选按钮来填写所有表格,然后总数将存储在数据库中,我想将所选单选按钮的总值传递到下一页并打印出来。

我知道这很容易,但我不能正确地提出解决方案。

我的 PHP 页面代码如下:

    <?php

   session_start();
   $Load=$_SESSION['login_user'];
   include('../connect.php');
   $sql= "Select name from student where ID='$Load'";
   $username = mysql_query($sql);
    $id=$_SESSION['login_user'];

                if (isset($_POST['submit']))

{  
  $v1 = $_POST['v1'];
  $v2 = intval($_POST['v2']);
  $v3 = intval($_POST['v3']);
  $v4 = intval($_POST['v4']);
  $total = $2 + $v3 + $v4 ;

 mysql_query("INSERT into Form1 (ID,P1,P2,P3,TOTAL)
 values('$id','$v2','$v3','$v4','$total')") or die(mysql_error());
 header("Location: mark.php");
 }


?>


<html>

<head>

<?php


if(!isset($_SESSION['login_user']))

header("Location:index.html");



?>
  <title>Q&A Form</title>

</head>

<body>


    <center><form method="post" action="mark.php"  >

    <table style="width: 20%" >


<font size='4'>
    <table style="width: 70%">
        <tr>
<th > School Evaluation <font size="4" > </font></th>


<tr>
<th> Your attendance<font size="4" > </font></th>
<td>  <input type="radio" name ="v2" value = "4"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v2" value = "3"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v2" value = "2"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v2" value = "1"    onclick="updateTotal();" /></td>    
</tr>

<tr>
<th > Your grades  <font size="4" > </font></th>
<td>  <input type="radio" name ="v3" value = "4"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "3"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "2"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "1"    onclick="updateTotal();" /></td>    
</tr>

<tr>
<th >Your self-control <font size="4" > </font></th>
<td>  <input type="radio" name ="v4" value = "4"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v4" value = "3"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v4" value = "2"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v4" value = "1"    onclick="updateTotal();" /></td>    
</tr>       


        </tr>
    </table>


    <br>
   <td><input type="submit" name="submit" value="Submit">

    <input type="reset" name="clear" value="clear" style="width: 70px"></td>

 </form> 
</center>
</div>


</body>
</html>

当用户按下提交时,所有无线电输入将存储在数据库中,它将转到 mark.php 我想在此页面中打印出总数如何将总数传递到下一页?

4

1 回答 1

0

您的 PHP 脚本中可能有错误。

这个...

$total = $2 + $v3 + $v4 ;

应该

$total = $v2 + $v3 + $v4 ;

你的脚本有点混乱......这是我的重构建议:

<?php
session_start();
if (!isset($_SESSION['login_user'])) {
    // User is not logged in so we redirect it to the login page
    header("Location:index.html");
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>Q&A Form</title>
    <!-- (head stuff) --></head>
<body>
<?php

//User is logged, let's retrieve user information
$userID = $_SESSION['login_user'];

// I Assume this is your database handler helper
include('../connect.php'); 
$query = sprintf("SELECT name FROM student WHERE ID = '%s'", $userID);
$username = mysql_query($query);

/* MULTI STEP FORM */

if (isset($_POST['submit_laststep'])) {
    // User completed the multistep form
    // We insert data in the database

    // Insert data into database here (I don't know which data it is)
    // but you can retrieve the data accessing $_SESSION
    $v2 = $_SESSION['form_values']['step1']['v1'];
    $v3 = $_SESSION['form_values']['step1']['v2'];
    $v4 = $_SESSION['form_values']['step1']['v3'];
    $total = $_SESSION['form_values']['step1']['total'];

    $query = "INSERT into Form1 (ID,P1,P2,P3,TOTAL) values('$id','$v2','$v3','$v4','$total')";
    mysql_query($query) or die(mysql_error());

    // Print a thank you notice
    print "<span>Thank you $username for submitting this form</span>";

} else if (isset($_POST['submit_step1'])) {
    // User completed first step of form

    // Handle form values
    $v1 = $_POST['v1'];
    $v2 = intval($_POST['v2']);
    $v3 = intval($_POST['v3']);
    $v4 = intval($_POST['v4']);

    $_SESSION['form_values']['step1'] = array();
    // Save values for further steps
    // Mode 1 - Manual
    /*
    $_SESSION['form_values']['step1'] = array
    (
        'v1' => $v1,
        'v2' => $v2,
        'v3' => $v3,
        'v4' => $v4
    );
    */
    // This process can be form value agnostic, like this:
    // Mode 2 - More automatic (field agnostic, saves everything).
    foreach ($_POST as $k => $v) {
        $_SESSION['form_values']['step1'][$k] = $v;
    }

    // Calculate total
    $total = $v2 + $v3 + $v4 ;

    // save total to session too
    $_SESSION['form_values']['step1']['total'] = $total;

    // We print the sum of the radio buttons like you asked
    print "<span>Total from previous: $total</span>"; 
    include ('step2.form.html');

} else {
    //User has not completed step 1, so we show step 1 form
    include ('step1.form.html');
}
?>
</body>
</html>

形式:

表格 1:

<form id ="step1" method="post" action="">
    <table>
        <tr><th colspan="4">School Evaluation</th></tr>
        <tr><td colspan="4">Your attendance</td></tr>
        <tr>
            <td><input type="radio" name ="v2" value = "4"    onclick="updateTotal();" /></td>
            <td><input type="radio" name ="v2" value = "3"    onclick="updateTotal();" /></td>
            <td><input type="radio" name ="v2" value = "2"    onclick="updateTotal();" /></td>
            <td><input type="radio" name ="v2" value = "1"    onclick="updateTotal();" /></td>    
        </tr>

        <tr><td colspan="4">Your grades</td></tr>
        <tr>
            <td><input type="radio" name ="v3" value = "4"    onclick="updateTotal();" /></td>
            <td><input type="radio" name ="v3" value = "3"    onclick="updateTotal();" /></td>
            <td><input type="radio" name ="v3" value = "2"    onclick="updateTotal();" /></td>
            <td><input type="radio" name ="v3" value = "1"    onclick="updateTotal();" /></td>    
        </tr>

        <tr><td colspan="4">Your self-control</td></tr>
        <tr>
            <td><input type="radio" name ="v4" value = "4"    onclick="updateTotal();" /></td>
            <td><input type="radio" name ="v4" value = "3"    onclick="updateTotal();" /></td>
            <td><input type="radio" name ="v4" value = "2"    onclick="updateTotal();" /></td>
            <td><input type="radio" name ="v4" value = "1"    onclick="updateTotal();" /></td>    
        </tr>
        <tr>
            <td colspan="4">
                <input type="submit" name="submit_step1" value="Next">
                <input type="reset" name="clear" value="clear" style="width: 70px">
            </td>
        </tr>
    </table>
</form>

表单2示例!

<form id ="steplast" method="post" action="">
    <!-- form stuff -->
    <input type="submit" name="submit_steplast" value="Submit">
</form>
于 2012-11-12T14:49:44.023 回答