Jump to content

Problem submitting value from dynamic option


Orasion

Recommended Posts

So, it's me again.

Hope you dont mind  ;)

 

I try to fetch data from mysql and list it to select option like this

 

<td>Category</td>						
<td>:   <select name="category">
<?php
	$query = "SELECT DISTINCT category FROM dress";
	$sql = mysql_query($query);
		while($result = mysql_fetch_array($sql)){ 									
			echo "<option value='".$result['category']."'>".$result['category']."</option>";
		}
?>										
<option value="other">New Category --></option>										
</select><input type="text" name="categoryOther"></td>						
</tr>

 

...and success. BUT, when I try to echo submitted value it return nothing

this is the code to process above form

if(isset($_POST['categoryOther'])){
	$category = $_POST['categoryOther'];
	} else {
		$category = $_POST['category'];
		}			
if(isset($_POST['subcategoryOther'])){
	$subcategory = $_POST['subcategoryOther'];
	} else {
		$subcategory = $_POST['subcategory'];
		}
echo "category : ".$category."<br>";

 

What did I do wrong here?

 

type="text" form fields are set, even if they are empty. if(isset($_POST['categoryOther'])){ will always be true any time the form has been submitted. Perhaps you want to test if $_POST['categoryOther'] is empty or if it is equal to an empty string?

 

 

I change my code to this one

 

if(strlen($_POST['categoryOther'] = 0)){		
	$category = $_POST['category'];
	} else {			
		$category = $_POST['categoryOther'];
		}

 

..and success to submit my select category option. BUT, cannot submit input from categoryOther.

I'll think another way to submit one of them.

 

thanks for your suggestion, PFMaBiSmAd.

 

EDIT :

silly me, it is as simple as this

 

if($_POST['category'] == "other"){		
	$category = $_POST['categoryOther'];
	} else {						
		$category = $_POST['category'];
		}

 

Now, both works.

:D

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.