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; ?>
Related
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
This is likely a lot simpler than I'm thinking but I can't seem to figure it out.
I have the following code that's part of a image results list:
<option value="30" <?php echo ($_SESSION['results']== 30) ? 'selected' : ''; ?>>30</option>
<option value="40" <?php echo ($_SESSION['results']== 40) ? 'selected' : ''; ?>>40</option>
<option value="50" <?php echo ($_SESSION['results']== 50) ? 'selected' : ''; ?>>50</option>
<option value="60" <?php echo ($_SESSION['results']== 60) ? 'selected' : ''; ?>>60</option>
<option value="70" <?php echo ($_SESSION['results']== 70) ? 'selected' : ''; ?>>70</option>
<option value="80" <?php echo ($_SESSION['results']== 80) ? 'selected' : ''; ?>>80</option>
<option value="90" <?php echo ($_SESSION['results']== 90) ? 'selected' : ''; ?>>90</option>
The default of course is 40 results but I want the default to be 80. How do I make it so 80 is automatically selected by default
A cleaner way to do this would be:
<?php
session_start(); //Start session
$_SESSION['results'] = $_SESSION['results'] ? $_SESSION['results'] : 80; //Check and set the session variable else default it to 80
$numbers = [30, 40, 50, 60, 70, 80, 90]; //Initialize your array
?>
<select name="test">
<?php
foreach ($numbers as $num) { //Loop through the array and populate your select box.
?>
<option value="<?php echo $num; ?>" <?php echo ($_SESSION['results'] == $num) ? 'selected' : ''; ?>><?php echo $num; ?></option>
<?php
}
?>
</select>
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?
<select name="year" >
<option value="0000"<?php echo $year == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
<?php
for($i=date('Y'); $i>1899; $i--) {
$selected = '';
if ($year == $i) $selected = ' selected="selected"';
print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");
}
?>
</select>
This is my code for the user to choose the year he entered in a combobox, but i wanted to make a 2 year interval like 2000-2002 and etc until the present year. Help me how? Thank you!
You can use a $i=$i-2 instead of $i--.
Something like that:
<select name="year" >
<option value="0000"<?php echo $year == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
<?php
for($i=date('Y'); $i>1899; $i=$i-2) {
$selected = '';
$year2 = $i-2;
if ($year == $i) $selected = ' selected="selected"';
print('<option value="'.$year2. " " . $i .'" '.$selected.'> '.$year2.' '.$i.'</option>'."\n");
}
?>
</select>
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>";