0

First php script -

<?php
 session_start(); 
?>

<html>
<head>
</head>
<body><form method = "post">
<select name="feature" id="feature">
        <?php
        ?>
        <option value > Select Feature</option>
        <?php
        foreach($newFeature as $feat)
        {
            ?>
        <option value="<?php echo $feat;?>"><?php echo $feat;?></option>
            <?php
       }
         ?>
</select>
</form>
</body>
</html>

<?php
    $_SESSION['feature'] = $feature;    
 ?>

second php script -

<?php
session_start(); 
echo $_SESSION['feature'];
?>

When I run second php script, I get Array as echoed element instead of the element I selected. What is wrong with the logic here ?

Please guide.

5
  • where do you get the post/get value? Commented Apr 29, 2014 at 6:26
  • What is $feature? Where it is defined?? Commented Apr 29, 2014 at 6:27
  • You need to submit the form and then get it through $_POST Commented Apr 29, 2014 at 6:29
  • is $feature supposed to be the selected option? Commented Apr 29, 2014 at 6:30
  • @UglyEddie - yes it is. Commented Apr 29, 2014 at 6:32

1 Answer 1

2

You have to submit the select. It is impossible to set $feature at that moment because the user hasn't yet selected anything.

<form method = "post">
<select name="feature" id="feature">
        <option value > Select Feature</option>
        <?php foreach($newFeature as $feat) : ?>
           <option value="<?php echo $feat;?>" <?= $feat == $_SESSION['feature'] : " selected = 'selected'" : "" ?>><?php echo $feat;?></option>
        <?php endforeach; ?>
</select>
<input type="submit" value="send" name="mySubmit" />
</form>

When you hit 'send' you can get the value by using $_POST['feature']; on the same page. If you want to go to another page you have to set the form action property.

session_start();
$_SESSION['feature'] = $_POST['feature'];

After the submit the page will 'reload'. Check if mySubmit is set and set the $_SESSION['feature'](don't forget to start your session at top of the page):

if (isset($_POST['mySubmit'])){
   $_SESSION['feature'] = $_POST['feature'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

without using action cant I send $_POST['feature']; to another page ?
When you are leaving action blank, it will request the same page as you are on.
here I want to store this value $_POST['feature']; in a variable so that I can use across multiple other script and so i want to use session

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.