Jump to content

Transfering Select Value From Drop Down Box


Recommended Posts

My first attempt to create dropdown box.


I was able to get the information to correct show up but I'm a little confused on how to transfer it.


Here is the old line of code where I hand type in information

  <td width="100" height="27" align="center" valign="top" bgcolor="#FFFFFF" class="style20"><input type="text" name="msdstype" value="<?php echo $msdstype; ?>"/></td>
I have created this dropdown box which does show the values available.
<select name="dropdown">
<?php typequery() ?>
But after the value is selected and I go to enter next field I want that value to go into "msdstype"
Thanks any insight will be helpful.
Link to comment
Share on other sites

So, just so I understand the process...


You have two fields -- one dropdown, and one text field? And when a user selects from the dropdown, you want the value from the dropdown to automatically be added to the text field? I'm assuming you need that text field because the user may be editing the content?


You'd need to do that with Javascript.


-- In pure Javascript, you'd want to use the onChange event (http://www.w3schools.com/jsref/event_onchange.asp), which would get the value from the dropdown (http://www.w3schools.com/jsref/met_doc_getelementbyid.asp) and set the value of the text field (http://stackoverflow.com/questions/4541855/how-do-i-set-value-of-a-textbox-using-javascript)


-- If you're using jQuery, it would be similar, using change() (http://api.jquery.com/change/) to check when the value of the dropdown changes, find the value (http://learn.jquery.com/using-jquery-core/faq/how-do-i-get-the-text-value-of-a-selected-option/) and set the value of the text field (also using val() http://api.jquery.com/val/)

Link to comment
Share on other sites

Sorry Ben that not what I was trying to say but I do understand how you came up with that.


Ok me try this again.


The page I have I'm entering new data, the field "msdstype" I want to limit the the field options so I created a dropdown.  But once that dropped has been select which is coming from a different table called typeinfo and the field from that table is called typename. 


How do i get that value that has been selected to be placed in the database.

Link to comment
Share on other sites

Assuming I'm understanding you correctly...


So you have a single dropdown field that the user can select from. And you want to save their selected value in the database?


In that case, you'd name the dropdown using the name attribute (which in your code above you named "dropdown"), you'd have the user submit the form, you'd retrieve the dropdown value using $_POST['dropdown'], and you'd use that value when creating an SQL query that would save it in the database using an SQL insert statement.


I guess I'm just not sure what part of that process you are confused about? It works exactly the same way as using a regular text input and saving that value to a database. If your existing PHP script which processes the form and saves the values to the database is expecting to get that data from a field named msdstype, you'd just want to name your select "msdstype" rather than "dropdown", and everything else should work fine.

Link to comment
Share on other sites



I based my view edit delete add off of your forum topic.




I was able to incorporate the Dropdowns on the new page how would approach placing the drop down in the edit page.  I want to show what the current selection is but also show the possibilities per field.


I'm converting an old MS Access database that we are using.  This is a screen shot of that.


Thanks any guidance you can give me would be greatly appreciated.




Link to comment
Share on other sites

The easiest solution would be to show the dropdown -- same as you would in the "create new" page -- but have the default selected state be whatever that field is set to. That's pretty straight-forward. You'd want to create the dropdown in the same way that you created the dropdown on the create new page, but for every option you create in your dropdown, you check to see if the value of that option matches the selected option, and if so, give that the "selected" attribute. http://www.w3schools.com/tags/att_option_selected.asp


So, something like this...

<select name="selectname">

    <option value="optionvalue" <?php if($selectedname == 'optionvalue') { echo 'selected'; } ?>>Option Name</option>


So in this case, you'd need to change the "selectname" to whatever you've named the select, and any values/names as appropriate. $selectedname is whatever value the user had selected for that field when they originally created the record.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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