I have the following HTML/PHP sign up form:
<div class="form_block" id="dob-check" style="display:none; float:left">
<p><label for="sign_up_dob_day">DOB</label> <select name="sign_up_dob_day" id="sign_up_dob_day" class="required"><option value="">-</option><?php for($day = 1; $day <= 31; $day++) : ?>
<option value="<?php echo $day ?>" <?php echo set_select('sign_up_dob_day', $day, ((is_array($dob) && count($dob) == 3 && $dob[2]==$day)? true : false)); ?>><?php echo $day ?></option>
<?php endfor; ?>
</select>
<select name="sign_up_dob_month" class="required">
<option value="">-</option>
<?php $months = getMonthsArray(); ?>
<?php for($month = 1; $month <= 12; $month++) : ?>
<option value="<?php echo $month ?>" <?php echo set_select('sign_up_dob_month', $month, ((is_array($dob) && count($dob) == 3 && $dob[1]==$month)? true : false)); ?>><?php echo $months[$month - 1] ?></option>
<?php endfor; ?>
</select>
<select name="sign_up_dob_year" class="required">
<option value="">-</option>
<?php for($year = 2005; $year >= 1930; $year--) : ?>
<option value="<?php echo $year ?>" <?php echo set_select('sign_up_dob_year', $year, ((is_array($dob) && count($dob) == 3 && $dob[0]==$year || (empty($dob[0]) && $year==1990)))? true : false); ?>><?php echo $year ?></option>
<?php endfor; ?>
</select>
<?php echo form_error('sign_up_dob_day'); ?>
<a class="hand" id="dob-why" onclick="showMessage()"> Why do we need this?</a></p>
</div>
Which displays as follows:
However, if a user fails to enter a DOB, the formatting of the select boxes gets completely messed up, as below:
I guess its because the width of the div is not enough when adding the 'field required' text.
I have tried putting an outer div but this did not work. I have also tried floating left, but this also did not work.
I am new to PHP so cannot see how to remove the second 'field required' message-this may possibly help?
Any other ideas eg prevent a new line being formed within the div?
Try using display: inline-block on inputs and error messages instead of display: block.
Should fix the problem.
Related
I've got a dynamic select list, which is generated from MySQL. I've got no problem listing them, but I cant seem to get it's value when I submit the form. Here's my script for the Select:
<div class="form-group">
<select class="form-control" id="make" name="make">
<option value="">Make</option>
<?php
if ($result->num_rows > 0) {
$x = 1;
While($row = $result->fetch_assoc()) {
?>
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
<?php
$x = $x + 1;
}
}?>
</select>
</div>
And here's the script to get it's value:
if ($_POST["submit"]) {
$make = $_POST['make'];
when I do an echo for $make, I don't get anything at all. What went wrong? All help appreciated. Thanks
You forgot to echo $row['fmake'] and quotation marks mistake.
change this
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
by
<option value="<?php echo $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
shouldn't it be $row['fmake'] instead of $row[fmake]
<option value="<?php $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
Believe you're missing an echo.
value="<?php $row[fmake];?>"
Should be:
value="<?php echo $row[fmake];?>"
I'm using a date range on a web page to aggregate MySQL data and present it based on the selected time frame. For some reason the values for each option in the drop-down menus are not displaying. Here's the PHP I'm using:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option value="<?=$date ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?><?=$date ?></option>
<?php } ?>
</select>
And here's the HTML output:
<option value="2015-01-03" selected="selected" 2015-01-03<="" option=""></option>
The weirdest part is this was working for the longest time and suddenly the dates in both menus vanished. Any ideas why?
This happened becuase you are missing the ending > of tag
Modified code:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option value="<?=$date ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?>>
<?=$date ?>
</option>
<?php } ?>
</select>
Your PHP snippet is missing a closing >:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option
value="<?=$date ?>"
<?php if($date == $date1) { ?> selected="selected"<?php } ?>
>
<?=$date ?>
</option>
<?php } ?>
</select>
Try this way also :
<select name="per1" id="per1">
<option selected="selected">Choose one</option>
<?php
foreach($names as $name) { ?>
<option value="<?= $name['name'] ?>"><?= $name['name'] ?></option>
<?php
} ?>
</select>
Now you can put your code here.
1. Add > tag closer after selected attribute
2. Remove shorthanded <?= ?> tag which is unwanted while you also using <?php ?>
<select name="date1" title="<?php print $date1; ?>">
<?php foreach($availableDates as $date) {
?><option value="<?php print $date; ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?>>
<?php print $date; ?>
</option>
<?php } ?>
</select>
I suggest this less error prone code.
$selHTML = '<select name="date1" title="'.$date1.'">';
foreach($availableDates as $date) {
$sel = ($date == $date1)?" selected":"";
$selHTML .= '<option value="'.$date.'"'.$sel.'>'.$date.'</option>';
}
$selHTML .= '</select>';
echo $selHTML;
My Drop down menu to choose a date does not work unless I hold down left click on my mouse.
Only the year drop down works correctly but the month and days only work if you hold your mouse button down.
Here is the website it is on: www.cipslimoshuttle.com/tickets
Here is the code:
<label>
Date of travel:
<select name="ticketYear">
<?php
$i = date('Y');
while($i <= date('Y')+5){
?>
<option <?php if(date('Y') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketDay">
<?php
$i = 1;
while($i <= 31){
?>
<option <?php if(date('j') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketMonth">
<?php
$months = array(
'01-January',
'02-February',
'03-March',
'04-April',
'05-May',
'06-June',
'07-July',
'08-August',
'09-September',
'10-October',
'11-November',
'12-December'
);
foreach($months as $month) {
$month = explode('-',$month);
?>
<option <?php if(date('m') == $month[0]) echo 'selected' ?> value="<?php echo $month[0] ?>"><?php echo $month[1] ?></option>
<?php
}
?>
</select>
</label>
The problem is with your label tag You need to end before the select box.Check this it will work
<label>
Date of travel: </label>
<select name="ticketYear">
<?php
$i = date('Y');
while($i <= date('Y')+5){
?>
<option <?php if(date('Y') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketDay">
<?php
$i = 1;
while($i <= 31){
?>
<option <?php if(date('j') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketMonth">
<?php
$months = array(
'01-January',
'02-February',
'03-March',
'04-April',
'05-May',
'06-June',
'07-July',
'08-August',
'09-September',
'10-October',
'11-November',
'12-December'
);
foreach($months as $month) {
$month = explode('-',$month);
?>
<option <?php if(date('m') == $month[0]) echo 'selected' ?> value="<?php echo $month[0] ?>"><?php echo $month[1] ?></option>
<?php
}
?>
</select>
How to make the current month selected by default in option select using php
Here is what i have tried so far.
$curmonth = date("F");
And to display the entire month
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
And according to the above code, I am assigning the current month as $curmonth, and inside loop assigning the $allmonth for entire, month.
And inside the Value
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
for checking if the current month equals all month and displaying the selected to make it select. But i am not getting result.. What i am getting is all the items are being displayed in the option select.
What i am missing ?
What you have missed is,
You are trying to display the selected inside the value
What you need to do is
<option value="<?php
echo $i;
?>"
<?php
if($allmonth==$curmonth)
{
echo ' selected';
}
?>
>
<?php
echo $allmonth;
}
?>
</option>
So, the result will be
<select >
<option value="1">
January<option value="2">
February<option value="3">
March<option value="4">
April<option value="5">
May<option value="6">
June<option value="7">
July<option value="8">
August<option value="9" selected>
September<option value="10">
October<option value="11">
November<option value="12">
December</option>
You're not correctly closing the <option> tags you are creating. Indenting you're code makes these issues more apparent:
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
//Close tag inside loop
?>
</option>
<?php
}
Hope you are wrong here
<option value="<?php
echo $i; ?>"
<?php
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
You have missed closing quotes for the value
Take a look on this example:
$current = date('F');
for($i = 1 ; $i <= 12; $i++) {
$month = date("F",mktime(0,0,0,$i,1,date("Y")));
if( $current == $month )
echo $month . " - Selected \r\n";
else
echo $month . "\r\n";
}
IN HTML FORMAT:
echo "<select>";
$current = date('F');
for($i = 1 ; $i <= 12; $i++) {
$month = date("F",mktime(0,0,0,$i,1,date("Y")));
if( $current == $month )
echo "<option value='$month' selected='selected'>" $month . "</option>";
else
echo "<option value='$month'>" $month . "</option>";
}
echo "</select>";
DEMO
You have error with closing quotes in the value of options in the line
<option value="<?php
echo $i; if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
^ // This is the quotes opened for value and is not properly closed
>
Here you have to close the quotes. As per your code it will display as:
<option value="9 selected" >
Also you are not closing <option> properly. Change your code to :
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php echo $i;?>" <?php if($curmonth==$allmonth){echo 'selected';}?> >
^ // close quotes here
<?php
echo $allmonth;?>
</option>
}
</select>
I am developing user edit form where users can edit their info. I wants only that date come in combo box which was user add in table as Date of Birth. In following code I successfully pull Month into Combo box which user added in table.
IN FOLLOWING LINE I RUN SELECT QUERY TO FETCH DATA FROM DB INCLUDING YEAR, MONTH AND DAY:
<?php
$cnic_selected=$_POST['all_cnic'];
if(isset($cnic_selected))
{
$query = mysql_query("SELECT refer_id, emp_name, emp_father, emp_cnic, YEAR(emp_dob) AS byear, MONTH(emp_dob) AS bmonth, DAY(emp_dob) AS bday FROM staff_users WHERE emp_cnic = '$cnic_selected'");
$count = mysql_num_rows($query);
while($row = mysql_fetch_object($query))
{
$ref = $row->refer_id;
$name = $row->emp_name;
$fname = $row->emp_father;
$cnic = $row->emp_cnic;
$byear = $row->byear;
$bmonth = $row->bmonth;
$bday = $row->bday;
}
?>
<html>
<head>
</head>
<body>
<table width="95%">
<form name="edituserform" action="" method="Post" onSubmit="return validateform(this);">
<tr><td width="125px"><b>Referral Code</b></td>
<td><?php echo $ref; ?></td></tr>
<tr><td><b>Full Name</b></td>
<td><input type="text" name="emp_name" id="emp_name" value="<?php echo $name; ?>" />
</td></tr>
<tr><td><b>Father's Name</b></td>
<td><input type="text" name="emp_father" id="emp_father" value="<?php echo $fname; ?>" />
</td></tr>
<tr><td><b>CNIC Number</b></td>
<td><?php echo $cnic; ?></td></tr>
<tr><td><b>Date of Birth</b></td>
<td>
/* Run Loop to generate 31 days of the month */
<select name="dt">
<option value='--'>--</option>
<?php
for ($d=1; $d<32; $d++)
{
"<option value='$d'>$d</option>";
}
?>
</select>
/* Here I manually type months list. Use "Selected" to fetch that month which user select in DB */
<select name="month">
<option value="--">--</option>
<option value='01' <?php echo ($bmonth == 1) ? 'selected="selected"': ''; ?>>
Jan</option>
<option value='02' <?php echo ($bmonth == 2) ? 'selected="selected"': ''; ?>>
Feb</option>
<option value='03' <?php echo ($bmonth == 3) ? 'selected="selected"': ''; ?>>
Mar</option>
<option value='04' <?php echo ($bmonth == 4) ? 'selected="selected"': ''; ?>>
Apr</option>
<option value='05' <?php echo ($bmonth == 5) ? 'selected="selected"': ''; ?>>
May</option>
<option value='06' <?php echo ($bmonth == 6) ? 'selected="selected"': ''; ?>>
Jun</option>
<option value='07' <?php echo ($bmonth == 7) ? 'selected="selected"': ''; ?>>
Jul</option>
<option value='08' <?php echo ($bmonth == 8) ? 'selected="selected"': ''; ?>>
Aug</option>
<option value='09' <?php echo ($bmonth == 9) ? 'selected="selected"': ''; ?>>
Sep</option>
<option value='10' <?php echo ($bmonth == 10) ? 'selected="selected"': ''; ?>>
Oct</option>
<option value='11' <?php echo ($bmonth == 11) ? 'selected="selected"': ''; ?>>
Nov</option>
<option value='12' <?php echo ($bmonth == 12) ? 'selected="selected"': ''; ?>>
Dec</option>
</select>
<select name="year">
<option value='--'>--</option>
<?php
for ($y=2000; $y>1950; $y--)
{
echo "<option value='$y'>$y</option>";
}
?>
</select>
</td></tr>
</table>
</body>
</html>
Now my question is Please tell how to pull Year and Day from table into combo box which user was added while I am using Loop to generate years and days!??
Regards,
MAT
For days you are missing an echo and a check as to which day is selected
<?php
for ($d=1; $d<32; $d++)
{
"<option value='$d'>$d</option>";
}
?>
Should be
<?php
for ($d=1; $d<32; $d++)
{
if($d==$bday)
{
echo "<option value='$d' selected='selected'>$d</option>";
}
else
{
echo "<option value='$d'>$d</option>";
}
}
?>
Same for year
for ($y=2000; $y>1950; $y--)
{
if($y==$byear)
{
echo "<option value='$y' selected='selected'>$y</option>";
}
else
{
echo "<option value='$y'>$y</option>";
}
}