I have 2 pages: index.php (where I input brand name in a textbox and on clicking submit I need to view the brands I added in drop down box in same page) and product page (where I can edit the brands I select in the index page).
I am not using database, instead I'm trying to store value in session variable. I need to know how I can use dynamic session variable so that each time a new brand would be added to drop down menu...
index.php
<body><div>
<form name="brand" method="post" action="brand_action.php">
Brand Name: <input type="text" name="brand" id="brand"><br /><br />
<input type="submit" value="Submit">
</form>
</div>
<div>
<?PHP
if(isset($_SESSION["bname"]))
{?>
<form name="add" action="product.php" method="post">
Brands :<select name="brand" id="brand">
<?PHP
$a=$_SESSION["bname"];
?>
<option value="<?PHP echo($a);?>"> <?PHP echo($a);?> </option>
</select>
<input type="submit" name="Edit" value="EDIT" />
</form>
<?PHP
}
?>
</div>
</body>
In the index_action.php page,I'm thinking to push each brand into an array and store in session variable and call it in index.php page and put it in drop down list.How can I do it?
Well you can make a session variable act as an array . Store all the brand names in that array, then loop through it.
Here's an example.
<?php
session_start();
if(!isset($_POST['brand']))
{
$_SESSION['bname'] = array();
}
else
{
$_SESSION['bname'][] = $_POST['brand'];
}
?>
<body><div>
<form name="brand" method="post" action="s.php">
Brand Name: <input type="text" name="brand" id="brand"><br /><br />
<input type="submit" value="Submit">
</form>
</div>
<div>
<?PHP
if(isset($_SESSION["bname"]))
{?>
<form name="add" action="product.php" method="post">
Brands :
<select name="brand" id="brand">
<?php
foreach($_SESSION['bname'] as $brand_name)
{
?>
<option value="<?PHP echo($brand_name);?>"> <?PHP echo($brand_name);?> </option>
<?php
}
?>
</select>
<input type="submit" name="Edit" value="EDIT" />
</form>
<?PHP
}
?>
</div>
</body>
Also remember, session_start
should always be the first line of the page in which you need to use sessions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.