Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am building a website after reading this book, and I am having trouble with the selected="selected" part of a select menu. Nothing is getting selected. When I click on submit form it goes through but the item I selected isn't being selected in my database. 

 

In the following code, the problem lies in the following section: where the select menu is, but I can't get it to work. help would be appreciated. 

 

<?php
$optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education', 
18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping', 
26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
 
echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
 
foreach ($optionarray as $key => $value){
echo '<option value="$key"';
if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '. $key . ')) echo 'selected="selected">'; 
echo $value . '</option>';
}
echo '</select>';
?>

 

 

 

 

 

 

 

<?php
$page_title = 'Edit Your Account';
include ('includes/header.html');
include ('includes/functions.php');
include ('includes/config.inc.php');
if (isset($_GET['id']) && is_numeric($_GET['id'])){
$id = $_GET['id'];
} elseif (isset($_POST['id']) && is_numeric($_POST['id'])) {
$id = $_POST['id'];
} else {
echo '<p class="error">This page has been accessed in error.</p>';
include ('includes/footer.html');
exit();
}
if (isset($_SESSION['UserID'])){
require (MYSQL);
echo '<div class="text">';
$scrubbed = array_map('spam_scrubber', $_POST);
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
if (filter_var($scrubbed['url'], FILTER_VALIDATE_URL)){
$url = mysqli_real_escape_string($dbc, $scrubbed['url']);
} else {
$url = "";
echo '<p class="error">Please enter a valid url</p>';
}
if (!empty($scrubbed['SiteTypeID'])){
$sitetypeid = $scrubbed['SiteTypeID'];
} else {
$sitetypeid ="";
echo '<p class="error">Please select a category</p>';
}
if($url && $sitetypeid){
$q = "SELECT UserID, url, SiteID FROM sites WHERE url='$url' AND SiteTypeID='$sitetypeid' AND SiteTypeID!=$id";
$query = mysqli_query ($dbc, $q);
if (mysqli_num_rows($query) == 0){
$q = "UPDATE sites SET url='$url', SiteTypeID='$sitetypeid' WHERE SiteID=$id LIMIT 1";
$query = mysqli_query ($dbc, $q);
if (mysqli_affected_rows($dbc) == 1){
echo '<p><b>Your url has been successfully edited</b></p>';
} elseif (mysqli_affected_rows($dbc) == 0){
echo '<p>No new details have been inserted</p>';
} else {
echo '<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message.
echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message.
}
} else {
echo '<p><b>That url has already been registered with that category, if you own this site you can change categories if you wish to. Please note that a website cannot be added multiple times in different categories.</b></p>';
}
 
}
}
 
?>
<h1>Edit Your Sites</h1><p><b>Do not add duplicate urls, they will be deleted. We only accept family-safe urls. We play fair and expect our members to do the same.</b> </p>
<?php 
$query2 = "SELECT s.SiteTypeID, u.UserID, s.SiteType FROM sitetypes AS s LEFT JOIN sites AS u USING (SiteTypeID) WHERE SiteID=$id";
$mx = mysqli_query($dbc, $query2);
if (mysqli_num_rows($mx) ==1){
$row2 = mysqli_fetch_array($mx, MYSQLI_ASSOC);
echo '<p>Your site\'s current category is <b>'.$row2['SiteType'] . '</b></p>';
}
?>
<?php
$optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education', 
18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping', 
26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
foreach ($optionarray as $key => $value){
echo '<option value="$key"';
if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '. $key . ')) echo 'selected="selected">'; 
echo $value . '</option>';
}
echo '</select>';
?>
<?php
 
 
$ms = "SELECT UserID, url, SiteID FROM sites WHERE SiteID=$id";
$msp = mysqli_query($dbc, $ms);
if (mysqli_num_rows($msp) > 0){
$row = mysqli_fetch_array($msp, MYSQLI_ASSOC);
echo '<p>Url:<input type="text" name="url" size="60" maxlength="80" value="' . $row['url'] .'" /><small>You can only add each url once into the database, each url must be unique.</small></p><p><input type="submit" name="submit" value="Edit Site Information!" /><input type="reset" name="reset" value="Clear Form" />
<input type="hidden" name="id" value="' . $id . '" />
</form><p><a href="delete_url.php?id=' . $row['SiteID'] . '"><b>Delete Url</b></a>';
} else {
echo '<p class="error">A system error occurred, we apologize for the inconvenience.</p>';
}
?>
<?php
} else {
$url = BASE_URL . 'index.php';
header("Location: $url");
}
echo '</div>';
include ('includes/footer.html');
?>
Link to comment
Share on other sites

I tried that and it shows that the array is empty. So post contains nothing. I am still unclear how to get my code to work though. What I am trying to do is replace a lot of hardcoded data with a foreach loop, however the key in my foreach loop isn't being selected and therefore my code isn't working. The problem might be with the selected="selected" part. Here is the hard-coded code, and below it is my new code with the foreach loop. The hardcoded code works perfectly. 

 

<form action="edit_your_sites.php" method="post">
Please select a category:<select name="SiteTypeID">
<option value="">Select a Category</option>
<option value="5"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '5')) echo ' selected="selected"'; ?>>Advertising</option>
<option value="6"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '6')) echo ' selected="selected"'; ?>>Affiliate Programs</option>
<option value="7"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '7')) echo ' selected="selected"'; ?>>Art & Photography</option>
<option value="9"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '9')) echo ' selected="selected"'; ?>>Beauty & Jewelry</option>
<option value="10"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '10')) echo ' selected="selected"'; ?>>Blogging</option>
<option value="11"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '11')) echo ' selected="selected"'; ?>>Books, Literature</option>
<option value="12"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '12')) echo ' selected="selected"'; ?>>Business & Finance</option>
<option value="3"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '3')) echo ' selected="selected"'; ?>>Children & Family</option>
<option value="13"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '13')) echo ' selected="selected"'; ?>>Computer Games</option>
<option value="14"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '14')) echo ' selected="selected"'; ?>>Computing</option>
<option value="15"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '15')) echo ' selected="selected"'; ?>>Dating & Relationships</option>
<option value="16"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '16')) echo ' selected="selected"'; ?>>Directories</option>
<option value="17"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '17')) echo ' selected="selected"'; ?>>Education</option>
<option value="18"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '18')) echo ' selected="selected"'; ?>>Electronics</option>
<option value="19"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '19')) echo ' selected="selected"'; ?>>Entertainment</option>
<option value="20"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '20')) echo ' selected="selected"'; ?>>Environment</option>
<option value="21"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '21')) echo ' selected="selected"'; ?>>Flowers</option>
<option value="22"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '22')) echo ' selected="selected"'; ?>>Food, Drink</option>
<option value="23"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '23')) echo ' selected="selected"'; ?>>Forums, chat rooms</option>
<option value="24"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '24')) echo ' selected="selected"'; ?>>Free Stuff</option>
<option value="25"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '25')) echo ' selected="selected"'; ?>>Gifts & Shopping</option>
<option value="26"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '26')) echo ' selected="selected"'; ?>>Health</option>
<option value="4"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '4')) echo ' selected="selected"'; ?>>Home Business</option>
<option value="27"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '27')) echo ' selected="selected"'; ?>>Humor</option>
<option value="28"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '28')) echo ' selected="selected"'; ?>>Interior Design</option>
<option value="29"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '29')) echo ' selected="selected"'; ?>>Internet Marketing</option>
<option value="30"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '30')) echo ' selected="selected"'; ?>>Miscellaneous</option>
<option value="31"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '31')) echo ' selected="selected"'; ?>>Music</option>
<option value="32"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '32')) echo ' selected="selected"'; ?>>Pets</option>
<option value="43"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '43')) echo ' selected="selected"'; ?>>Psychology</option>
<option value="33"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '33')) echo ' selected="selected"'; ?>>Real Estate</option>
<option value="34"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '34')) echo ' selected="selected"'; ?>>Religion & Spirituality</option>
<option value="35"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '35')) echo ' selected="selected"'; ?>>Science</option>
<option value="36"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '36')) echo ' selected="selected"'; ?>>Sports</option>
<option value="37"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '37')) echo ' selected="selected"'; ?>>Stocks & Trading</option>
<option value="38"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '38')) echo ' selected="selected"'; ?>>Travel</option>
<option value="39"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '39')) echo ' selected="selected"'; ?>>Vehicles</option>
<option value="40"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '40')) echo ' selected="selected"'; ?>>Web design</option>
<option value="41"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '41')) echo ' selected="selected"'; ?>>Web hosting</option>
<option value="42"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '42')) echo ' selected="selected"'; ?>>Work at Home</option>
</select>
 
Here is the code with the foreach loop:
 
$optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education', 
18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping', 
26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
foreach ($optionarray as $key => $value){
echo '<option value="$key"';
if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == $key)) 
echo 'selected="selected">'; 
echo $value . '</option>';
}
echo '</select>';
Link to comment
Share on other sites


<form action="edit_your_sites.php" method="post">
Please select a category:<select name="SiteTypeID">
<option value="">Select a Category</option>
<option value="5"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '5')) echo ' selected="selected"'; ?>>Advertising</option>
<option value="6"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '6')) echo ' selected="selected"'; ?>>Affiliate Programs</option>
<option value="7"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '7')) echo ' selected="selected"'; ?>>Art & Photography</option>
<option value="9"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '9')) echo ' selected="selected"'; ?>>Beauty & Jewelry</option>
<option value="10"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '10')) echo ' selected="selected"'; ?>>Blogging</option>
<option value="11"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '11')) echo ' selected="selected"'; ?>>Books, Literature</option>
<option value="12"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '12')) echo ' selected="selected"'; ?>>Business & Finance</option>
<option value="3"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '3')) echo ' selected="selected"'; ?>>Children & Family</option>
<option value="13"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '13')) echo ' selected="selected"'; ?>>Computer Games</option>
<option value="14"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '14')) echo ' selected="selected"'; ?>>Computing</option>
<option value="15"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '15')) echo ' selected="selected"'; ?>>Dating & Relationships</option>
<option value="16"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '16')) echo ' selected="selected"'; ?>>Directories</option>
<option value="17"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '17')) echo ' selected="selected"'; ?>>Education</option>
<option value="18"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '18')) echo ' selected="selected"'; ?>>Electronics</option>
<option value="19"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '19')) echo ' selected="selected"'; ?>>Entertainment</option>
<option value="20"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '20')) echo ' selected="selected"'; ?>>Environment</option>
<option value="21"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '21')) echo ' selected="selected"'; ?>>Flowers</option>
<option value="22"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '22')) echo ' selected="selected"'; ?>>Food, Drink</option>
<option value="23"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '23')) echo ' selected="selected"'; ?>>Forums, chat rooms</option>
<option value="24"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '24')) echo ' selected="selected"'; ?>>Free Stuff</option>
<option value="25"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '25')) echo ' selected="selected"'; ?>>Gifts & Shopping</option>
<option value="26"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '26')) echo ' selected="selected"'; ?>>Health</option>
<option value="4"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '4')) echo ' selected="selected"'; ?>>Home Business</option>
<option value="27"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '27')) echo ' selected="selected"'; ?>>Humor</option>
<option value="28"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '28')) echo ' selected="selected"'; ?>>Interior Design</option>
<option value="29"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '29')) echo ' selected="selected"'; ?>>Internet Marketing</option>
<option value="30"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '30')) echo ' selected="selected"'; ?>>Miscellaneous</option>
<option value="31"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '31')) echo ' selected="selected"'; ?>>Music</option>
<option value="32"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '32')) echo ' selected="selected"'; ?>>Pets</option>
<option value="43"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '43')) echo ' selected="selected"'; ?>>Psychology</option>
<option value="33"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '33')) echo ' selected="selected"'; ?>>Real Estate</option>
<option value="34"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '34')) echo ' selected="selected"'; ?>>Religion & Spirituality</option>
<option value="35"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '35')) echo ' selected="selected"'; ?>>Science</option>
<option value="36"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '36')) echo ' selected="selected"'; ?>>Sports</option>
<option value="37"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '37')) echo ' selected="selected"'; ?>>Stocks & Trading</option>
<option value="38"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '38')) echo ' selected="selected"'; ?>>Travel</option>
<option value="39"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '39')) echo ' selected="selected"'; ?>>Vehicles</option>
<option value="40"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '40')) echo ' selected="selected"'; ?>>Web design</option>
<option value="41"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '41')) echo ' selected="selected"'; ?>>Web hosting</option>
<option value="42"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '42')) echo ' selected="selected"'; ?>>Work at Home</option>
</select>

Here is the code with the foreach loop:

$optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education',
18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping',
26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
foreach ($optionarray as $key => $value){
echo '<option value="$key"';
if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == $key))
echo 'selected="selected">';
echo $value . '</option>';
}
echo '</select>';

 

Link to comment
Share on other sites

You said that your POST array was empty when you posted the form, correct?

If that's the case, then that is your problem.

 

You need to figure out how to properly post your form.

Start with a much simpler form, and get it to the point where it's properly posting, and then the problem should become clear.

 

If the problem is related to something other than the POST array not being received, then you need to better describe your problem.

 

...And if you don't even know what your problem is, then you need to first work on better describing the problem for yourself before you try to explain it to us.

 

Thank you.

Link to comment
Share on other sites

I will better explain my question so that it will deserve a proper answer. 

 

First at the top of my script I am validating that the field SiteTypeID is ok. 

if (!empty($scrubbed['SiteTypeID'])){
		$sitetypeid = $scrubbed['SiteTypeID'];
	} else {
		$sitetypeid ="";
		echo '<p class="error">Please select a category</p>';
	}

Then at the bottom of the script I have a select menu that is populated with an associative array and a foreach loop. However the $key variable is not working. It does not have a value between 3 and 43 like it should. Instead it's value is always 0. I want to know why it doesn't have the number value like it the keys in my associative array. 

 

Here is the code that shows the array and the select menu and the foreach loop.

$optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education', 
18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping', 
26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
foreach ($optionarray as $key => $value){
	echo '<option value="$key"';
	if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == $key));
echo 'selected="selected">'; 
	echo $value . '</option>';
}
echo '</select>';
Link to comment
Share on other sites

It's because you're using single quotes instead of double quotes.

Run your PHP and then look at the resulting source code. You'll see that each option element starts with the following:

<option value="$key"selected="selected">

Obviously, you don't want a value of "$key", and you don't want every option to have a value of selected, and you don't want the missing space between value and selected.

Basically, you have a bunch of mistakes in your code.

  1. Change value="$key" to value="' . $key . '".
  2. Get rid of the semicolon at the end of the if statement.
  3. Add a space before selected="selected".
  4. Move the ">" after selected to the beginning of the echo $value statement.

 

That should solve your problem.

In the future, if you're having a problem, look at the source code resulting from the PHP code, and look for problems.

Link to comment
Share on other sites

 Share

×
×
  • Create New...