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?

 

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.