hit a wall on what should be a non-issue. Have a PHP echo with two concatenations in them to populate option values for a pull-down menu in a form, with the data for the option values being pulled from a MySQL database query. Works great, except that for some reason the option value will ONLY display the first word, and when it encounters a space in the value it drops it out of the option value field and populates the second, third words prior to the option value.
Here's the code:
<td width="160"><p> Job Location:</p><select class="listmedium" id="JobLocation" name="JobLocation"><option value='' selected="selected"></option>
<?php
$con = mysql_connect("Values Deleted");
if (!$con)
{die('Could not connect: ' . mysql_error());}
mysql_select_db ("database1", $con);
$result = mysql_query("SELECT CustomerLoc FROM customerloc ORDER BY CustomerLoc ASC");
while($row = mysql_fetch_array($result))
{echo "<option value=" . $row['CustomerLoc'] . ">" . $row['CustomerLoc'] ."</option>";}
mysql_close($con); ?>
</select>
Which outputs as:
<td width="160">
<p> Job Location:</p>
<select id="JobLocation" class="listmedium" name="JobLocation">
<option selected="selected" value=""></option>
<option springs="" value="Carrizo">Carrizo Springs</option>
<option wells="" value="Artesia">Artesia Wells</option>
</select>
Notice the "springs" and "wells" place themselves BEFORE the option value quotes where the concantenation is intended to place the entire value, which should read "Carrizo Springs" and "Artesia Wells" for the option values.
Now, if I insert a   manually between the values, the entire value shows up as "Carrizo Springs" and works (but I don't want to insert   in each if I don't have to) and also if I manually code the HTML values to read "Carrizo Springs" it then works fine as an option value, thus it seems to be something happening with the PHP data call.
I know it is not ideal to use text in place of integers in option values, but there is a reason behind it, and I would appreciate any help from ya'll if someone knows why the PHP would display the output in that manner.
The problem is this row:
"<option value=" . $row['CustomerLoc'] . ">"
it becoming to <option value=Carrizo Springs >
so it is like value=Carrizo ,to solve this just change to this:
"<option value=\"" . $row['CustomerLoc'] . "\">"
Related
So got a small problem here, tried so many different solution but it always ends up sending a blank value to the database. So i got a dropdown with two values and one value that is never going to be allowed to make a POST. So if you accidentally click on the "Oppdater" button before selecting a value from the dropdown menu i dont want it to be send, but now it does and just adds a blank value to the database. Example:
<form action="" method="post">
<input type="hidden" name="id" value="' . $row['id'] . '">
<select name="status2" class="endre-status">
<option selected="selected" disabled="disabled">Change status</option>
<option value="Done">Done</option>
<option value="Pending">Pending</option>
</select>
<input type="submit" name="insert2" value="Oppdater">
</form>
Here you can see that i have it selected and disabled and that should mean it will not be sent when i check with isset(), but it still does and puts a blank value in my database.
Here is the code i use to update the database with the new value from the dropdown.
if (isset($_POST['insert2']))
{
$status2 = $_POST['status2'];
$id = $_POST['id'];
$sql2 = ("UPDATE test3 SET status='$status2' WHERE id='$id'");
if (mysqli_query($conn, $sql2)) {
header("Location: index.php");
exit;
} else {
echo "Error: " . $sql2 . "<br>" . mysqli_error($conn);
}}
Here it how it looks:
I have also tried with setting the value of the disabled option to "0" and checking with empty(), but it still sends "0" to the database. If someone can help me that would be very nice. Thank you.
If your options are static you can check it that if value is from your options or not?
in_array function can help you
for example:
$options = array("Done","Pending");
if (in_array($_POST['status2'], $options)) {
in line :
<option selected="selected" disabled="disabled">Change status</option>
you have mentioned selected so by default , the first option would be returned as selected option and it seems logical that it returns 0 .
Suggested solution :
set a break point on insert2 button and leave dropdown empty , then check the returned value . at last try to prevent code side to send that value by control statements such as If
I am grabbing some values from my database and putting them in a dropdown select box. For some reason, there is a blank value after each option, which is weird as my database doesn't contain any blank fields.
<select id="school" name="school_name">
<option value=0> Choose</option>
<?php
$con = mysqli_connect("localhost", "root", "****") or die("error".mysqli_error());
mysqli_select_db($con, "newprojectdb") or die("error in database".mysqli_error());
$sql="SELECT school_id, school_name FROM schools";
$result=mysqli_query($con, $sql);
while($row= mysqli_fetch_array($result)){
$school_name=$row["school_name"];
echo '<OPTION VALUE=\'$school_name\'>'.$school_name.'<OPTION>';
}
mysqli_close ($con);
?>
</select>
The reason why you are getting a blank space between each value is because of the last <OPTION> which the closing / was missing and should have been closed like this </OPTION>
I also noticed you escaped the single quotes for
echo '<OPTION VALUE=\'$school_name\'>'.$school_name.'<OPTION>';
^^ ^^ ^ missing the / slash
which would not have echo'ed the value in HTML source but the variable itself $school_name
Sidenote: Variables are not parsed properly when set inside single quotes which is what you were doing in escaping the single quotes.
Example:
<OPTION VALUE='$school_name'>
Change it to:
echo '<OPTION VALUE="'.$school_name.'">'.$school_name.'</OPTION>';
and it will work.
You can also do it this way: (escaping the double quotes for VALUE instead).
echo "<OPTION VALUE=\"$school_name\">".$school_name."</OPTION>";
Footnotes
For cleaner and more readable HTML, use a concatenated . "\n" at the end like this:
echo "<OPTION VALUE=\"$school_name\">".$school_name."</OPTION>" . "\n";
which will produce something like:
<OPTION VALUE="St-Alexander">St-Alexander</OPTION>
<OPTION VALUE="St-Peter">St-Peter</OPTION>
<OPTION VALUE="St-John">St-John</OPTION>
instead of
<OPTION VALUE="St-Alexander">St-Alexander</OPTION><OPTION VALUE="St-Peter">St-Peter</OPTION><OPTION VALUE="St-John">St-John</OPTION>
Pleae find changes you need. Thanks
CHANGE
echo '<OPTION VALUE=\'$school_name\'>'.$school_name.'<OPTION>';
TO
echo "<option value=\"$school_name\"> $school_name </option>";
I want to create a php page that contains a html drop down list of people's names (as the option
text) and then their age (as the value). Below is my form for you to see (almost what I mean) which I hard coded:
<form>
<select name="nameOption">
<option value="">Select your name:</option>
<option value="45">Mary Smith</option>
<option value="16">Lily Roe</option>
<option value="32">Elliot Perkins</option>
</select>
<p><input type="submit" name="submit" value="Submit"/>
<input type="reset" value="Reset" />
</form>
What I want to do (or been trying to do) is to create the drop down list by running a SQL query to obtain the data (the people's name and age) from my database (unlike what I written above) and then when I click on one of the options, only their value or age should appear. So basically, I need to implement the data from the database into a drop down list
Now it's here where I am stuck. I am familiar with writing SQL statements for tables but I seem to get puzzled when I try to create a SQL statement for a drop down list in a php tag.
How would I write it? Like:
$sql = "SELECT name, age FROM person WHERE name = ". $person. ";
or
$nameOption = $_POST['nameOption'];
print_r ($nameOption);
with selecting a database:
$conn = mysql_connect("localhost", " ", " ");
mysql_select_db(" ", $conn)
I know it may seem like a dull answer but I need help. How would I implement SQL query to a drop down list? I would love your help.
As you have to enclose string in quotes, change your query to
$sql = "SELECT name, age FROM person WHERE name = '$person'";
and for showing dropdown dynamically you can do like
$query=mysql_query($sql);
echo '<select name="nameOption">
<option value="">Select your name:</option>';
while($result=mysql_fetch_array($query))
{
echo '<option value="'.$result['age'].'">'.$result['name'].'</option>';
}
echo '</select>';
You should do like that
<select name="nameOption">
<?php
$query = mysql_query($sql);
while( $row = mysql_fetch_array($query) ) {
echo '<option value="'.$row['age'].'">"'.$row['name'].'"</option>';
}
?>
</select>
You need to get the full list of people from the database first, then iterate through that outputting each option tag for each row:
$cnx = new Mysqli('localhost', 'username', 'password', 'database_name');
$people = $cnx->query("SELECT name, age FROM person");
echo '<select name="nameOption">';
while($person = $people->fetch_assoc())
{
echo '<option value="' . $person['age'] . '">' . $person['name'] . '</option>';
}
echo '</select>';
So this should be my last question for this entire project. I've made the employee timecard pages. Works great. I'm trying to make them a review/change page per timecard. Basically the initial review page pulls up the timecards they've submitted. They select Review. It carries the ID number to a review page, and I call upon the ID number to display all the info from that timecard into the new timecard html/php form. (Basically its the exact same form they used to submit, however I've echo'd the values as the option already)
When I do update though it's not only carrying over the echo'd value. It's only Updating any changes they make, and in fact deleting everything else. Below is a snippit of just one drop-down list (all over non drop-down's update fine. This is just for drop down lists where the data is contained in the DB.
<select name="starttime" id="input_6" style="width:150px">
<option value="" selected><?php echo $stime ?></option>
<?php
$result = mysql_query("SELECT time FROM selTime ORDER BY id");
while($row = mysql_fetch_array($result)) {
echo "<option value=\"" . $row['time'] . "\">" . $row['time'] . " </option>";
}
?>
</select>
So what happens with this code. Is when they open the review page They would see their start time as they submitted it. If they leave it alone, and not change anything and push submit after changing something else. The starttime actually UPDATES as blank. If they change the starttime to a value it DOES submit the change. Is there something in this code that I can change that will submit the echo'd value, instead of just displaying it?
I believe the issue is with this line of code:
<option value="" selected><?php echo $stime ?></option>
Although you set it as "selected" the selected element has the value of "" (empty). Try changing it to:
<option value="<?php echo $stime; ?>" selected="selected"><?php echo $stime; ?></option>
If they dont change this select list item, and submit the page directly, the selected item is the first one with the value="", so this will update in the database with blank ( null).
You need to give your fist item a value of date time now. This code here:
<option value="DATETINMENOW" selected><?php echo $stime ?></option>
I generated a drop down menu with the code below. How can I set the field to the GET variable after submit is clicked?
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
$options.="<option value=\"$id\">".$c;
}
?>
<option value=''>C <?=$options?> </option>
</select>
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
if($_GET['var'] == $id)
echo '<option selected="selected" value=\"$id\">' . $c . '</option>';
else
echo '<option value=\"$id\">' . $c . '</option>';
}
?>
</select>
Basic idea is compare value GET data with database data and using if else condition add selected="selected" if condition matched. I am directly printing string as they will not be getting use later on.
I'm a bit confused about what you want to know. To have an option selected by default, use the selected="selected" attribute on that option.
If you want to know how to get the submitted value in PHP, you need to assign the name attribute to the <select> tag. In general, however, you've got the idea right.