PHP Form Validation with preg_match and regular expressions - php

Hello everyone I am currently trying to validate user input in a form. I am using regular expressions and the php preg_match function. I am trying to do this with the email, phone number, and zip code. However, when the user enters an email, phone number, or zip code that doesn't fit the regular expression, nothing happens, the user is supposed to get an error message when this happens. So I believe my regular expression is not being read and I have no idea why. Thank you for any help.
Here is my code:
<?php
if(isset($_POST['order'])){
$fname = trim(htmlspecialchars($_POST['first_name']));
echo $fname;
$address = trim(htmlspecialchars($_POST['address']));
echo $address;
$city = trim(htmlspecialchars($_POST['city']));
echo $city;
$zipcode = trim(htmlspecialchars($_POST['zip_code']));
echo $zipcode;
$email = trim(htmlspecialchars($_POST['email']));
echo $email;
$validzip = "/^\d{5}$|^\d{5}-\d{4}$/";
$validemail = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/";
$state = $_POST['states'];
echo $state;
$lname = trim(htmlspecialchars($_POST['last_name']));
echo $lname;
$phonenumber = trim(htmlspecialchars($_POST['phone_number']));
echo $phonenumber;
$vaildpn = "/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/";
if(isset($_POST['first_name']) && $fname!= "" && isset($_POST['address'])&& $address !="" && isset($_POST['city']) && $city != "" && isset($_POST['zip_code']) && $zipcode != ""&& isset($_POST['email']) && $email != "" && isset($_POST['last_name']) && $lname != "" && isset($_POST['phone_number']) && $phonenumber != "" && isset($_POST['card_Number']) && $cardnumber != "" && isset($_POST['exp_date']) && $expdate != "" && isset($_POST['cvc']) && $cvc != "" && preg_match($validzip, $zipcode) && preg_match($validemail, $email)){
header("Location: orderplaced.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Confrimation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="images/favicon.ico">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/confirm.css">
</head>
<body>
<header>
<h1>Order Confrimation</h1>
</header>
<main>
<nav>
<ul>
<form action="cart.php">
<li><button type="submit" name="back" value="Back to Cart">Back to Cart</button></li>
</form>
<form action="index.php">
<li><button type="submit" action="index.php" name="shopping">Return to Shopping</button></li>
</form>
</ul>
</nav>
<form method="post" name="shipping" action="confirm.php">
<h2>Please enter your shipping information:</h2>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="first_name"
<?php
if(isset($_POST['first_name'])&& $fname==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['fname'])){
echo "value=$fname>";
}
else{
echo"><span>*</span>";
}
?>
<input type="hidden" name="fname2" value="<?php echo "$fname2"; ?>" />
<br>
<label for="lname">Last Name:</label>
<input type="text" id="lname" name="last_name"
<?php
if(isset($_POST['last_name'])&& $lname==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['lname'])){
echo "value=$lname>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for="email">Email:</label>
<input type="text" id="email" name="email"
<?php
if(isset($_POST['email'])&& $email==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['email'])){
echo "value=$email>";
}
else if(isset($_POST['email']) && !preg_match($validemail, $email)){
echo "value=$email>";
echo "<span>Please enter a vaild email</span>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for="address">Address:</label>
<input type="text" id="address" name="address"
<?php
if(isset($_POST['address'])&& $address==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['address'])){
echo "value=$address>";
}
else{
echo"><span>*</span>";
}
?><br>
<label for ="state">State</label>
<select name="states">
<option value="AL"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AL') ? 'selected="selected"' : ''; ?>>Alabama</option>
<option value="AK"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AK') ? 'selected="selected"' : ''; ?>>Alaska</option>
<option value="AZ"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AZ') ? 'selected="selected"' : ''; ?>>Arizona</option>
<option value="AR"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AR') ? 'selected="selected"' : ''; ?>>Arkansas</option>
<option value="CA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'CA') ? 'selected="selected"' : ''; ?>>California</option>
<option value="CO"<?php echo (isset($_POST['states']) && $_POST['states'] == 'CO') ? 'selected="selected"' : ''; ?>>Colorado</option>
<option value="CT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'CT') ? 'selected="selected"' : ''; ?>>Connecticut</option>
<option value="DE"<?php echo (isset($_POST['states']) && $_POST['states'] == 'DE') ? 'selected="selected"' : ''; ?>>Delaware</option>
<option value="FL"<?php echo (isset($_POST['states']) && $_POST['states'] == 'FL') ? 'selected="selected"' : ''; ?>>Florida</option>
<option value="GA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'GA') ? 'selected="selected"' : ''; ?>>Georgia</option>
<option value="HI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'HI') ? 'selected="selected"' : ''; ?>>Hawaii</option>
<option value="ID"<?php echo (isset($_POST['states']) && $_POST['states'] == 'ID') ? 'selected="selected"' : ''; ?>>Idaho</option>
<option value="IL"<?php echo (isset($_POST['states']) && $_POST['states'] == 'IL') ? 'selected="selected"' : ''; ?>>Illinois</option>
<option value="IN"<?php echo (isset($_POST['states']) && $_POST['states'] == 'IN') ? 'selected="selected"' : ''; ?>>Indiana</option>
<option value="IA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'IA') ? 'selected="selected"' : ''; ?>>Iowa</option>
<option value="KS"<?php echo (isset($_POST['states']) && $_POST['states'] == 'KS') ? 'selected="selected"' : ''; ?>>Kansas</option>
<option value="KY"<?php echo (isset($_POST['states']) && $_POST['states'] == 'KY') ? 'selected="selected"' : ''; ?>>Kentucky</option>
<option value="LA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'LA') ? 'selected="selected"' : ''; ?>>Louisiana</option>
<option value="ME"<?php echo (isset($_POST['states']) && $_POST['states'] == 'ME') ? 'selected="selected"' : ''; ?>>Maine</option>
<option value="MD"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MD') ? 'selected="selected"' : ''; ?>>Maryland</option>
<option value="MA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MA') ? 'selected="selected"' : ''; ?>>Massachusetts</option>
<option value="MI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MI') ? 'selected="selected"' : ''; ?>>Michigan</option>
<option value="MN"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MN') ? 'selected="selected"' : ''; ?>>Minnesota</option>
<option value="MS"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MS') ? 'selected="selected"' : ''; ?>>Mississippi</option>
<option value="MO"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MO') ? 'selected="selected"' : ''; ?>>Missouri</option>
<option value="MT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MT') ? 'selected="selected"' : ''; ?>>Montana</option>
<option value="NE"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NE') ? 'selected="selected"' : ''; ?>>Nebraska</option>
<option value="NV"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NV') ? 'selected="selected"' : ''; ?>>Nevada</option>
<option value="NH"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NH') ? 'selected="selected"' : ''; ?>>New Hampshire</option>
<option value="NJ"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NJ') ? 'selected="selected"' : ''; ?>>New Jersey</option>
<option value="NM"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NM') ? 'selected="selected"' : ''; ?>>New Mexico</option>
<option value="NY"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NY') ? 'selected="selected"' : ''; ?>>New York</option>
<option value="NC"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NC') ? 'selected="selected"' : ''; ?>>North Carolina</option>
<option value="ND"<?php echo (isset($_POST['states']) && $_POST['states'] == 'ND') ? 'selected="selected"' : ''; ?>>North Dakota</option>
<option value="OH"<?php echo (isset($_POST['states']) && $_POST['states'] == 'OH') ? 'selected="selected"' : ''; ?>>Ohio</option>
<option value="OK"<?php echo (isset($_POST['states']) && $_POST['states'] == 'OK') ? 'selected="selected"' : ''; ?>>Oklahoma</option>
<option value="OR"<?php echo (isset($_POST['states']) && $_POST['states'] == 'OR') ? 'selected="selected"' : ''; ?>>Oregon</option>
<option value="PA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'PA') ? 'selected="selected"' : ''; ?>>Pennsylvania</option>
<option value="RI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'RI') ? 'selected="selected"' : ''; ?>>Rhode Island</option>
<option value="SC"<?php echo (isset($_POST['states']) && $_POST['states'] == 'SC') ? 'selected="selected"' : ''; ?>>South Carolina</option>
<option value="SD"<?php echo (isset($_POST['states']) && $_POST['states'] == 'SD') ? 'selected="selected"' : ''; ?>>South Dakota</option>
<option value="TN"<?php echo (isset($_POST['states']) && $_POST['states'] == 'TN') ? 'selected="selected"' : ''; ?>>Tennessee</option>
<option value="TX"<?php echo (isset($_POST['states']) && $_POST['states'] == 'TX') ? 'selected="selected"' : ''; ?>>Texas</option>
<option value="UT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'UT') ? 'selected="selected"' : ''; ?>>Utah</option>
<option value="VT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'VT') ? 'selected="selected"' : ''; ?>>Vermont</option>
<option value="VA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'VA') ? 'selected="selected"' : ''; ?>>Virginia</option>
<option value="WA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WA') ? 'selected="selected"' : ''; ?>>Washington</option>
<option value="WV"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WV') ? 'selected="selected"' : ''; ?>>West Virginia</option>
<option value="WI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WI') ? 'selected="selected"' : ''; ?>>Wisconsin</option>
<option value="WY"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WY') ? 'selected="selected"' : ''; ?>>Wyoming</option>
</select>
<label for ="city">City:</label>
<input type="text" id="city" name="city"
<?php
if(isset($_POST['city'])&& $city==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['city'])){
echo "value=$city>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for ="zipcode">Zip Code:</label>
<input type="text" id="zipcode" name="zip_code"
<?php
if(isset($_POST['zip_code'])&& $zipcode==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['zip_code'])){
echo "value=$zipcode>";
}
else if(isset($_POST['zip_code'])&& !preg_match($validzip, $zipcode) ){
echo "value=$zipcode>";
echo "<span>Please enter a vaild zip-code</span>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for ="phonenumber">Phone Number:</label>
<input type="text" id="phonenumber" name="phone_number"
<?php
if(isset($_POST['phone_number'])&& $phonenumber==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['phone_number'])){
echo "value=$phonenumber>";
}
else if(isset($_POST['phone_number'])&& !preg_match($vaildpn, $phonenumber)){
echo "value=$phonenumber>";
echo "<span>Invaild Phone Number, please enter between 7-10 digts</span>";
}
else{
echo"><span>*</span>";
}
?>
<button type="submit" name="order" id="order" value="Checkout">Checkout</button>
</form>
</main>
<footer>
Sierra Sprungl – CS3800 Final Project - Spring 2020
</footer>

There is no $_POST['order'] being submitted. Try adding this to your form:
<input type="hidden" value="1" name="order">
I would also advise you to use better validation methods like filter_var

Related

How to deal large select with PHP (array, loop or casual)

I have a dilemma with large select boxes on the website. If I have a select box that contains more than 300 options, should I use array, loop or just add each option?
I have to use the for loop function to specify later whether the user has this option selected or not (checking with MySQLi database).
I care about the speed of the code.
Option 1 - Casual code:
<optgroup label='{$lang['c_010']}'>
<option value='{$i_array_value[0]}' ".($g_interests_row_0 == 1 ? 'selected' : '').">{$i_array_name[0]}/option>
<option value='{$i_array_value[1]}' ".($g_interests_row_1 == 1 ? 'selected' : '').">{$i_array_name[1]}/option>
<option value='{$i_array_value[2]}' ".($g_interests_row_2 == 1 ? 'selected' : '').">{$i_array_name[2]}/option>
<option value='{$i_array_value[3]}' ".($g_interests_row_3 == 1 ? 'selected' : '').">{$i_array_name[3]}/option>
<option $p_2_t data-subtext='{$lang['c_015']}' disabled></option>
<option value='{$i_array_value[4]}' ".($g_interests_row_4 == 1 ? 'selected' : '').">{$i_array_name[4]}/option>
<option value='{$i_array_value[5]}' ".($g_interests_row_5 == 1 ? 'selected' : '').">{$i_array_name[5]}/option>
<option value='{$i_array_value[6]}' ".($g_interests_row_6 == 1 ? 'selected' : '').">{$i_array_name[6]}/option>
<option value='{$i_array_value[7]}' ".($g_interests_row_7 == 1 ? 'selected' : '').">{$i_array_name[7]}/option>
<option value='{$i_array_value[8]}' ".($g_interests_row_8 == 1 ? 'selected' : '').">{$i_array_name[8]}/option>
<option $p_2_t data-subtext='{$lang['c_021']}' disabled></option>
<option value='{$i_array_value[9]}' ".($g_interests_row_9 == 1 ? 'selected' : '').">{$i_array_name[9]}/option>
<option value='{$i_array_value[10]}' ".($g_interests_row_10 == 1 ? 'selected' : '').">{$i_array_name[10]}/option>
<option value='{$i_array_value[11]}' ".($g_interests_row_11 == 1 ? 'selected' : '').">{$i_array_name[11]}/option>
<option value='{$i_array_value[12]}' ".($g_interests_row_12 == 1 ? 'selected' : '').">{$i_array_name[12]}/option>
<option value='{$i_array_value[13]}' ".($g_interests_row_13 == 1 ? 'selected' : '').">{$i_array_name[13]}/option>
<option value='{$i_array_value[14]}' ".($g_interests_row_14 == 1 ? 'selected' : '').">{$i_array_name[14]}/option>
<option value='{$i_array_value[15]}' ".($g_interests_row_15 == 1 ? 'selected' : '').">{$i_array_name[15]}/option>
<option value='{$i_array_value[16]}' ".($g_interests_row_16 == 1 ? 'selected' : '').">{$i_array_name[16]}/option>
<option value='{$i_array_value[17]}' ".($g_interests_row_17 == 1 ? 'selected' : '').">{$i_array_name[17]}/option>
<option value='{$i_array_value[18]}' ".($g_interests_row_18 == 1 ? 'selected' : '').">{$i_array_name[18]}/option>
<option $p_2_t data-subtext='{$lang['c_033']}' disabled></option>
<option value='{$i_array_value[19]}' ".($g_interests_row_19 == 1 ? 'selected' : '').">{$i_array_name[19]}/option>
<option value='{$i_array_value[20]}' ".($g_interests_row_20 == 1 ? 'selected' : '').">{$i_array_name[20]}/option>
<option value='{$i_array_value[21]}' ".($g_interests_row_21 == 1 ? 'selected' : '').">{$i_array_name[21]}/option>
<option value='{$i_array_value[22]}' ".($g_interests_row_22 == 1 ? 'selected' : '').">{$i_array_name[22]}/option>
<option value='{$i_array_value[23]}' ".($g_interests_row_23 == 1 ? 'selected' : '').">{$i_array_name[23]}/option>
<option value='{$i_array_value[24]}' ".($g_interests_row_24 == 1 ? 'selected' : '').">{$i_array_name[24]}/option>
<option value='{$i_array_value[25]}' ".($g_interests_row_25 == 1 ? 'selected' : '').">{$i_array_name[25]}/option>
<option value='{$i_array_value[26]}' ".($g_interests_row_26 == 1 ? 'selected' : '').">{$i_array_name[26]}/option>
<option value='{$i_array_value[27]}' ".($g_interests_row_27 == 1 ? 'selected' : '').">{$i_array_name[27]}/option>
<option value='{$i_array_value[28]}' ".($g_interests_row_28 == 1 ? 'selected' : '').">{$i_array_name[28]}/option>
<option $p_2_t data-subtext='{$lang['c_044']}' disabled></option>
<option value='{$i_array_value[29]}' ".($g_interests_row_29 == 1 ? 'selected' : '').">{$i_array_name[29]}/option>
<option value='{$i_array_value[30]}' ".($g_interests_row_30 == 1 ? 'selected' : '').">{$i_array_name[30]}/option>
<option value='{$i_array_value[31]}' ".($g_interests_row_31 == 1 ? 'selected' : '').">{$i_array_name[31]}/option>
<option value='{$i_array_value[32]}' ".($g_interests_row_32 == 1 ? 'selected' : '').">{$i_array_name[32]}/option>
<option value='{$i_array_value[33]}' ".($g_interests_row_33 == 1 ? 'selected' : '').">{$i_array_name[33]}/option>
<option value='{$i_array_value[34]}' ".($g_interests_row_34 == 1 ? 'selected' : '').">{$i_array_name[34]}/option>
<option value='{$i_array_value[35]}' ".($g_interests_row_35 == 1 ? 'selected' : '').">{$i_array_name[35]}/option>
<option value='{$i_array_value[36]}' ".($g_interests_row_36 == 1 ? 'selected' : '').">{$i_array_name[36]}/option>
<option value='{$i_array_value[37]}' ".($g_interests_row_37 == 1 ? 'selected' : '').">{$i_array_name[37]}/option>
</optgroup>
Option 2 - For loop:
for ($j = 0; $j < 367; $j++) {
$interests_rows = 'g_interests_row_'.$j;
$demographics_rows = 'g_demographics_row_'.$j;
$$interests_rows = $db->QueryGetNumRows("$modules_rows_count FIND_IN_SET('".$i_array_value[$j]."', interests)");
$$demographics_rows = $db->QueryGetNumRows("$modules_rows_count FIND_IN_SET('".$d_array_value[$j]."', demographics)");
if ($j == 0) {
echo "<optgroup label='{$lang['c_010']}'>";
} elseif ($j <= 4) {
echo $am_sh_di_1;
} elseif ($j == 5) {
echo "<option $p_2_t data-subtext='{$lang['c_015']}' disabled></option>";
} elseif ($j <= 8) {
echo $am_sh_di_1;
} elseif ($j <= 10) {
echo $am_sh_di_2;
} elseif ($j == 11) {
echo "<option $p_2_t data-subtext='{$lang['c_021']}' disabled></option>";
} elseif ($j <= 14) {
echo $am_sh_di_1;
} elseif ($j <= 22) {
echo $am_sh_di_2;
} elseif ($j == 23) {
echo "<option $p_2_t data-subtext='{$lang['c_033']}' disabled></option>";
} elseif ($j <= 33) {
echo $am_sh_di_1;
} elseif ($j == 34) {
echo "<option $p_2_t data-subtext='{$lang['c_044']}' disabled></option>";
} elseif ($j <= 38) {
echo $am_sh_di_1;
} elseif ($j <= 43) {
echo $am_sh_di_2;
} elseif ($j == 44) {
echo '</optgroup>';
}
if ($j >= count($d_array_value)) {
$r_interests = ${"g_interests_row_".($j - count($d_array_value)).""};
} else {
$r_demographics = ${"g_demographics_row_$j"};
}
}
Option 3 - Array (which is almost the same as Option 1):
$test = array(
"<optgroup label='{$lang['c_010']}'>",
array(
"<option value='{$i_array_value[0]}' ".($g_interests_row_0 == 1 ? 'selected' : '').">{$i_array_name[0]}/option>",
"<option value='{$i_array_value[1]}' ".($g_interests_row_1 == 1 ? 'selected' : '').">{$i_array_name[1]}/option>",
"<option value='{$i_array_value[2]}' ".($g_interests_row_2 == 1 ? 'selected' : '').">{$i_array_name[2]}/option>",
"<option value='{$i_array_value[3]}' ".($g_interests_row_3 == 1 ? 'selected' : '').">{$i_array_name[3]}/option>",
array(
"<option $p_2_t data-subtext='{$lang['c_015']}' disabled></option>",
array(
"<option value='{$i_array_value[4]}' ".($g_interests_row_0 == 1 ? 'selected' : '').">{$i_array_name[4]}/option>",
...
)
)
)
)
How I should do it?

Sticky dropdown selected only if there's error on submit

I try to make my dropdown selected only if error happen, and this is my script
<select name="usertype" id="usertype" class="form-control">
<option value="">Please choose the user right</option>
<option value="admin"<?php if(isset($error)
&& $_POST['usertype'] == 'admin' ? ' selected="selected"' : '');?>>
Admin
</option>
<option value="author"<?php if(isset($error)
&& $_POST['usertype'] == 'author' ? ' selected="selected"' : '');?>>
Author
</option>
<option value="public"<?php if(isset($error)
&& $_POST['usertype'] == 'public' ? ' selected="selected"' : '');?>>
Public
</option>
</select>
can anyone tell me the right way? because it doesn't work.
You're mixing up your ternary, its (condition) ? true : false. Here's a revised one:
<?php $usertype = array('admin', 'author', 'public'); ?>
<select name="usertype" id="usertype" class="form-control">
<option disabled selected>Please choose the user right</option>
<?php foreach($usertype as $val): ?>
<option
value="<?php echo $val; ?>"
<?php echo (isset($error, $_POST['usertype']) && $_POST['usertype'] == $val) ? 'selected="selected"' : ''; ?>
>
<?php echo ucfirst($val); ?>
</option>
<?php endforeach; ?>
</select>
try out this code:
<select name="usertype" id="usertype" class="form-control">
<option value="">Please choose the user right</option>
<option value="admin" <?php echo ((isset($error) && $_POST['usertype'] == 'admin') ? ' selected="selected"' : '');?>>Admin</option>
<option value="author" <?php echo ((isset($error)&& $_POST['usertype'] == 'author') ? ' selected="selected"' : '');?>>Author</option>
<option value="public" <?php echo ((isset($error) && $_POST['usertype'] == 'public') ? ' selected="selected"' : '');?>>Public</option>
</select>

Correct use of an If statement in echo?

I have a variable called $position which has a value selected from a database.
I am having an issue displaying the $position value in my dropdown menu as the selected value on page load.
The $position variable does echo out the correct value outside of this code.
I read about the Ternary Operator method so I have used that here instead of normal IF statment but it still does not work. Is my code correct? Is there another method I should use?
Thanks
My Code
$number = $_GET['id'];
$number = mysqli_real_escape_string($connect,$number);
$stmt = $connect->prepare("SELECT s_code, s_type, s_position, s_idno, s_firstnames, s_email, s_cellno FROM smco WHERE s_ainumber = ?") or die(mysqli_error());
$stmt->bind_param('s', $number);
$stmt->execute();
$stmt->bind_result($code, $type, $position, $idno, $name, $email, $cell);
$stmt->fetch();
$stmt->close();
echo "<td width=\"62%\" align=\"left\">
<p><b>Position:</b></p>
<select name=\"position\">
<option value=\"AQ\" (($position == 'AQ') ? \"selected='selected'\")>Account Queries</option>
<option value=\"KM\" (($position == 'KM') ? \"selected='selected'\")>Key Account Manager</option>
<option value=\"MD\" (($position == 'MD') ? \"selected='selected'\")>Managing Director</option>
<option value=\"RB\" (($position == 'RB') ? \"selected='selected'\")>Rebates</option>
<option value=\"BY\" (($position == 'BY') ? \"selected='selected'\")>Store Buyer</option>
<option value=\"OW\" (($position == 'OW') ? \"selected='selected'\")>Store Owner</option>
</select>
</td>";
echo '<option value="">' . (true ? 'true' : 'false') . '</option>';
In you case:
echo "<option value=\"AQ\"" . (($position == 'AQ') ? ' selected="selected"' : '') . ">Account Queries</option>";
You have to concat strings like that :
echo "foo is " . ($foo == true ? "true" : "false") . " whatever echo";
I'm not sure if I'm getting your question right but you can simplify this.
Just write a function like this:
function is_postion($value, $position){
if($value == $position) echo 'selected="selected"';
return;
}
Then simply put it inside the HTML rather that using echo for the whole thing. So:
<option value="AQ" <?php is_position('AQ', $position); ?> >Account Queries</option>
<td width='62%' align='left'>
<p><b>Position:</b></p>
<select name='position'>
<option value='AQ' <?php echo $position == 'AQ' ? 'selected' : '' ?>>Account Queries</option>
<option value='KM' <?php echo $position == 'KM' ? 'selected' : '' ?>>Key Account Manager</option>
<option value='MD' <?php echo $position == 'MD' ? 'selected' : '' ?>>Managing Director</option>
<option value='RB' <?php echo $position == 'RB' ? 'selected' : '' ?>>Rebates</option>
<option value='BY' <?php echo $position == 'BY' ? 'selected' : '' ?>>Store Buyer</option>
<option value='OW' <?php echo $position == 'OW' ? 'selected' : '' ?>>Store Owner</option>
</select>
</td>
First, you don't need to wrap all the HTML into the echo. It's enough to use PHP for dynamic parts only.
Second, you placed PHP code inside the string. So, it didn't worked.
Third, when using (expr) ? : (ternary operator) you missed the false branch.
Try using this.
<td width="62%" align="left">
<p><b>Position:</b></p>
<select name="position">
<option value="AQ"<?=$position == 'AQ' ? "selected='selected'" : ""?>>Account Queries</option>
<option value="KM"<?=$position == 'KM' ? "selected='selected'" : ""?>>Key Account Manager</option>
<option value="MD"<?=$position == 'MD' ? "selected='selected'" : ""?>>Managing Director</option>
<option value="RB"<?=$position == 'RB' ? "selected='selected'" : ""?>>Rebates</option>
<option value="BY"<?=$position == 'BY' ? "selected='selected'" : ""?>>Store Buyer</option>
<option value="OW"<?=$position == 'OW' ? "selected='selected'" : ""?>>Store Owner</option>
</select>
</td>
This will work for you:
echo "<td width=\"62%\" align=\"left\">
<p><b>Position:</b></p>
<select name=\"position\">
<option value=\"AQ\" ".(($position == "AQ") ? "Selected=selected" : "")." >Account Queries</option>
<option value=\"KM\" ".(($position == "KM") ? "Selected='selected'" : "")." >Key Account Manager</option>
<option value=\"MD\" ".(($position == "MD") ? "Selected='selected'" : "")." >Managing Director</option>
<option value=\"RB\" ".(($position == "RB") ? "Selected='selected'" : "")." >Rebates</option>
<option value=\"BY\" ".(($position == "BY") ? "Selected='selected'" : "")." >Store Buyer</option>
<option value=\"OW\" ".(($position == "OW") ? "Selected='selected'" : "").">Store Owner</option>
</select>
</td>";

How to remember posted fields in forms for drop down items?

I'm creating a simple registration page:
<?php
$firstName = $_POST['firstName'];
?>
<form action="registration.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>
<b>First name:</b>
</td>
<td>
<input type="text" name="firstName" size="30" maxlength="400" value="<?php echo $firstName; ?>" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
If the user enters an invalid first name (ex. too short, or weird symbols), the page reloads with an error message shows up to tell them that they have to re-enter their name HOWEVER, the value of the text field is what they most recently entered. This way whenever a user doesn't pass validation, they don't have to re-enter all the fields in the form.
What's a good way to remember what the user entered for a drop down menu? The problem for me is that the option value is different than the text inside the option tag. So when I use the above approach, if I select "Mar" if in invalid submission occurs, '03' appears in the dropdown menu.
<select name="birthdayMonth">
<option value="-1">Month:</option>
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">Jun</option>
<option value="07">Jul</option>
<option value="08">Aug</option>
<option value="09">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
At the top:
$birthdayMonth = $_POST['birthdayMonth']
In the select:
<select name="birthdayMonth">
<option value="-1">Month:</option>
<option value="01"<?php echo $birthdayMonth == '01' ? 'selected="selected"' : ''; ?>>Jan</option>
<option value="02"<?php echo $birthdayMonth == '02' ? 'selected="selected"' : ''; ?>>Feb</option>
<option value="03"<?php echo $birthdayMonth == '03' ? 'selected="selected"' : ''; ?>>Mar</option>
<option value="04"<?php echo $birthdayMonth == '04' ? 'selected="selected"' : ''; ?>>Apr</option>
<option value="05"<?php echo $birthdayMonth == '05' ? 'selected="selected"' : ''; ?>>May</option>
<option value="06"<?php echo $birthdayMonth == '06' ? 'selected="selected"' : ''; ?>>Jun</option>
<option value="07"<?php echo $birthdayMonth == '07' ? 'selected="selected"' : ''; ?>>Jul</option>
<option value="08"<?php echo $birthdayMonth == '08' ? 'selected="selected"' : ''; ?>>Aug</option>
<option value="09"<?php echo $birthdayMonth == '09' ? 'selected="selected"' : ''; ?>>Sep</option>
<option value="10"<?php echo $birthdayMonth == '10' ? 'selected="selected"' : ''; ?>>Oct</option>
<option value="11"<?php echo $birthdayMonth == '11' ? 'selected="selected"' : ''; ?>>Nov</option>
<option value="12"<?php echo $birthdayMonth == '12' ? 'selected="selected"' : ''; ?>>Dec</option>
</select>
This is a really dirty of what doing what you want, but it will work:
http://www.plus2net.com/php_tutorial/pb-drop.php
It would be better to use loop to build the dropdown and then add the selected="selected" value to the correct option.
<option value="03" <?php echo $_POST['birthdayMonth'] == '03' ?
'selected="selected"' : '' ?>>Mar</option>

How to create a loop for this dropdown menu?

And, would a loop be more efficient then listing them out one by one?
<select name="birthdayYear" >
<option value="0000"<?php echo $birthdayYear == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
<option value="2011"<?php echo $birthdayYear == '2011' ? 'selected="selected"' : ''; ?>>2011</option>
<option value="2010"<?php echo $birthdayYear == '2010' ? 'selected="selected"' : ''; ?>>2010</option>
<option value="2009"<?php echo $birthdayYear == '2009' ? 'selected="selected"' : ''; ?>>2009</option>
<option value="2008"<?php echo $birthdayYear == '2008' ? 'selected="selected"' : ''; ?>>2008</option>
<option value="2007"<?php echo $birthdayYear == '2007' ? 'selected="selected"' : ''; ?>>2007</option>
<option value="2006"<?php echo $birthdayYear == '2006' ? 'selected="selected"' : ''; ?>>2006</option>
<option value="2005"<?php echo $birthdayYear == '2005' ? 'selected="selected"' : ''; ?>>2005</option>
<option value="2004"<?php echo $birthdayYear == '2004' ? 'selected="selected"' : ''; ?>>2004</option>
<option value="2003"<?php echo $birthdayYear == '2003' ? 'selected="selected"' : ''; ?>>2003</option>
<option value="2002"<?php echo $birthdayYear == '2002' ? 'selected="selected"' : ''; ?>>2002</option>
<option value="2001"<?php echo $birthdayYear == '2001' ? 'selected="selected"' : ''; ?>>2001</option>
<option value="2000"<?php echo $birthdayYear == '2000' ? 'selected="selected"' : ''; ?>>2000</option>
<option value="1999"<?php echo $birthdayYear == '1999' ? 'selected="selected"' : ''; ?>>1999</option>
<option value="1998"<?php echo $birthdayYear == '1998' ? 'selected="selected"' : ''; ?>>1998</option>
<option value="1997"<?php echo $birthdayYear == '1997' ? 'selected="selected"' : ''; ?>>1997</option>
<option value="1996"<?php echo $birthdayYear == '1996' ? 'selected="selected"' : ''; ?>>1996</option>
<option value="1995"<?php echo $birthdayYear == '1995' ? 'selected="selected"' : ''; ?>>1995</option>
<option value="1994"<?php echo $birthdayYear == '1994' ? 'selected="selected"' : ''; ?>>1994</option>
<option value="1993"<?php echo $birthdayYear == '1993' ? 'selected="selected"' : ''; ?>>1993</option>
<option value="1992"<?php echo $birthdayYear == '1992' ? 'selected="selected"' : ''; ?>>1992</option>
<option value="1991"<?php echo $birthdayYear == '1991' ? 'selected="selected"' : ''; ?>>1991</option>
<option value="1990"<?php echo $birthdayYear == '1990' ? 'selected="selected"' : ''; ?>>1990</option>
<option value="1989"<?php echo $birthdayYear == '1989' ? 'selected="selected"' : ''; ?>>1989</option>
<option value="1988"<?php echo $birthdayYear == '1988' ? 'selected="selected"' : ''; ?>>1988</option>
<option value="1987"<?php echo $birthdayYear == '1987' ? 'selected="selected"' : ''; ?>>1987</option>
<option value="1986"<?php echo $birthdayYear == '1986' ? 'selected="selected"' : ''; ?>>1986</option>
<option value="1985"<?php echo $birthdayYear == '1985' ? 'selected="selected"' : ''; ?>>1985</option>
<option value="1984"<?php echo $birthdayYear == '1984' ? 'selected="selected"' : ''; ?>>1984</option>
<option value="1983"<?php echo $birthdayYear == '1983' ? 'selected="selected"' : ''; ?>>1983</option>
<option value="1982"<?php echo $birthdayYear == '1982' ? 'selected="selected"' : ''; ?>>1982</option>
<option value="1981"<?php echo $birthdayYear == '1981' ? 'selected="selected"' : ''; ?>>1981</option>
<option value="1980"<?php echo $birthdayYear == '1980' ? 'selected="selected"' : ''; ?>>1980</option>
<option value="1979"<?php echo $birthdayYear == '1979' ? 'selected="selected"' : ''; ?>>1979</option>
<option value="1978"<?php echo $birthdayYear == '1978' ? 'selected="selected"' : ''; ?>>1978</option>
<option value="1977"<?php echo $birthdayYear == '1977' ? 'selected="selected"' : ''; ?>>1977</option>
<option value="1976"<?php echo $birthdayYear == '1976' ? 'selected="selected"' : ''; ?>>1976</option>
<option value="1975"<?php echo $birthdayYear == '1975' ? 'selected="selected"' : ''; ?>>1975</option>
<option value="1974"<?php echo $birthdayYear == '1974' ? 'selected="selected"' : ''; ?>>1974</option>
<option value="1973"<?php echo $birthdayYear == '1973' ? 'selected="selected"' : ''; ?>>1973</option>
<option value="1972"<?php echo $birthdayYear == '1972' ? 'selected="selected"' : ''; ?>>1972</option>
<option value="1971"<?php echo $birthdayYear == '1971' ? 'selected="selected"' : ''; ?>>1971</option>
<option value="1970"<?php echo $birthdayYear == '1970' ? 'selected="selected"' : ''; ?>>1970</option>
<option value="1969"<?php echo $birthdayYear == '1969' ? 'selected="selected"' : ''; ?>>1969</option>
<option value="1968"<?php echo $birthdayYear == '1968' ? 'selected="selected"' : ''; ?>>1968</option>
<option value="1967"<?php echo $birthdayYear == '1967' ? 'selected="selected"' : ''; ?>>1967</option>
<option value="1966"<?php echo $birthdayYear == '1966' ? 'selected="selected"' : ''; ?>>1966</option>
<option value="1965"<?php echo $birthdayYear == '1965' ? 'selected="selected"' : ''; ?>>1965</option>
<option value="1964"<?php echo $birthdayYear == '1964' ? 'selected="selected"' : ''; ?>>1964</option>
<option value="1963"<?php echo $birthdayYear == '1963' ? 'selected="selected"' : ''; ?>>1963</option>
<option value="1962"<?php echo $birthdayYear == '1962' ? 'selected="selected"' : ''; ?>>1962</option>
<option value="1961"<?php echo $birthdayYear == '1961' ? 'selected="selected"' : ''; ?>>1961</option>
<option value="1960"<?php echo $birthdayYear == '1960' ? 'selected="selected"' : ''; ?>>1960</option>
<option value="1959"<?php echo $birthdayYear == '1959' ? 'selected="selected"' : ''; ?>>1959</option>
<option value="1958"<?php echo $birthdayYear == '1958' ? 'selected="selected"' : ''; ?>>1958</option>
<option value="1957"<?php echo $birthdayYear == '1957' ? 'selected="selected"' : ''; ?>>1957</option>
<option value="1956"<?php echo $birthdayYear == '1956' ? 'selected="selected"' : ''; ?>>1956</option>
<option value="1955"<?php echo $birthdayYear == '1955' ? 'selected="selected"' : ''; ?>>1955</option>
<option value="1954"<?php echo $birthdayYear == '1954' ? 'selected="selected"' : ''; ?>>1954</option>
<option value="1953"<?php echo $birthdayYear == '1953' ? 'selected="selected"' : ''; ?>>1953</option>
<option value="1952"<?php echo $birthdayYear == '1952' ? 'selected="selected"' : ''; ?>>1952</option>
<option value="1951"<?php echo $birthdayYear == '1951' ? 'selected="selected"' : ''; ?>>1951</option>
<option value="1950"<?php echo $birthdayYear == '1950' ? 'selected="selected"' : ''; ?>>1950</option>
<option value="1949"<?php echo $birthdayYear == '1949' ? 'selected="selected"' : ''; ?>>1949</option>
<option value="1948"<?php echo $birthdayYear == '1948' ? 'selected="selected"' : ''; ?>>1948</option>
<option value="1947"<?php echo $birthdayYear == '1947' ? 'selected="selected"' : ''; ?>>1947</option>
<option value="1946"<?php echo $birthdayYear == '1946' ? 'selected="selected"' : ''; ?>>1946</option>
<option value="1945"<?php echo $birthdayYear == '1945' ? 'selected="selected"' : ''; ?>>1945</option>
<option value="1944"<?php echo $birthdayYear == '1944' ? 'selected="selected"' : ''; ?>>1944</option>
<option value="1943"<?php echo $birthdayYear == '1943' ? 'selected="selected"' : ''; ?>>1943</option>
<option value="1942"<?php echo $birthdayYear == '1942' ? 'selected="selected"' : ''; ?>>1942</option>
<option value="1941"<?php echo $birthdayYear == '1941' ? 'selected="selected"' : ''; ?>>1941</option>
<option value="1940"<?php echo $birthdayYear == '1940' ? 'selected="selected"' : ''; ?>>1940</option>
<option value="1939"<?php echo $birthdayYear == '1939' ? 'selected="selected"' : ''; ?>>1939</option>
<option value="1938"<?php echo $birthdayYear == '1938' ? 'selected="selected"' : ''; ?>>1938</option>
<option value="1937"<?php echo $birthdayYear == '1937' ? 'selected="selected"' : ''; ?>>1937</option>
<option value="1936"<?php echo $birthdayYear == '1936' ? 'selected="selected"' : ''; ?>>1936</option>
<option value="1935"<?php echo $birthdayYear == '1935' ? 'selected="selected"' : ''; ?>>1935</option>
<option value="1934"<?php echo $birthdayYear == '1934' ? 'selected="selected"' : ''; ?>>1934</option>
<option value="1933"<?php echo $birthdayYear == '1933' ? 'selected="selected"' : ''; ?>>1933</option>
<option value="1932"<?php echo $birthdayYear == '1932' ? 'selected="selected"' : ''; ?>>1932</option>
<option value="1931"<?php echo $birthdayYear == '1931' ? 'selected="selected"' : ''; ?>>1931</option>
<option value="1930"<?php echo $birthdayYear == '1930' ? 'selected="selected"' : ''; ?>>1930</option>
<option value="1929"<?php echo $birthdayYear == '1929' ? 'selected="selected"' : ''; ?>>1929</option>
<option value="1928"<?php echo $birthdayYear == '1928' ? 'selected="selected"' : ''; ?>>1928</option>
<option value="1927"<?php echo $birthdayYear == '1927' ? 'selected="selected"' : ''; ?>>1927</option>
<option value="1926"<?php echo $birthdayYear == '1926' ? 'selected="selected"' : ''; ?>>1926</option>
<option value="1925"<?php echo $birthdayYear == '1925' ? 'selected="selected"' : ''; ?>>1925</option>
<option value="1924"<?php echo $birthdayYear == '1924' ? 'selected="selected"' : ''; ?>>1924</option>
<option value="1923"<?php echo $birthdayYear == '1923' ? 'selected="selected"' : ''; ?>>1923</option>
<option value="1922"<?php echo $birthdayYear == '1922' ? 'selected="selected"' : ''; ?>>1922</option>
<option value="1921"<?php echo $birthdayYear == '1921' ? 'selected="selected"' : ''; ?>>1921</option>
<option value="1920"<?php echo $birthdayYear == '1920' ? 'selected="selected"' : ''; ?>>1920</option>
<option value="1919"<?php echo $birthdayYear == '1919' ? 'selected="selected"' : ''; ?>>1919</option>
<option value="1918"<?php echo $birthdayYear == '1918' ? 'selected="selected"' : ''; ?>>1918</option>
<option value="1917"<?php echo $birthdayYear == '1917' ? 'selected="selected"' : ''; ?>>1917</option>
<option value="1916"<?php echo $birthdayYear == '1916' ? 'selected="selected"' : ''; ?>>1916</option>
<option value="1915"<?php echo $birthdayYear == '1915' ? 'selected="selected"' : ''; ?>>1915</option>
<option value="1914"<?php echo $birthdayYear == '1914' ? 'selected="selected"' : ''; ?>>1914</option>
<option value="1913"<?php echo $birthdayYear == '1913' ? 'selected="selected"' : ''; ?>>1913</option>
<option value="1912"<?php echo $birthdayYear == '1912' ? 'selected="selected"' : ''; ?>>1912</option>
<option value="1911"<?php echo $birthdayYear == '1911' ? 'selected="selected"' : ''; ?>>1911</option>
<option value="1910"<?php echo $birthdayYear == '1910' ? 'selected="selected"' : ''; ?>>1910</option>
<option value="1909"<?php echo $birthdayYear == '1909' ? 'selected="selected"' : ''; ?>>1909</option>
<option value="1908"<?php echo $birthdayYear == '1908' ? 'selected="selected"' : ''; ?>>1908</option>
<option value="1907"<?php echo $birthdayYear == '1907' ? 'selected="selected"' : ''; ?>>1907</option>
<option value="1906"<?php echo $birthdayYear == '1906' ? 'selected="selected"' : ''; ?>>1906</option>
<option value="1905"<?php echo $birthdayYear == '1905' ? 'selected="selected"' : ''; ?>>1905</option>
<option value="1904"<?php echo $birthdayYear == '1904' ? 'selected="selected"' : ''; ?>>1904</option>
<option value="1903"<?php echo $birthdayYear == '1903' ? 'selected="selected"' : ''; ?>>1903</option>
<option value="1902"<?php echo $birthdayYear == '1902' ? 'selected="selected"' : ''; ?>>1902</option>
<option value="1901"<?php echo $birthdayYear == '1901' ? 'selected="selected"' : ''; ?>>1901</option>
<option value="1900"<?php echo $birthdayYear == '1900' ? 'selected="selected"' : ''; ?>>1900</option>
</select>
If it's not more efficient, it's certainly more readable and reusable, which is far more important.
<select name="birthdayYear" >
<option value="0000">Year:</option>
<?php
for(var $x = year(); $x >= 1900; $x--) {
var $selected = "";
if($x == $birthdayYear) {
$selected = " selected = 'selected'";
}
echo("<option value="$x"$selected>$x</option>");
}
?>
</select>
In short, my hair is a bird.
Basically, you will :
Deal with the 0000 specific line yourself,
and then, have a for() loop from 2011 to 1900
The loop could look a bit like this :
for ($year=2011 ; $year>=1900 ; $year--) {
echo '<option value="' . $year . '"';
if ($year == $birthdayYear) {
echo 'selected="selected"';
}
echo '>' . $year . '</option>' . PHP_EOL;
}
Of course, the 2011 year should probably not be hard-coded -- take a look at date() for a possible way of getting the current year.
<select name="birthdayYear" >
<option value="0000"<?php echo $birthdayYear == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
<?php
for($i=date('Y'); $i>1899; $i--) {
$selected = '';
if ($birthdayYear == $i) $selected = ' selected="selected"';
print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");
}
?>
</select>
And, would a loop be more efficient then listing them out one by one?
I don't think you have to worry about efficiency. Using a loop just looks cleaner it will be easier to maintain I think.
for ($x=(int)date("Y"); $x>=1900; $x=$x--) {
echo "<option value=\"", $x, "\"", ($birthdayYear==$x) ? " selected=\"selected\"" : "", ">", $x, "</option>"
}
This has the added bonus that you don't have to update it every year.
Try this:
<select name="birthdayYear" >
<?php
if($year == '0000') {
echo '<option value="0000" selected="selected">0000</option>';
} else {
echo '<option value="0000">0000</option>';
}
for($year = intval(date('Y')); $year > 1900; $year --) {
if($year == $birthdayYear) {
echo '<option value="'.$year.'" selected="selected">'.$year.'</option>';
} else {
echo '<option value="'.$year.'">'.$year.'</option>';
}
}
?>
</select>
First, output the '0000' option. It does not follow the pattern, so it should not be included in the loop:
<option value="0000"<?php echo $birthdayYear == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
Next, create a loop and output each of the parts of the option. I started with date('Y') (the current year) rather than the hard-coded '2011' so your script will not need to be updated every year.
for ( $i = date('Y'); $i >= 1900; $i-- ) {
echo "<option value=\"$i\"";
echo $birthdayYear == $i ? 'selected="selected"' : '';
echo ">$i</option>";
}
The three echo statements could be combined into one. I placed them on separate lines for clarity.
1900 is at least as arbitrary as 2011. We can make one that only spans 100 years from the current year.
<?php $selectedYear = $_REQUEST['birthdayYear'] ?>
<select id="birthdayYear">
<option value="0">Year:</option>
<?php $currYear = date('Y') ?>
<?php for ($i=0; $i<100; ++$i) : ?>
<?php $displayYear = $currYear-$i ?>
<option value="<?= $displayYear ?>"<?= ($displayYear == $selectedYear ? 'selected="selected"' : '') ?>><?= $displayYear ?></option>
<?php endfor; ?>
</select>
To get back to 1900 exactly, just change the for condition to 112.
Loop, please. It's not so much a matter of code efficiency (which, at this level, hardly matters at all), but very much one of developer efficiency: A loop is much easier on the eye, it doesn't force the reader to scroll or fold, and it reduces the potential for a typo to introduce subtle bugs (with a loop, they're either all wrong or all correct).
Honestly, any such dropdown with more than two entries in such a regular pattern makes me twitch and reach for the for keyword. A hundred of them is bordering on embarrassment.
<select name="birthday">
<option value="0">-</option>
<?php
for($i=1990; $i<=2013; $i++) { ?>
<option value="<?php echo $i; ?>">
<?php echo $i; ?>

Categories