elements on select box did not show on php selectbox - php

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>";

Related

Session variable from Select Option

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.

php echo fails if I echo "<"

I have a strange problem which I presume is to do with my setup (WAMP on Windows 10). I am a beginner and working on my first project (rewriting an old Access/VBA solution).
I want to create an HTML drop down list on the fly - mysqli_query works fine and I can echo the list I need and get an accurate row count. But when I try any echo statement which starts with a '<' the rest of the page doesn't run.
The < is fine anywhere else but at the beginning. What I need is to be able to put
while($row = mysqli_fetch_assoc($result)) {
echo "<option value ='".$row['schoolName']."'>".$row["schoolName"]."</option>";
}
This just displays a list of $row["schoolName"] biut no other text.
There may be some mistakes in that code but I have tested this with much shorter echo strings and they always fail if '<' is the first thing after echo. I don't get an error message - the code above just gives a correct list of $row['schoolName].
Any ideas?
Try this
<select name="" id="input" class="form-control">
<?php
while($row = mysqli_fetch_assoc($result)) {
echo "<option value ='".$row['schoolName']."'>".$row["schoolName"]." </option>";
}
?>
</select>
You must add the <select> tag before using an <option> tag.
<select name="" class="">
<?php
while($row = mysqli_fetch_assoc($result)) {
echo "<option value ='".$row['schoolName']."'>".$row["schoolName"]."</option>";
}
?>
</select>

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.

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.

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