Ell20 Posted October 29, 2007 Share Posted October 29, 2007 Hey, Im trying to create a drop down box which at the end has a Other option. If Other is selected I want the variable which is set from the drop down box to be set to whatever is typed in, in the input box below by the user e.g: <select name="team_name"><option value='' selected disabled>Select Team Name</option> <option value="1st XI">1st XI</option> <option value="2nd XI">2nd XI</option> <option value="3rd XI">3rd XI</option> <option value="Other>Other</option> </select> I dont really want to use Java or AJAX, is there anyway in which this can still be done? Cheers Quote Link to comment Share on other sites More sharing options...
trq Posted October 29, 2007 Share Posted October 29, 2007 <?php if (isset($_POST['submit'])) { if ($_POST['team_name'] == 'Other') { $val = $_POST['textbox']; } } ?> Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 29, 2007 Author Share Posted October 29, 2007 Having a bit of trouble, I can only get either Other to work or a selected value from the drop down box to work, here is my code: if (isset($_POST['submit2'])) { if($_POST['team_name'] == 'Other') { $team_name = $_POST['textboxother']; } else { $team_name = escape_data($_POST['team_name']); $captain = escape_data($_POST['captain']); $vice_captain = escape_data($_POST['vice_captain']); $description = escape_data($_POST['description']); $query = "INSERT INTO cricket_teams (club_id, team_name, captain, vice_captain, description) VALUES ('$id', '$team_name', '$captain', '$vice_captain', '$description')"; $result = @mysql_query ($query); if ($result) { echo '<h3>New Team Created!</h3>'; } } } Cheers Quote Link to comment Share on other sites More sharing options...
otuatail Posted October 29, 2007 Share Posted October 29, 2007 I think what you need is a text box underor above the drop down. If you enter info into the textbox. The next page will check to see if you have entered anything. If you have use that, otherwise use what has been selected. Desmond. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 29, 2007 Author Share Posted October 29, 2007 I have a text box under the drop down box. The code is meant to set the variable to the text put into the textbox if the drop down box selection is Other? Quote Link to comment Share on other sites More sharing options...
otuatail Posted October 29, 2007 Share Posted October 29, 2007 I think what you are trying to do is add the value of the text box to the drop down box. I.E populating a drop down list more than once. If this is correct. Where is the drop down getting ints info freom. Is it a database field? If so you will have to create a SQL script to add this to the databes. Desmond. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 29, 2007 Author Share Posted October 29, 2007 No i dont think your quiet along the right lines. All I want is a drop down box with the option "Other" at the bottom. If the option "Other" is selected from the drop down box then whatever is written in the text box below becomes the value of the drop down box. e.g Drop down box: Egg Milk Other (selected) Text box: Sasuage I now want sausage to be put into the database instead of Other etc. Quote Link to comment Share on other sites More sharing options...
otuatail Posted October 29, 2007 Share Posted October 29, 2007 Yes I see but Why select Other from the drop down box and then type Sasuage in the text box as well. You should be able to detect if the text box is empty. I.E. leave the drop down box alone with it's default. Lets say it defaults to the first Item Egg. Because you typed in Sasuage into the text box. When you hit submit. The next form will add Sasuage to the database regardless of what was selected in the drop down box. I am asuming that the drop down box has 2 items in a table Egg and Milk. And these values are used in the drop down box, AND that you do not want Sasuage to be added to the database for future use in the drop down box. Is this correct Demond. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 29, 2007 Author Share Posted October 29, 2007 I want Other to be selected in the drop down box to make things easier for the user. If Egg is selected then sausage is typed in then it makes it confusing. I do not want sausage to be added to the drop down box for future use. I can get either the Other then text box option to work OR the normal selection from the drop down box however I cant get them to work together. <?php if (isset($_POST['submit2'])) { $team_name = escape_data($_POST['team_name']); $captain = escape_data($_POST['captain']); $vice_captain = escape_data($_POST['vice_captain']); $description = escape_data($_POST['description']); if($_POST['team_name'] == 'Other') { $team_name = $_POST['textboxother']; $query = "INSERT INTO cricket_teams (club_id, team_name, captain, vice_captain, description) VALUES ('$id', '$team_name', '$captain', '$vice_captain', '$description')"; $result = @mysql_query ($query); if ($result) { echo '<h3>New Team Created!</h3>'; } } } ?> Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 29, 2007 Author Share Posted October 29, 2007 Done it I was just closing the if statement in the wrong place: <?php if (isset($_POST['submit2'])) { $team_name = escape_data($_POST['team_name']); $captain = escape_data($_POST['captain']); $vice_captain = escape_data($_POST['vice_captain']); $description = escape_data($_POST['description']); if($_POST['team_name'] == 'Other') { $team_name = $_POST['textboxother']; } $query = "INSERT INTO cricket_teams (club_id, team_name, captain, vice_captain, description) VALUES ('$id', '$team_name', '$captain', '$vice_captain', '$description')"; $result = @mysql_query ($query); if ($result) { echo '<h3>New Team Created!</h3>'; } } ?> Quote Link to comment Share on other sites More sharing options...
otuatail Posted October 29, 2007 Share Posted October 29, 2007 Ok then. When you hit submit you check for Other selected, BUT you will still have to check if the text box has been used. What would happen if the USER selected Other from the drop down box and forgot to enter a value in the text box. Desmond. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted October 29, 2007 Author Share Posted October 29, 2007 Thats a good point, I hadnt thought of that, is there anyway I could add something like: if($team_name == ""){ You must enter a team name} Cheers Quote Link to comment Share on other sites More sharing options...
otuatail Posted October 29, 2007 Share Posted October 29, 2007 What I do is populate a session variable with "A value must be entered" header('Location: index.php'); The page prints the message to the right of the text box. Default this to null string after . so the meesage can be canceled. Desmond. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.