PHP - 无法将选择值传递给其他页面

时间:2013-08-08 04:18:45

标签: php

我有两个页面,一个通过POST将值传递给另一个页面。这些值不会从选择操作显示在第二页上。输入文本框传递其值。我已经包含了以下两个页面

第一页:     

mysql_connect("localhost","root","") or die("Unable to connect");
    mysql_select_db("testsite");

$sql = "SELECT state FROM states";
$result = mysql_query($sql);

$sqldegrees = "SELECT degree FROM degrees";
$resultdegrees = mysql_query($sqldegrees);

$sqlschools = "SELECT school FROM schools";
$resultschools = mysql_query($sqlschools);


echo "<html>";
echo "<head>";
echo "<link rel='stylesheet' type='text/css' href='styles.css'/>";
echo "</head>";
echo "<body>";
echo "<h3>Education</h3>";
echo "<form method='post' action='educationhistoryinsert.php'>";
echo "<table border='3' width='280'>";
echo "<tr><td>State:</td><td>";
echo "<select state='state'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['state'] ."'>" . $row['state'] ."</option>";}
echo "</select>";
echo "</td></tr>";
echo "<tr><td>School:</td><td>";
echo "<select school='school'>";
while ($row = mysql_fetch_array($resultschools)) {
echo "<option value='" . $row['school'] ."'>" . $row['school'] ."</option>";}
echo "</select>";
echo "</td></tr>";
echo "<tr><td>Degree:</td><td>";
echo "<select degree='degrees'>";
while ($row = mysql_fetch_array($resultdegrees)) {
echo "<option value='" . $row['degree'] ."'>" . $row['degree'] ."</option>";}
echo "</select>";
echo "</td></tr>";
echo "<tr><td>Subject:</td><td><input type='text' name='subject' maxlength='20'/></td>      
</tr>";
echo "</table>";
echo "<table border='1'>";
echo "<tr>";
echo "<td width='5'>Month:</td><td><input  style='width: 30px;' type='number'     
name='month' maxlength='1'/></td>";
echo "<td width='5'>Year:</td><td><input  style='width: 50px;' type='number'      
name='year' maxlength='5'/></td>";
echo "<td width='5'>GPA:</td><td><input  style='width: 30px;' type='number' name='gpa'    
maxlength='5'/></td>";
echo "</tr>";
echo "</table>";
echo "<input type='submit' value='Save'>";
echo "</form>";
echo "</body>";
echo "</html>";
?>

======= ------------------------------- 'educationhistoryinsert.php'

<?php
$degree = $_POST['degree'];
$subject = $_REQUEST['subject'];

echo $degree."<br/>";
?>

3 个答案:

答案 0 :(得分:2)

您需要更改您的选择名称 -

echo "<select state='state'>";
echo "<select school='school'>";
echo "<select degree='degrees'>";

echo "<select name='state'>";
echo "<select name='school'>";
echo "<select name='degrees'>";

$degree = $_POST['degree'];

缺少s

中的degrees
$degree = $_POST['degrees'];

答案 1 :(得分:0)

name提供给选择框,例如

echo "<select name='degree'>";
echo "<select name='school'>";

然后你可以通过POST来获取它们

$degree = $_POST['degree'];
$school = $_POST['school'];

答案 2 :(得分:0)

当您使用HTML(而不是XML)时,您需要提供默认标记属性,例如name="" *或使用 data-XYZ

其中 XYZ 是您的自定义属性。

因此,为了获得正确的结果,只需替换

  • state='state'name='state'
  • school='school'name ='school'
  • degree='degrees'name ='degrees'

然后它应该工作