1

I want to echo part of an array 'name' and then have a submit button next to it which redirects to a new page which echos the rest of the array eg. address, phone number ect. but the information gets lost when redirected. is there some way around this. This code should explain what I'm getting at.

Page 1:

<?php
   $info = mysql_query ("SELECT * FROM table WHERE name= 'something'");
   $info = mysql_fetch_assoc($info));
?>

<html>
  <form action='page2.php' method='POST'>
    <?php echo $info['name']; ?>
    <input type='submit' value='see other details'>
  </form>
</html> 

Page 2:

<?php
   echo $info['address'].$info['address'].$info['address'];
?>
user1557515
  • 87
  • 2
  • 3
  • 14

2 Answers2

0

the information gets lost when redirected. is there some way around this

Yes, use the PHP session. Store your array as $_SESSION['info'], and it will become available on the next page. Don't forget to session_start() on both pages.

Page 1:

<?php
   session_start();
   $info = mysql_query ("SELECT * FROM table WHERE name= 'something'");
   $info = mysql_fetch_assoc($info));
   $_SESSION['info'] = $info;
?>

Page 2:

<?php
   session_start();
   $info = $_SESSION['info'];
   echo $info['address'].$info['address'].$info['address'];
?>
MrCode
  • 63,975
  • 10
  • 90
  • 112
  • Oh, OK. I didn't realize one could do that. Thanks Very much. :) – user1557515 Dec 14 '12 at 16:59
  • @user1557515 see my answer here for a brief session explanation: http://stackoverflow.com/questions/13865711/session-variable-vs-normal-variable/13865894#13865894 – MrCode Dec 14 '12 at 17:01
0

Unless this is a greatly simplified version of what you're trying to do, there's no reason to use a form here. You're not posting any data to the form, just using it to get to another page, which technically is what a link is for. It's not a blatantly incorrect use of a form, but it's not really all that --right-- either.

Simply echo the value with a link to the next page. You can style the a:link using something like jQuery buttons or even with an image to mimic a button if that's a requirement.

So, for instance:

<?php
   $info = mysql_query ("SELECT * FROM table WHERE name= 'something'");
   $info = mysql_fetch_assoc($info));
?>

<html>
    <?php echo $info['name']; ?>
    <a href="page2.php">Submit</a>
</html> 

Another alternative would be do do two stacked divs and show/hide them on click of a button. It requires a little jQuery or javascript, but eliminates the hit to the server in between, making a smoother user experience. It's tough to say what's best without more details of what you're trying to do.

Finally, bear in mind that while sessions do work, there can be issues. For instance, some companies and people turn sessions off (or block them altogether) so they can't be relied on 100%. IE in particular (in some versions) interpreted the user's choice to block cookies to also block sessions. If they have your app open in two tabs, it can get the values confused and result in undesirable outcomes. At the least, it can cause your application to throw all sorts of dialogs that scare users, even though they're really not that big of a deal.

bpeterson76
  • 12,918
  • 5
  • 49
  • 82