gsashwin Posted December 1, 2010 Share Posted December 1, 2010 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, Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/ Share on other sites More sharing options...
trq Posted December 1, 2010 Share Posted December 1, 2010 Post some relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141696 Share on other sites More sharing options...
gsashwin Posted December 1, 2010 Author Share Posted December 1, 2010 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141697 Share on other sites More sharing options...
trq Posted December 1, 2010 Share Posted December 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141700 Share on other sites More sharing options...
gsashwin Posted December 1, 2010 Author Share Posted December 1, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141702 Share on other sites More sharing options...
OldWest Posted December 1, 2010 Share Posted December 1, 2010 gsashwin, Just my opinion, but you should spend some time getting familiar with the basics of php and mysql. Buy a beginner php book and read the intro php.net documentation. Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141705 Share on other sites More sharing options...
gsashwin Posted December 1, 2010 Author Share Posted December 1, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141713 Share on other sites More sharing options...
OldWest Posted December 1, 2010 Share Posted December 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141715 Share on other sites More sharing options...
gsashwin Posted December 1, 2010 Author Share Posted December 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141716 Share on other sites More sharing options...
trq Posted December 1, 2010 Share Posted December 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141720 Share on other sites More sharing options...
gsashwin Posted December 1, 2010 Author Share Posted December 1, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141723 Share on other sites More sharing options...
OldWest Posted December 1, 2010 Share Posted December 1, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/220324-transfer-of-variable-value-from-page1-to-page-2/#findComment-1141724 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.