Session variable from Select Option - php

I am trying to help users with filling form in the case if their submission was not successfully send.. The first part of the code down below works fine, but the second part is not "detected". Even if the statement is false Select Pair is not shown.
HTML output:
<option selected='selected'> </option>
This is just a small piece of my script.
Any suggestion how to fix this? Thanks!
if(isset($_SESSION['pair'])) {
echo "<option selected='selected'> " . $_SESSION['pair'] . "</option>";
} else {
echo "<option disabled='disabled'>Select Pair</option>";
}

unset($_SESSION['pair']);
And run the code again. Chances you didn't properly unset it hence only the first part work.

Related

elements on select box did not show on php selectbox

I have a problem on my php code.I am a beginner in php programmng language.I am using codeigniter to do this.But,when i receive my data using php in controller makes this problem.It makes selectbox with only two elements on it. one is 'select nation' and another is '$'.c['Nation_name'].
this works on html properly.But when i using it controller using ajax makes this problem.Please help me friends...
echo"<select name='sel_PAdrs_nation'
value=".$sel_nation."onchange='state(this.value,'C')'
id='sel_PAdrs_nation'><option>Select Nation</option>
<?php
if(isset('$'.contentCoun)){
foreach('$'.contentCoun as '$'.c){
if('$'.c['Nation_id']=='$'.sel_nation){
echo'<option selected='selected'
value='$'.c['Nation_id']>'$'.c['Nation_name']</option>';
}
else{
print'<option value='$'.c['Nation_id']>'$'.c['Nation_name']
</option>';
}
}
}
?></select>";
Inside the PHP echo no more PHP code to be evaluated because PHP interprets your code in a single pass.
$tempfunc="state(this.value,'C')";
echo"<select name='sel_PAdrs_nation' onchange='".$tempFunc."' id='sel_PAdrs_nation'><option>Select Nation</option>";
if(isset($contentCoun)){
foreach($contentCoun as $c){
if($c['Nation_id'] == $sel_nation){
echo "<option selected='selected'value='".$c['Nation_id']."'>".$c['Nation_name']."</option>";
}
else{
echo "<option value='".$c['Nation_id']."'>".$c['Nation_name']."</option>";
}
}
}
echo "</select>";

Echo option value with selected

Im tryin to fix when i press my search button. That the selected search from my option field remains selected. But at the moment it automaticly picks the first field of the options in my form.
First one is hardcoded and it works.
<option value="HS" <?= ($nickval == 'HS' ? 'selected="selected' : '')?>>Homer Simpsons</option>
But then i wanted to echo out option value from database so its not hardcoded.
<?php
while(db2_fetch_row($queryexe)) {
echo "<option value='$pin'>$fullname</option>";
}
?>
And now when i want to add if its selected i tried to solve it like this.
echo "<option value='$pin'($nickval == '$pin' ? 'selected='selected'' : '')>$fullname </option>";
This is how i get my pin
$pin = db2_result($queryexe, 'P510PIN');
This is how i get my $nickval
$nickval = $_GET["int"];
Any suggestions what im doin wrong? Sorry if im unclear but i've tried my best
Aside from quoting errors indicated in the syntax highlighting...
You're trying to execute PHP code inside of a string:
echo "<option value='$pin'($nickval == '$pin' ? 'selected='selected'' : '')>$fullname </option>";
Variable interpolation is one thing, but code inside of a string isn't going to automatically execute. It's just a string being echoed to the page. (Check the page source and see what's actually being emitted to the browser.)
Separate the strings from the code which builds the strings:
echo "<option value='$pin' " . ($nickval == $pin ? "selected='selected'" : "") . ">$fullname </option>";

PHP echo not showing up in one computer?

I have a php script that queries a MySQL database and populates a drop-down menu using the data received. Everything was working fine and suddenly, the echo "Custom" option doesn't show up for me. I asked someone else to check the same page, and it showed up for him. I tried changing browsers, and nothing. Does anyone know why this would happen?
echo '<div class="c_element" style="height: auto;">
<select class="c_sel">';
$c= mysql_query("SELECT * FROM C WHERE c_lo_id =".$sel_lo_id) or die(mysql_error());
while($row = mysql_fetch_array($c))
{
echo '<option value='.$row['c_id'].'>'.$row['c_name'].'</option>';
}
echo '<option value="0">Custom </option>
</select>';
This can happen if your values contain characters that break your html, like ', > or <. When outputting to html, you should always make sure that these are encoded correctly.
Apart from that this would also happen if there are spaces in your values as you don't quote the attribute value.
With both corrections:;
echo '<option value="'.htmlspecialchars($row['c_id']).'">'.htmlspecialchars($row['c_name']).'</option>';
^ added as well ^

Using PHP function to create a dynamic dropdown menu using arrays: dropdown doesn't populate

I am trying to dynamically build a drop down menu using PHP. The idea is: the elements are formed from a loop which calls and array. If the array element matches the data held in session then it adds the "selected" attribute to the tag, meaning that the page displays the previously selected option.
I have tried to include one complete set of code here, all the way from defining the variables from session data to echoing the HTML for the form element.
It doesn't currently work - the drop down menu appears, but is blank, and has no options. I've debugged it with ideone and it seemed to run successfully, and I can't see where I am going wrong, however this is my first PHP function! So I'm sure I've screwed it up somehow :)
Any help much appreciated.
<?php
session_start();
//if the session data has been set, then the variable $sv_02 is defined
//as the data held in the session under that name, otherwise it is blank
if (isset($_SESSION['sv_02'])) {$sv_02=$_SESSION['sv_02'];} else {$sv_02="";}
//define the array
$dm_sv_02 = array('-Year','-2012','-2011','-2010','-2009');
//create the function
function dropdown($dropdownoptions, $session_data)
{
foreach($dropdownoptions as $dropdownoption){
if($session_data == $dropdownoption){
echo '<option value="' . $dropdownoption . '" selected>' . $dropdownoption . '</option>';
} else {
echo '<option value="' . $dropdownoption . '">' . $dropdownoption . '</option>';
}
}
}
//echo the HTML needed to create a drop down, and populate it with
//the function which should create the <option> elements
echo '<select name="sv_02">';
dropdown($dm_sv_02, $sv_02);
echo '</select>';
?>
Try this:
foreach ($dropdownoptions as $dropdownoption) {
echo ($dropdownoption == $sv_02) ? "<option selected=\"selected\" value=\"$dropdownoption\">$dropdownoption</option>" : "<option value=\"$dropdownoption\">$dropdownoption</option>";}
This turned out to be a result of the fact I was using {smarty} tags to build my php, the code was as written but only worked when it was all included in one smarty tag, I'm not sure I understand why that should be the case but in any regard it was fixed by including it all in one tag.

3 select boxes in a row in a single line?

it is only displaying the first select box and the last one ..
here is the code.
function select_nom_of_guests($guest_type){
$i=0;
echo $guest_type;
echo "<select name=\"adults_num\" id= \"a\">";
while ($i<=5){
echo "<option value= $i>$i</option>" ;
$i++;
}
}
echo "<p>";
select_nom_of_guests("מספר מבוגרים");
select_nom_of_guests("מספר ילדים");
select_nom_of_guests("מספר תינוקות");
echo "</p>";
Close your <select> tags and it should work better ;-)
Note that a for loop would be more appropriate in your case.
Note that you don't end the <select> tag. I'm not sure how browsers would respond to that, but it definitely wouldn't help.
One helpful tool in these scenarios is the View Source tool that all major browsers have: instead of being confused about what's appearing on the screen, look at the HTML that the browser received to see why it might be showing what it's showing. If this is the issue, the source code would have revealed it lickity-split :)
You didn't close the select tag. You probably also want to make the name and id attributes different for each select.
function select_nom_of_guests($guest_type){
$i=0;
echo $guest_type;
echo "<select name=\"adults_num".$guest_type."\" id= \"select_".$guestType."\">";
while ($i<=5){
echo "<option value= $i>$i</option>" ;
$i++;
}
echo "</select>";
}

Categories