Getting dynamic dropdown value to a Session in PHP - php

a PHP newbie here and need to clarify a thing.
I have populated a dropdown box with data from a SQL database. The code looks like like below.
echo '<select id="dropMe" name="dropMe" style="width:150px; font-family:Georgia;">';
echo '<option value=""></option>';
while($rec=mysql_fetch_array($run1))
{
$value = $rec['route'];
echo "<option value=\"$value\">$value</option>";
}
echo '</select>';
What I want to know it is it possible to assign a value selected by a user to a SESSION hence this is a dynamic dropdown? (saveroute is my submit button)
if (isset($_POST['saveroute']))
{
$Q = $_POST['dropMe'];
$_SESSION['menuRoute'] = $Q;
echo ($_SESSION['menuRoute']);
}
I code something like this, but I get an undefined error with 'dropMe'. I'm not quiet familier with this type of error and can some one throw some suggestions or point out any errors in the method.
Thanks for looking.

Try printing out the whole POST array with
print_r
Like this
print_r($_POST);
It looks like you are checking if "saveroute" is set and not "dropMe", looks like it doesn't exist.
http://php.net/manual/en/function.print-r.php

I would recommend you to inspect the $_POST array to be sure that you are receiving the value you are expecting from the html form.
var_dump($_POST);
or
print_r($_POST);
If that is correct then be sure that you have started the session before accesing it with:
session_start();

Related

Making value checkbox appear

I have a problem with my code. I use CodeIgniter.
I made a checkbox form with a code like this :
foreach ($get_student->result_array() as $row) {
$select_student[$row['student_number']] = $row['student_name'];
echo form_checkbox($select_student,$student_number,FALSE);
}
then the result just like this, only the checkbox appears, the student's name or student number does not appear:
As per the document,
Find the CodeIgnitor form_checkbox documentation
the array should have an element with key='value'.
Hence this line of code
$select_student[$row['student_number']] = $row['student_name'];
should be changed to
$select_student['value'] = $row['student_number'];

How to generate list from selected value

Please, I need generate selectable list based on value from other list. I have function for generating first list.
function getCatList ()
{
$result = getCategory ();
echo "<select id=\"catList\">";
echo "<option value=\"\" selected=\"selected\">Select category</option>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value=\"".$row['codename']."\" onclick=\"<?php if($options==".$row['codename'].") echo 'selected=\"selected\"'; ?>".$row['visible_name']."</option>";
}
echo "</select>";
}
and I need generate next list based on selected value from this list.
I try set variable when onclick or some other attributes but without result.
I suppose it's caused because PHP is server-side language.
Can you help me? I would like avoid JS if possible. Is there any option how can I do this?
You cannot launch PHP that way.
If you want to (without refresh) generate a list from a selected value you need to use Javascript. The easy way is to use Ajax, but you can make something even with Javascript alone (in the same page).
You can for example save on loading some arrays of data, and then on select change the list or what you want.

PHP - Hold Session variable from Select Option box to every page

I seem to be having a problem and after hours of searching and messing with different ideas I can't seem to come up with this seems to be simple PHP Session problem with my Select Options menu.
All I want to do is simple have the selected Hotel Name from the dropdown hold till it is changed, and this goes for every page. So I thought, of yea lets just use Sessions. Doesn't seem to work very well as it keeps refreshing the data. Here is my code, and maybe one of you guys can come up with a solution. I would really appreciate it!
We are using a json file to populate the menu that we get by using a CURL to call the data and then decode the json.
Here is the dropdown code:
<select id="hotelSelected" name="hotelSelected">
<?php
sort($hotelName);
foreach($hotelName as $value):
echo '<option value="'.$value.'"'.ucfirst($value).'</option>';
endforeach; ?>
</select>
And then I tried to create a session by doing this:
$_SESSION['hotelName'] = $hotelName;
And now I need this session to carry on every page. And possibly echo on certain pages by possibly using this:
echo $_SESSION['hotelName'];
Any help is appreciated or redirect me to a forum that can fix my problem. Thank you guys!
you should use session_start(); at the top of every PHP file which uses sessions.
Also if you have a form contains SELECT tag , after submitting the form you should use $_POST['hotelSelected'] for grabbing tha value of option selected by user.
EDIT 1:
<select id="hotelSelected" name="hotelSelected">
<?php
sort($hotelName);
foreach($hotelName as $key=>$value):
echo '<option value=($_SESSION['hotelname'][$key]):"'.$_SESSION['hotelname'][$key].'"'.ucfirst($_SESSION['hotelname'][$key]).'?"'.$value.'"'.ucfirst($value).'</option>';
endforeach; ?>
</select>
<?php
$_SESSION['hotelname']=$hotelname;
html
<select id="hotelSelected" name="hotelSelected" onchange="run(this)">
<?php
sort($hotelName);
foreach($hotelName as $value):
echo '<option value="'.$value.'"'.ucfirst($value).'</option>';
endforeach;
?>
</select>
javascript
function run(sel) {
var i = sel.selectedIndex;
if (i != -1) {
$.post('updatesession.php', {id:i}, function(){
alert('Session Updated!');
});
}
}
updatesession.php
if(isset($_POST['id'])){
session_start();
$_SESSION['hotelName']=id;
}

Use HTML dropdownlist made in PHP with data from MySQL database

I have a PHP script which connects to a MySQL database and creates an HTML dropdown list with data retrieved from it. The script works, I just don't understand how I'm supposed to use it in an HTML form.
The PHP script, named CountryList.php, looks like this:
<?php
function createCountryList() {
$con = mysql_connect("localhost","user","pass");
mysql_select_db('database', $con);
$sql="SELECT Country FROM CountryList";
$result = mysql_query($sql,$con);
echo "<select name=country value=''>Country</option>";
echo "<option value=0>Select Country</option>";
echo "<option value=1></option>";
$curvalue=2;
while($nt=mysql_fetch_array($result)){
echo "<option value=$curvalue>$nt[Country]</option>";
$curvalue = $curvalue+1;
}
echo "</select>";
mysql_close($con);
}
?>
I tried including the PHP file in the head of the HTML page with:
<?php include("CountryList.php"); ?>
And then I tried calling the function which creates the dropdown menu in a form later on:
<form action="insert.php" method="POST">
<label>Which country are you from?</label>
<?php createCountryList(); ?>
</form>
Since nothing happens, I did it wrong. Any advice? Help is appreciated.
EDIT: Bah, I knew it was something silly. The page was an HTML file, PHP didn't process it. Changing the HTML file to PHP solved everything.
What happens when you change this line
echo "<select name=country value=''>Country</option>";
to this
echo "<select name='country'>Country";
Are you sure its in the same folder. Its a good practice to do a check before
if ((include 'CountryList.php') == 'OK') {
createCountryList();
}
else{
echo "Didnt import";
}
Dont do brackets like include()
Try changing echo "<option value=$curvalue>$nt[Country]</option>"; to echo "<option value=$curvalue>{$nt['Country']}</option>";
Echoing out arrays requires curly brackets around the array index if inside a string. Also the index needs quotes or php will assume constant.
This is what I did in one of my HTML forms. Try and see if it works for you.
<select name="country"><?php createCountryList();?></select>
What you do here is on the page where you want the selectbox, you put in this code and call the function.

how to keep value in a listbox in PHP?

I got a listbox in my php page that generate from another listbox, the code is as follows
<?php $myArray1 = $_POST['countryRF'];?>
<select name='countryRF[]' id='countryRF' size='10' multiple style='width:180px' class='selfont'>
<?php
foreach($myArray1 as $value){ // Loop through each element
print "<option value=\"".$value."\">".$value."</option>";
}
?>
</option></select>
While refreshing the form listbox get empty, how can I keep added values even after form reload ?
Is it possible to keep the mysql result table stable even after form reloading by session ? If so please give me a help ?
i see you get the 'countryRF' from post.
you can store it in a cookie/session, of store it on the server.
use
if (isset($_POST['countryRF'])) {
$_COOKIE['countryRF'] = $_POST['countryRF'];
}
$myArray = $_COOKIE['countryRF'];
same goes with session
save them into Sessions

Categories