Jump to content

[SOLVED] PHP Dynamic Drop Down


godster

Recommended Posts

Hi guys.

 

New to the forums so please go gentle....

 

Esentially i'm trying to create a PHP based dynamic drop down that would page the selected items ID from the mySQL database to a subsequent details page via the hyperlink.

 

i.e.

 

User selects an item from the drop down, which populates with my categories.

 

When selecting 'Submit' the corresponding ID for that category is passed in the URL to the details page.

 

I have got the catagories to load and appear in the drop down, yet on selecting a category and hitting the button I get a blank template for the corresponding page as no value was passed for the page ID.

 

Please help as i'm sure its something quite trivial i'm missing.

 

Code as below:

 

<?php include 'dbconnect.php' ?> 

<?php 
// If submitted, check the value of "select". If its not blank value, get the value and put it into $select. 
if(isset($select)&&$select!=""){ 
$select=$_POST['select']; 
} 
?> 
<form id="form1" name="form1" method="post" action="cat_conventional_sheet.php?intCatID=<?php echo $select; ?>"> 
Please select a category: 
<select name="select"> 
<option value="">--- Category ---</option> 
<? 
// Get records from database 
$sqlCats = "SELECT c.* 
FROM pv_topic t 
JOIN pv_topicxcat txc ON t.intTopicID = txc.intTopicID 
JOIN pv_cat c ON c.intCatID = txc.intCatID 
WHERE t.intTopicID = 2"; 

$qryCats = mysql_query($sqlCats); 

//Check to see if query came back empty 
$rows = @mysql_num_rows($qryCats); 

if(!$rows) 
{ 
do_error("No record available!"); 
} 
else { 
// Show records by while loop. 
while ($myrow = mysql_fetch_array($qryCats)) { 

$intCatID = $myrow["intCatID"]; 
$strCatName = $myrow["strName"]; 
$strCatLatinName = $myrow["strLatinName"]; 

?> 
<option value="<? echo $myrow['intCatID']; ?>" <? if($myrow['intCatID']==$select){ echo "selected"; } ?>><? echo $strCatName; ?></option> 
<? 
// End while loop 
} 
//End else 
} 
?> 
</select> 
<input type="submit" name="Submit" value="Select" /> 
</form> 
<?php 
// Close database connection. 
mysql_close(); 
?> 

Link to comment
https://forums.phpfreaks.com/topic/148873-solved-php-dynamic-drop-down/
Share on other sites

thats because there is no value of $select on the current page until you submit the form, when you submit the form it does not reload the page giving it the select value it goes straight to the page you designated in your action.

Therefore remove this from your form action  ?intCatID=<?php echo $select; ?>

 

and on your cat_conventional_sheet.php page

 

call this  $select=$_POST['select'];

 

this is the variable you are passing to the page

 

Excuse me if I'm wrong, but shouldnt...

if(isset($select)&&$select!=""){ 
$select=$_POST['select']; 
} 

be...

if(isset($_POST['select'])&&$_POST['select']!=""){ 
$select=$_POST['select']; 
} 

I dont see how isset would check if $select is set if its not yet defined on the post.

This is not even needed on the first page as this page does not process the form, he is sending the data to cat_conventional_sheet.php, hence meaning $select would never get set on the first page anyway.

 

However he was attempting to pass $select via the url which means, what he had would work unless global variables was turned off in which case he would need to use $_GET to access it, either way he is sending what he wants via a form input to another page rendering the statement redundant.

Hi guys, this is really helpful so firstly thank you.

 

Is the POST method then, rather than GET, the right approach for a form,where the value of 'submit' is not know until the the form is actioned?

 

Essentially I want to pass the category ID, via whatever means is preferable. On the cat_conventional_sheet.php (subsequent page) it currently has a GET for the intCatID, which I was inteding to pass via the URL.

 

If therefore the POST method would be more suitable, how do I ensure that it knows the variable inCatID in that subsequent page will be that of $_POST['select'] from the previous page?

 

Thanks again...hoping to crack this tonight.

 

In your first page with the submit form change this

<form id="form1" name="form1" method="post" action="cat_conventional_sheet.php?intCatID=<?php echo $select; ?>">

 

to this

<form id="form1" name="form1" method="post" action="cat_conventional_sheet.php">

 

On the cat_conventional_sheet.php do the check like this

 

<?php $select=$_POST['select']; //get the select id variable
echo $select; //visual reference for correct id remove when satisfied
if (isset($select) && is_numeric ('$select')) { } //input code between these braces if select not empty and is a number
else { } //perform this code if not present or not a number
?>

 

that should do it

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.