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
Related
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;
}
I know I this is simple, but i just can;t find the resource that tells me how to do it.
SO my code is as follows:
session_start();
$wquery=
"select week_num,week_name
from stats_week
where season=$this_season
order by week_num";
$wresult=mysql_query($wquery);
print'<form action="changeplayer_2.php" method="post">';
print"<select name='Week_select'> <br>";
while ($wrow=mysql_fetch_array($wresult))
{
print '<option value="'.$wrow['week_num'].'">'.'week '.$wrow['week_num'].' '.$wrow['week_name'].'</option><br>\n';
}
print "</select><br><br>";#
print'<button type="submit" >Next</button>';
print"</form>";
So I am making a selection:
I want that selection to end up in: $_SESSION['week']
$_SESSION['week']=$_POST['Week_select'];
you can do this either sending data by form on submit or if you want to do without refreshing page you can do this by ajax
and than set like this
$_SESSION['week']=isset($_POST['Week_select'])?$_POST['Week_select']:someDefault;
and to do this by ajax check this answer
Inside file "changeplayer_2.php" you'll want to load the POSTed value to the session:
session_start();
$_SESSION['week'] = $_POST['Week_select'];
Whenever you want to get the value of the session at key 'week', simply do:
$weekValue = isset($_SESSION['week']) ? $_SESSION['week'] : false;
But if you do use the session variable on any script, make sure you start the session using session_start().
To destroy the session variable, you can do:
session_start();
unset($_SESSION['week']);
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();
ok, i'm trying to do a quiz...all good by now. but when i'm trying to send the collected data(radio buttons values) through pages i can't get the logic flow. I have the main idea but i can;t put it into practice.
i want to collect all radio values
create an array containing this values
serialize the array
put the serialized array into a hidden input
the problem is that i want to send data on the same page via $_SERVER['PHP_SELF'] and i don;t know when in time to do those things.(cause on "first" page of the quiz i have nothing to receive, then on the "next" page i receive the S_POST['radio_names'] and just after the second page i can get that hidden input). i hope i made myself understood (it's hard even for me to understand what my question is :D )
You could try to use the $_SESSION object instead... For each page of your quiz, store up the results in the $_SESSION array. On the summary page, use this to show your results.
To accomplish this, on the beginning of each page, you could put something like:
<?
session_start();
foreach ($_POST as $name => $resp) {
$_SESSION['responses'][name] = $resp;
}
?>
Then, on the last page, you can loop through all results:
<?
session_start();
foreach ($_SESSION['responses'] as $name => $resp) {
// validate response ($resp) for input ($name)
}
?>
Name your form fields like this:
<input type="radio" name="quiz[page1][question1]" value="something"/>
...
<input type="hidden" name="quizdata" value="<?PHP serialize($quizdata); ?>"/>
Then when you process:
<?PHP
//if hidden field was passed, grab it.
if (! empty($_POST['quizdata'])){
$quizdata = unserialize($_POST['quizdata']);
}
// if $quizdata isn't an array, initialize it.
if (! is_array($quizdata)){
$quizdata = array();
}
// if there's new question data in post, merge it into quizdata
if (! empty($_POST)){
$quizdata = array_merge($quizdata,$_POST['quiz']);
}
//then output your html fields (as seen above)
As another approach, you could add a field to each "page" and track where you are. Then, in the handler at the top of the page, you would know what input is valid:
<?
if (isset($_POST['page'])) {
$last_page = $_POST['page'];
$current_page = $last_page + 1;
process_page_data($last_page);
} else {
$current_page = 1;
}
?>
... later on the page ...
<? display_page_data($current_page); ?>
<input type="hidden" name="page" value="<?= $current_page ?>" />
In this example, process_page_data($page) would handle reading all the input data necessary for the given page number and display_page_data($page) would show the user the valid questions for the given page number.
You could expand this further and create classes to represent pages, but this might give you an idea of where to start. Using this approach allows you to keep all the data handling in the same PHP script, and makes the data available to other functions in the same script.
You want to use a flow such as
if (isset $_POST){
//do the data processing and such
}
else {
/show entry form
}
That's the most straight forward way I know of to stay on the same page and accept for data.
I have a database populated drop down list.
$options4="";
while ($row = mysql_fetch_array($result)) {
$id=$row["Client_Code"];
$thing=$row["Client_Full_Name"];
$options4.="<OPTION VALUE=\"$id, $thing\">".$thing;
}
?>
<FORM name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<SELECT NAME="ClientNamefour" OnChange="this.form.submit()">
<OPTION VALUE=0>Client
<?php echo $options4?>
</SELECT>
</FORM>
Once this changes it pulls a client name from a database and brings up some information there is a form that can be filled out to insert more information into a database. Once that form is filled out this page sends the information to another page with the code to insert the data into the database. Then I am using a header at the end of that code to send it back to this page. But when it comes back to this page I want it to comeback to the client they selected before and not a blank screen. So how do I make a DB populated list automatically comeback to the previous selection using php?
All you need to do is modify the way you construct the options to spot when you are creating the selected one, then add a SELECTED attribute to it...
//obtain current value from GET, POST or COOKIE value...
$current=isset($_REQUEST['ClientNamefour'])?$_REQUEST['ClientNamefour']:'';
while ($row = mysql_fetch_array($result)) {
$id=$row["Client_Code"];
$thing=$row["Client_Full_Name"];
$value="$id, $thing";
//figure out if we should select this option...
$sel=($value==$current)?'SELECTED':'';
$options4.="<OPTION $sel VALUE=\"$value\">".$thing;
}
One way to keep the value persistent would be to store it in the session, e.g. when you process the form
//assuming session_start() has been called...
if (isset($_POST['ClientNamefour']))
$_SESSION['ClientNamefour']=$_POST['ClientNamefour'];