Option selected variable not carrying over to action page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SoreE yes
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 3:59 am

Option selected variable not carrying over to action page

Post by SoreE yes »

Hi, sometimes the variable $setcategoryID carries over, sometimes it doesn't. If the option value is the same, (this is an update script) it won't carry over, if it's different it will. (It looks like the line echo "<OPTION VALUE selected = '".$record2["CategoriesID"]."'>".$record2["Categories"];) is not working. But an answer eludes me.

This is the update script

Code: Select all

<?php /*?>Select a row from database for one event<?php */?>
<?
$db=mysql_connect ("localhost") or die(mysql_error());
	mysql_select_db("uptodatalinks") or die(mysql_error());
	$query = "SELECT website, title, description, priority, categoryID FROM ArticlesHelpTips  WHERE ArticlesHelpTipsID=$ArticlesHelpTipsID";
	$result=mysql_query($query) or die(mysql_error()); 
while ($record=mysql_fetch_array($result)) { 
	/*echo $record['EventID'].", ".$record['date'].", ".$record['therapy'].", ".$record['location'].", ".$record['course_details'];
    echo"<br><br>"; */
	$setwebsite=trim($record['website']);
	$settitle=$record['title'];
	$setdescription=$record['description'];
	$setpriority=trim($record['priority']);
	$setcategoryID=trim($record['categoryID']);
} 
echo "setcategoryID is ".$setcategoryID;
?>

<!--Create Form-->
<form id="form1" name="form1" method="post" action="ReplaceArticles.php?ArticlesHelpTipsID=<? echo $ArticlesHelpTipsID ?>">
  <br />
  Select Category
  <select name="CategoriesID" >
    <?php require_once('../Connections/uptodatalinks.php'); ?>
    <?php
	$db=mysql_connect ("localhost") or die(mysql_error());
	mysql_select_db("uptodatalinks") or die(mysql_error());
	$query2 = "SELECT * FROM categories";
	$result2=mysql_query($query2);
	while ($record2=mysql_fetch_assoc($result2)) {
	if ($setcategoryID != $record2["CategoriesID"])   {
			echo "<OPTION VALUE = '".$record2["CategoriesID"]."'>".$record2["Categories"];
			}
			else{
			echo "<OPTION VALUE selected = '".$record2["CategoriesID"]."'>".$record2["Categories"];
			}
			
	}
This is the action script

Code: Select all

<?php 
$website = $_POST['Website']; 
$description = addslashes($_POST['Description']);
$title = $_POST['Title'];
$ArticlesHelpTipsID = $_GET['ArticlesHelpTipsID'];
$CategoriesID = $_POST['CategoriesID'];

echo "</br>"."The CategoriesID is ".$CategoriesID;

$link="<br><br><a href='ShowAllArticles.php'>Show All Articles";

/*Validate input*/
$Description=trim($Description);
if (!$Title or !$Description) {echo "You must complete all entries";
echo $link;
exit;
}

$db=mysql_connect("localhost","root","") or die(mysql_error()); 
mysql_select_db("uptodatalinks") or die(mysql_error()); 
$adddata = "UPDATE ArticlesHelpTips SET
	Website='$Website',
	Title='$Title',
	Description='$Description',
	CategoryID='$CategoriesID'
		WHERE ArticlesHelpTipsID=$ArticlesHelpTipsID";
		$result = mysql_query($adddata) or die(mysql_error());

echo "You have successfully replaced the article";
echo $link;
?>
I had a similar problem to this before, and discovered that Firefox was not passing the option selected variable over, but this time it will not work with either Firefox or IE.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

The correct syntax is

Code: Select all

<option value="..." selected>
not

Code: Select all

<option value selected="...">
SoreE yes
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 3:59 am

Thanks, it worked.

Post by SoreE yes »

Many thanks. It worked a treat.
Post Reply