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

?>

here in the above code I have lastname string in $lastn and passing this as hidden variable to another page.

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

I'm trying to catch the hidden variable using above code but only "string" is getting displayed but not the last name which i'm getting from previous page.

3 Answers3

1

If you are trying to persist the value across page loads without having to resubmit a form, you will need to use sessions, like so:

page1.php

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

page2.php

$lastn = $_SESSION['lastn'];

Cheers

Zhube
  • 504
  • 2
  • 6
0

This is no valid HTML. Try:

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

And don't forget the form around the fields.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • still the same result. I placed everything in a form. –  Nov 12 '13 at 23:31
  • You can test if the POST isset when you make a var_dump($_POST) or print_r($_POST). Test it on the new site if the POST is set and you values are set in it. – René Höhle Nov 12 '13 at 23:32
0

you need to change your first page to:

$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>';

then open this page in browser and you will see form with button, after pressing button data will be posted to nextpage.php (rename it) and you will see value

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57