t4thilina
t4thilina

Reputation: 2197

Store the drop down selected value in a SESSION variable

Im getting a set of values from the database to show in a drop down list. Once the user selected an item from the drop down list the selected item should be assigned to a SESSION variable. I tried this but yet it is not working. Please smeone help me to sort it out.

<select id="FormNameSelecting" style="position:absolute; width:300px; top:50px; left:200px; "><option></option>
<?php

    $result = mysqli_query($con,"SELECT * FROM Form");
    while($row = mysqli_fetch_array($result)){      
    echo "<option  value='$row[Form_ID]'>$row[Form_Name]</option>";
    echo 
    }
?>
</select>

So I need to store these values receive from $row[Form_ID] and $row[Form_Name] in 2 session variables named $_SESSION['Form_ID'] and $_SESSION['Form_Name']; Could someone explain me how I can assign the selected item's values to these two session variables.

Upvotes: 0

Views: 11375

Answers (3)

Hansen
Hansen

Reputation: 690

try this

<form name="my_name" method="POST" action="">
<select name="FormNameSelecting" id="FormNameSelecting" style="position:absolute; width:300px; top:50px; left:200px; "> //don't forget put name at select
<option></option>
<?php

    $result = mysqli_query($con,"SELECT * FROM Form");
    while($row = mysqli_fetch_assoc($result)){ //prefer using assoc that array     
    echo "<option  value='" . $row['Form_ID'] . ":::" . $row['Form_Name'] . "'>" . $row['Form_Name'] . "</option>";
    }
?>
</select>
<input type="submit" name="go" value="go"/>
</form>

<?php
if(isset($_POST['go'])){

    $store_sesi = explode(":::",$_POST['FormNameSelecting']);
    $_SESSION['ID'] = $store[0];
    $_SESSION['Name'] = $store[1];
}
?>

hope this code help you

Upvotes: 0

Chirag Nagpal
Chirag Nagpal

Reputation: 729

Try This: In your page add this and create one more page "showValues.php"(This is the page where you want to create and use your session variable).Code of showValues.php is also given below for example.

<html>
<head>
 <script>
function showValue(vals)
{
    if (window.XMLHttpRequest)
    {   // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {

        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {

            var val= xmlhttp.responseText;

            document.getElementById("tbl").innerHTML+=val;

        }
    }
    xmlhttp.open("GET","showValues.php?vals="+vals,true);
    xmlhttp.send();

}
</script>
 </head>
<body>
<table id="tbl">
    <tr>
    <td>
    <select id="FormNameSelecting" style="position:absolute; width:300px; top:50px; left:200px; "><option></option>
<?php

$result = mysqli_query($con,"SELECT * FROM Form");
while($row = mysqli_fetch_array($result)){      
echo "<option  value='$row[Form_ID]'>$row[Form_Name]</option>";
echo 
}
?>
</select>
</td>
</tr>
</table>
</body>
</html>

showValues.php

<?php
session_start();
$vals=$_REQUEST['vals'];
$_SESSION['SelectValue']=$vals;
echo $_SESSION['SelectValue'];
?>

Upvotes: 0

ailinykh
ailinykh

Reputation: 73

The selected option element must have selected attribute. You should try something like this:

<select id="FormNameSelecting" style="position:absolute; width:300px; top:50px; left:200px; ">
<?php

    $result = mysqli_query($con,"SELECT * FROM Form");
    while($row = mysqli_fetch_array($result))
    {      
      echo "<option  value='{$row['Form_ID']}'";

      if($row['Form_ID'] == $_SESSION['Form_ID'])
        echo " selected";

      echo ">{$row['Form_Name']}</option>";
    }
?>
</select>

UPDATE:

You can store it in $_SESSION after the form submit

<?php
  session_start(); // dont forget it!  

  if(isset($_POST['submit']))
  {
    $_SESSION['Form_ID'] = $_POST['Form_ID']; 
    $_SESSION['Form_Name'] = $_POST['Form_Name']; 
  }
?>

Upvotes: 1

Related Questions