Jump to content

Transfer of variable value from page1 to page 2


gsashwin

Recommended Posts

Hi,

I am new to Php and having problems with redirecting of page particularly sending variables across pages.

I have this page1 which contains an if else statement.

If

{

User types in two text fields code should be executed and the page should be resubmitted to the same page with database results. For this reason I am using the form attribute of the page and directing the action variable in the form attribute to page1 ( this page itself) on submit.

Else

{

Now if user types in only one text field, it should go to another page wherein the another page should be able to receive the value entered by the user in the page1. I have got suggestions saying I can do this by using page header redirect to another page and concating the value of the textbox to the header variable. I am using this in page 1

<code> header("Location: Decryption.php?usersubmit=".urlencode($_POST($usersubmit))); </code> // Usersubmit is the value entered in the textbox field by the user.

and in page 2

<code> $userId = $_GET['usersubmit']; </code>

But of no use.

I also tried creating a session variable here and using the session variable in the page2. But that does not work either.

}

Someone please help me. Let me know if you have any questions?

Thanks,

 

Link to comment
Share on other sites

Code for the first page.

 

<?php
session_start(); // start up your PHP session!
?>

<html>
<head>
</head>

<body>
<form name="retrieve.php" action="retrievefieldex11.29.2.56.php" method="POST">
Enter your ID<input name="usersubmit" type="text"> <br>

<table><tr>
<td><b>Key size in bits: </b></td>

<td><select name="keySize">
<option value="128" selected="selected">128</option>
<option value="192">192</option>
<option value="256">256</option>
</select></td>
</tr>

<tr>
<td><b>Key in hex: </b></td>
<td><input type="text" size="66" name="key"></td></tr>

<tr>
<td><b>Ciphertext in hex: </b></td>
<td><input type="text" size="66" name="ciphertext"></td>

</tr>


</table>
<?php
mysql_connect("localhost","root","");
mysql_select_db("encryption") or die(mysql_error());
$userId = $_POST['usersubmit'];
if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key'] == "")) 
{

$query = mysql_query("select * from employee_details where id = '$userId'");
	if($row=mysql_fetch_assoc($query))
			{
				echo '<tr>';
				foreach($row as $value)
				echo '<td>'.$value.'</td>';
				echo '</tr>';
			}
	else
	echo "No rows returned";
  }
else if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key']))
{
//header("Location: Decryption.php?name=".urlencode($usersubmit));
//header("Location: Decryption.php?var1=$usersubmit");
    header("Location: Decryption.php?usersubmit=".urlencode($_POST($usersubmit)));
/*
     $columname = "ciphertext";
$tablename = "employee_details";

function getField($field, $tbl_name, $condition)
{

$result = mysql_query("SELECT $field FROM $tbl_name WHERE id = ".$condition);
return @mysql_result($result, 0);
}

$myValue = getField($columname,$tablename,$userId);
echo "$myValue";
echo "<br>";
*/
}
  


?>
<br>
<input name="submit" type="submit" value="Submit"><table>
</table>
</form>
</body>
</html></code>


Code for the second page:

<code><?php
session_start();

mysql_connect("localhost","root","");
mysql_select_db("encryption") or die(mysql_error());
$userId = $_GET['usersubmit'];
// $userId=$_SESSION['views'];

//        $userId = $_POST['var1'];
$columname = "ciphertext";
$tablename = "employee_details";
echo $userId;
//echo $var1;
echo "inside decryption";
function getField($field, $tbl_name, $condition)
{
echo "inside function";
$result = mysql_query("SELECT $field FROM $tbl_name WHERE id = ".$condition);

        return @mysql_result($result, 0);
}

$myValue = getField($columname,$tablename,$userId);
       
echo "$myValue";
echo "<br>";

?>

Link to comment
Share on other sites

Firstly, you cannot call header after outputting anything to the browser. You will need to rearrange your code accordingly.

 

Secondly, $_POST is an array, not a function. This....

 

header("Location: Decryption.php?usersubmit=".urlencode($_POST($usersubmit)));

 

should be...

 

header("Location: Decryption.php?usersubmit=".urlencode($_POST[$usersubmit]));

 

I don't see $usersubmit defined anywhere either though.

Link to comment
Share on other sites

Hi,

 

  Thanks for your prompt reply.

 

I have used the one you showed

 

header("Location: Decryption.php?usersubmit=".urlencode($_POST[$usersubmit]));

 

but it still does not print out the usersubmit value in the second page. usersubmit is the name given to the ID textbox in the page 1 code right at the top. Here it goes.

 

Enter your ID<input name="usersubmit" type="text"> <br>

 

 

Link to comment
Share on other sites

All right I will take that as a constructive criticism but can you tell me the reasons why you told me that so I can analyze my code from next time?

 

Mainly because the questions you are asking or introduction level questions. Like passing a variable from one page to another using $_POST or $_GET globals. These are one of the very first things most basic books or lessons teach. I don't really have time to look through all of your code, but that's just my 2cents.

Link to comment
Share on other sites

While this doesn't answer or have anything to do with your question, it must be fixed first. My previous reply might not have been clear enough. You cannot call header() after sending data to the browser. See all that html at the top of your page? That is output sent to the browser.

 

You will need to rearrange your code accordingly.

Link to comment
Share on other sites

Thorpe,

 

            Thanks thorpe. How do you think should I rearrange the code?  Can you give me a brief idea? Sorry for the trouble. But I am running out of ideas and this is frustrating me.

 

Is there any other method other than the header to redirect a page to the other page?

Link to comment
Share on other sites

Well I could understand get and post methods through books but I couldn't understand how do I approach this situation of two different submits.. That is the reason why I posted in the forum.

 

You can easily call two separate submits. Just use different names for each of them and call them with isset() and wrap that in an if statement and you can trigger any actions with any amount of submit buttons on a page like:

 

if (isset($_POST['NameOfSubmitButtonHere'])) {

do whatever code you want here..

}

 

MY example could be much improved, but should get you a direction. Good luck!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.