2012-11-12 42 views
0

我有一個PHP頁面,顯示單選按鈕的MySQL查詢總數。這是在一個表單內。用戶應該選擇一個單選按鈕來填寫所有表單,然後總數將被存儲在數據庫中,我想將選定的單選按鈕的總值傳遞到下一頁並打印出來。PHP - 將單選按鈕的總價值傳遞到下一頁

我知道這是一個容易的,但我不能正確的解決方案。

我對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我想打印出總的這個頁面如何將總數傳遞到下一頁?

+0

您可以在接下來的頁面上檢索數據庫中的值,或將其保存在'_SESSION' $ –

+0

或者你可以通過它的重定向。 header(「Location:mark.php?total = $ total」); – qooplmao

+0

你可以用代碼解釋嗎?所以我可以理解 – user1806136

回答

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> 
+0

它不起作用!我可以使用文章嗎? – user1806136