0
<?php
$hostname = "****";   //name of the server
$username = "***";   //id of the user
$password = "****"; //password of the user
$db = "**";          //name of the database

//connect to the database
$conn = mysql_connect($hostname,$username,$password);
if (!$conn) {
die("Could not connect: " . mysql_error());
}

//select the database
$db_selected = mysql_select_db($db, $conn);
if (!$db_selected) {
die ("Can\"t use " . $db . mysql_error());
}
echo "<form action='Assign6next.php' method=\"post\">";
$sql= "SELECT * FROM Author"; //This selects all columns from author

echo "Author List"; //Echo name
echo "<br>";
$result= mysql_query($sql, $conn);        //The result of the query in database
echo '<select name="AuthorName"><OPTION>';//Creates dropdown for Author dropdown
echo "Select an Author</OPTION>";         //
echo "<br>";
while ($row = mysql_fetch_array($result)){ //While in the loop return results in row
$last= $row["AuthorLast"];                 //Create variable last for Last Names
$first = $row["AuthorFirst"];              //Create variables for first Names
echo "<OPTION value=$last,$first>$last,$first</OPTION>"; 
} 
echo '</SELECT>';                             //End of drop down list
echo "<br>";

echo "<INPUT type='submit' value='Get Titles' name='submit'>";

if (isset($_POST['submit'])) 
{ 
//echo "button 1 has been pressed"; 
$auth = $_POST['AuthorName'];
//echo $auth;
//echo "<br>";
//$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($lastn, $firstn) = explode(",", $auth);
echo "<input type='hidden' name='lname' value=$lastn>";
//echo "</INPUT>";
//echo '<input type="hidden" name="lname" value="'.$lastn.'">';
//echo '<input type="hidden" name="lname" value="' . htmlspecialchars($lastn) . '">';
//$_COOKIE['lname'] = $lastn;

//echo $lastn; 
//echo "<br>";// foo
//echo $firstn; // *
} 

echo "</form>";

///////
//close the connection to MySQL
mysql_close($conn);

?>

在上面的代码中,我在 $lastn 中有姓氏字符串,并将其作为隐藏变量传递给另一个页面。

<?php
$lastn = $_POST['lname'];
echo $lastn;
echo "string";
?>

我正在尝试使用上面的代码捕获隐藏变量,但只显示“字符串”,而不是我从上一页获得的姓氏。

4

3 回答 3

1

如果您尝试在页面加载期间保留该值而无需重新提交表单,则需要使用会话,如下所示:

page1.php

session_start();
$lastn = 'something';
$_SESSION['lastn'] = $lastn;

page2.php

$lastn = $_SESSION['lastn'];

干杯

于 2013-11-12T23:30:01.660 回答
0

您需要将第一页更改为:

$auth = $_POST['AuthorName'];
list($lastn, $firstn) = explode(",", $auth);
echo '<form method="post" action="nextpage.php">';
echo '<input type="hidden" name="lname" value="' . htmlspecialchars($lastn) . '">';
echo '<input type="submit">';
echo '</form>';

然后在浏览器中打开此页面,您将看到带有按钮的表单,按下按钮后数据将发布到 nextpage.php(重命名),您将看到值

于 2013-11-12T23:28:19.693 回答
0

这不是有效的 HTML。尝试:

echo '<input type="hidden" name="lname" value="'.htmlspecialchars($lastn).'">';

并且不要忘记字段周围的表格。

于 2013-11-12T23:25:55.593 回答