php echo fails if I echo "<" - php

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>

Related

populate textbox based on combobox value using ajax

I badly need your help on this.
I can't seem to find what's wrong in my code.
The case is, I am trying to populate my textbox field based on my combobox value and it is connected in sql database. I have tried some codes on the web and then I found a code which seems accurate but I can't seem to display the result on my textbox.
here is my HTML Code:
<?php
echo"Concept Store:";
echo "<select width='100' id='strs' name='strs' >";
echo "<option></option>";
while($row=sqlsrv_fetch_array($stmt))
{
$x= $row['strnm'];
echo " <option> $x</option>" ;
}
echo "</select>";
?>
Address &nbsp&nbsp&nbsp: <input type="text" id="add" name="add" size="27" /><br><br>
here's the AJAX:
<script type="text/javascript">
$(document).ready(function(){
$('#strs').change(function(){
$.post("gadd.php",{strs:$(this.val() )},function(result){
$("#add").val(result);
});
});
});
</script>
and here's my 'gadd.php'
<?php
session_start();
include ('sqlconn.php');
$gadd=$_post['strs'];
//$t1= mysql_real_escape_string($t1);
$sql="Select distinct dadd1 from ostore where strnm='".$gadd."' ";
$stmt = sqlsrv_query($conn,$sql);
//echo "<option></option>";
while ($row = sqlsrv_fetch_array($stmt))
{
// $type2=$row['dadd1'];
echo $row['dadd1'];
//echo '<option value ="'.$type2.'">'.$type2.'</option>';
}
?>
if you could help me, that would be really really awesome thank you!
Check it. Global variables should be in upper case.
$gadd = $_POST['strs'];
First, make sure you use the proper format for global variables $gadd = $_POST['strs'];
Second, check your query if it is working, try this
$sql=("Select distinct dadd1 from ostore where strnm = '$gadd'");
Most problably it's because you're missing the value in the option.
Use this:
echo " <option value='$x'> $x</option>" ;
instead of this:
echo " <option> $x</option>" ;
If still don't work, like was said before check if the query is working.
You can do that in chrome by the option "Inspect element" and the go to the "Network" tab. You can see what is sent through ajax and the return data.

How to get the complete value of an option tag?

I have this code using php
<select id="sem" name="y[]">
<option>Year</option>
<?php
$petsa = new DateTime();
$yr=$petsa->format('Y');
$a=$yr+1;//2015
$b=$yr-1;//2013
for($y=$b;$y<=$a;$y++)
{
$bb = $y+1;
echo '<option value="'.$y.'"'.'-'.$bb;
if(isset($_POST['y'])){
if (in_array($y."-".$bb,$_POST['y'])){
echo 'selected="selected"';
}
echo '/>'.$y."-".$bb;
}else
echo "<option value ='$y'"."-"."$bb".">".$y."-".$bb;
echo "</option>";
}
?>
</select>
if(isset($_POST['submit'])){
$sem= $_POST['sem'];//this is from other field
foreach ($_POST['y'] as $yer);
$osql2 = "INSERT INTO sy VALUES('$sem','$ss')";
if (mysql_query($osql2)){
echo "Successfully Added!";
}
This code work well almost but the problem is when i call the value of the option tag it just give me this value $ss=2014 and it should be like this $ss=2014-2015
what is wrong with my code? that code generate SY from 2013-2014 to 2015-2016
please help me.
Look at the generated HTML. Look at where the quotes around the attribute value are. They are currently very wrong.
Stop trying to generate HTML by mashing together lots of different strings. It becomes very hard to see what is going on.
$value = $y . "-" . $bb;
?>
<option value="<?php echo htmlspecialchars($value); ?>">
<?php echo htmlspecialchars($value); ?>
</option>
<?php
I suspect it's your use of quotes here:
echo "<option value ='$y'"."-"."$bb".">".$y."-".$bb;
I would change it to this:
echo "<option value ='$y"."-"."$bb"."'>".$y."-".$bb;
I have moved the second ' to just before the > of the opening option tag.
The single quote ' was for the option tag, and the double quote " was for the PHP string, you were simply cutting the value for option short

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 ^

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