Jump to content

SESSION Arrays


nitestarz

Recommended Posts

I have a form that contains a list  (Interests) where you can make multiple selections. This information, like the rest of the form, is saved in a session. When the form is processed, the information is sent and the user goes to a thank you page.

 

Everything displays correctly except the list - it just displays like: Interest1, Interest2, Interest3.

 

How can I get the Interests list  to display in an unordered list?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/231279-session-arrays/
Share on other sites

On the form page:

 

<?php
session_start();

include "PHP_Lib.php";

$_SESSION['Contact_First_Name'] = $_POST['Contact_First_Name'];
$_SESSION['Contact_Last_Name'] = $_POST['Contact_Last_Name'];
$_SESSION['Contact_Email'] = $_POST['Contact_Email'];
$_SESSION['Interests'] = implode(',',$_POST['Interests']);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>

<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="form">
  <p>
    <label for="Firstname"><strong>First Name:</strong></label>
    <input name="Contact_First_Name" size="45" value="<?=$_POST['Contact_First_Name']?>" />
  </p>
  <p>
    <label for="Lastname"><strong>Last Name:</strong> </label>
    <input name="Contact_Last_Name" size="45" value="<?=$_POST['Contact_Last_Name']?>" />
  </p>
  <p>
    <label for="E-mail"><strong>*E-mail: </strong></label>
    <input name="Contact_Email" size="45" value="<?=$_POST['Contact_Email']?>" />
  </p>
  <?php
$Interests = is_array($_POST['Interests']) ? $_POST['Interests'] : array();
?>
  <p> <strong>Please indicate areas of interest.  Hold down the CTRL 
    key to select multiple interests.</strong><br />
    <select name="Interests[]" multiple="multiple" size="3">
      <option value="Interest1" <?php if(in_array('Interest1',$Interests)) echo " SELECTED"; ?>>Interest1</option>
      <option value="Interest2" <?php if(in_array('Interest2',$Interests)) echo " SELECTED"; ?>>Interest2</option>
      <option value="Interest3" <?php if(in_array('Interest3',$Interests)) echo " SELECTED"; ?>>Interest3</option>
    </select>
  </p>
</form>
</body>
</html>

 

On the thank you page:

 

<?php 
session_start(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php	   
  echo "  <table id=\"tablecolor\" style=\"font-size:12px;\">\n";
  echo "  <tr>\n";
  echo "    <th colspan=\"2\">The following information has been received:</th>\n";
  echo "  </tr>\n";
  echo "  <tr>\n";
  echo "    <td><strong>Name:</strong></td>\n";
  echo "    <td>$Contact_First_Name $Contact_Last_Name</td>\n";
  echo "  </tr>\n";
  echo "  <tr>\n";
  echo "    <td><strong>E-mail:</strong></td>\n";
  echo "    <td>$Contact_Email</td>\n";
  echo "  </tr>\n";
  echo "  <tr>\n";
  echo "    <td><strong>Interests:</strong></td>\n";
  echo "    <td>$Interests</td>\n";
  echo "  </tr>\n";  
  echo "</table>\n";

?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/231279-session-arrays/#findComment-1190394
Share on other sites

The way your code is written, it looks like you have register_globals turned on. This is very insecure.

 

As to your question, change

<?php
$_SESSION['Interests'] = implode(',',$_POST['Interests']);
?>

to

<?php
$_SESSION['Interests'] = '<ul><li>' . implode('</li><li>',$_POST['Interests']) . '</li></ul>;
?>

and you should get an unordered list.

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/231279-session-arrays/#findComment-1190406
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.