0

Heading

how i get the selected value of city_id from drop down list and store in a session??

`

<form action="dynamic.php" method="post">
<select name="city" > 
<option value="">Select City</option>
 <?php
 $sql="select * from city ORDER BY city_name";
 $result=mysqli_query($con,$sql);
 while($row=mysqli_fetch_array($result)){
  ?>
  <option value="<?php echo $row["city_id"];?>"><?php echo $row["city_name"];?></option>
  <?php
 }    
 ?>
</select>



</form>
<?php
session_start();
if(!empty($_POST['city'])){
    $_SESSION['city'] = $_POST['city'];
}
 ?>
Mani ch
  • 1
  • 2
  • 1
    Possible duplicate of [Get selected value in dropdown list using JavaScript?](https://stackoverflow.com/questions/1085801/get-selected-value-in-dropdown-list-using-javascript) – Sterling Archer May 22 '18 at 20:55
  • You submit the form towards `dynamic.php` as you've set it up and there you do something like `$_SESSION['city_id'] = $_POST['city'];` – icecub May 22 '18 at 20:55
  • @SterlingArcher I wouldn't call that a duplicate. He's asking how to store the value into a session. Not how to get the value with JS. – icecub May 22 '18 at 20:59
  • i did it but not working – Mani ch May 22 '18 at 21:00
  • Please clerify _"it's not working"_. How did you test it? Did you call `session_start();` on the page where you are getting the session value as well? Otherwise the session will be lost. – icecub May 22 '18 at 21:03
  • yes i start session ..but not working ...giving an error like Undefined index: city_id in ... – Mani ch May 22 '18 at 21:11
  • @Manich you could try using `isset` instead of `!empty` and this should not produce the undefined index error. Like this: if (isset($_POST['city_id'])) { $_SESSION['city'] = $_POST['city']; } – zysoft May 22 '18 at 21:15
  • @zysoft That's not correct. `isset()` does exactly the same as `empty()` except that `empty()` also verifies if the variable has a value or not. - Manich: What I don't get is that you're getting an error _Undefined index: city_id_ while your code doesn't have any array with `city_id` in it? All I see is indexes with `city`. – icecub May 22 '18 at 21:18
  • 1
    @icecub You are right. I think the problem is in ` – zysoft May 22 '18 at 21:22
  • @zysoft Yup. The problem is most likely the MySQL connection, query and/or database structure. OP should do some proper error handling on those. Nice find. – icecub May 22 '18 at 21:26
  • @icecub:previously I use $_SESSION['city_id'] then i change it into to $_SESSION['city'] ,that's why show udefined index:city_id – Mani ch May 22 '18 at 21:27
  • @Manich Please [edit](https://stackoverflow.com/posts/50476296/edit) your question and include the code where you're verifying the session variable. – icecub May 22 '18 at 21:31
  • @icecub thanxx ..its working now... – Mani ch May 22 '18 at 21:47
  • @icecub: i have a query... if i use button in
    tag it not direct the target page ?..//can you explain ...
    – Mani ch May 22 '18 at 22:07
  • You submit a form with `
    `
    – icecub May 22 '18 at 22:15

2 Answers2

0

You will need to session_start();

Then

$_SESSION['city'] = 'somecity';

Jairus
  • 816
  • 8
  • 27
0

you need use this in each page that you use the var:

session_start();

then you can add this

$_SESSION["myvar"] = "something"