HTML dropdownmenu adding own value - php

Is there any possibility to have a drop-down-menu where you can add an own value by typing it directly into the drop-down-field?
The Idea is to have all rows in a database to be listet in this drop-down-menu and to have the posibility to add a new row.
The php-File for saving all the Data is allready working.
Rightnow my code for the drop-down-menu looks like this:
<select id="name" name="name" onChange="enableOther();" onFocus="enableOther()">
<option> </option>
<option>Neu</option>
<?php
while($row = mysql_fetch_object($ergebnis))
{
echo('<option>'.$row->Material.'</option>');
}
?>
</select>
i hope there is any solution for my problem...

This isn't supported natively in HTML, but there are many JavaScript UI toolkits that provide an editable combo box widget, or you could try coding one in JS yourself (essentially, using a text input with a custom-coded drop-down list).
Alternatively, a simple approach would be something like this:
<select name="name" ...>
<option value=''>Custom (use text field)</option>
... etc ...
</select>
<input type='text' name='customName'>
Then at the server side, if the submitted value of name is a blank string, but the submitted value of customName is not, use customName. You could also add JavaScript to enable/disable the customName field when the user changes the select box as appropriate.
Edit: HTML5
Incidentally, this is possible in HTML5, but that, of course, is not supported by all browsers:
<input list="suggestedNames" name="name">
<datalist id="suggestedNames">
<option value="...">
...
</datalist>

Related

How do I Apply a working Dropdown instead of a Text field in php

I am working on customizing a plugin, however my knowledge is currently lacking on the php side.
currently i have an Ad plugin in which you can sell ads per day, however my predicament is that i want to sell the ads only in packs of 30,60 & 90 days.
In this plugin upon typing a number of days this plugin calculates the amount and gives a result right under without anything pressed, so it is actively reading an input; however the only problem with this plugin is that it allows anyone to type a random number in there.
now the only real solution that i could think of was to make a drop-down with pre-existing values 30, 60, 90
My attempts thus far have been futile as i keep applying HTML <Select> (yes i had changed the wording from <input to <select and then added <option value="value">value wording</option>)techniques and it seems that anytime that i attempt to put a number for the value as one of the choices i get nothing but an empty drop-down, this then made sense to me since this is a PHP form and not an HTML form.
I was able to find where to code the changes, as i was able to find where the field is pulling from; here is the code:
(How should i proceed to make the changes that i want happen?)
<div class="control-group" id="total_days" style="<?php echo $date_dis;?>">
<label class="ad-price-lable control-label" width="40%"><div id="total_days_label"><?php JText::_("TOTAL_DAYS_FOR_RENEWAL");?>:</div> </label>
<div class="controls">
<input type="text" maxlength="5" name="totaldays" class="ad-pricing" id="totaldays" value="<?php echo $ad_totaldays ;?>" onchange="caltotal()" >
<input type="hidden" name="ad_totaldays" id="ad_totaldays" value="<?php echo $ad_totaldays;?>" />
</div>
</div>
now i am also assuming that out of this area, the main thing to be changed would be:
<input type="text" maxlength="5" name="totaldays" class="ad-pricing" id="totaldays" value="<?php echo $ad_totaldays ;?>" onchange="caltotal()" >
of course, i could be wrong, but i really would appreciate help with this!
thank you very much!
Here is how I've always done dropdowns
<select form="examplef">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<form id="examplef">
<input type="submit">
</form>
The php you're looking at is setting a value field for the inputs. This just means that something will be in the box already when you load the page.
Also keep in mind that just because you have predefined options doesn't mean you wont get unexpected input; Changing the options is as easy as right clicking
PHP form vs HTML form
They are the same. The html <form> tag is a way to communicate with php. This tag will (most likely) have an attribute called "action" and "method". The action attribute specifies what to do with the collected information. You can think of it almost as an <a href> that includes data.
The method attribute is a little more complicated. It tells your browser how to send the data entered into the form. The two options are "GET" and "POST". the GET method is data put in the url. I'm sure you've seen youtube links with the ?id=. This is just a GET variable included in the URL. The POST method is for secure data, or data that shouldnt be in the URL. If you would like to read more, W3Schools has a great document here or from Php here and here

How to run php when an option is selected from a select list?

I would like to have a select list that runs a php database query when an option is selected. I have the code:
<select>
<option value="available">Available</option>
<option value="sold">Sold</option>
</select>
<input type="submit" name="change status">
Say, when a user selects 'sold' I would like to run:
<?php
db_query("UPDATE {product_stock} SET stock='0' WHERE nid='$value'");
?>
I've tried
<option value="sold" <?php db_query("UPDATE {product_stock} SET stock='0' WHERE nid='$value'"); ?> >Sold</option>
but doesn't seem to work.
I don't know if I should be using
<form method="GET">...</form>
I know I can use ajax somehow but I'm really not familiar with it.
Any ideas how to do this?
Thanks
You have to use javascript! Or else you must update your database after form submit.
A possible way with the javascript library jquery would be:
$("select").change(function() {
$.post("/your/url", {option: $(this).val()}, function(return) {
//echo result if necessary
}
});
On server side you should check for $_POST['option'] and update your database after you
ESCAPED
the contents of the variable "option".
You have to send an AJAX call with jquery or plain JavaScript when the combobox got changed.
Depending on the new value, you can send various parameter and call your PHP scripts.
But the interaction between HTML and PHP like you tried will never work. The time you see the combo box in your browser, your PHP commands were already executed.
My site is build on drupal so I created a custom module and used hook_form_alter so I could just use PHP and didn't need to code in ajax.
Well, you will have to create two files one with the form and a second one with the action itself, the first will have something like this.
<form method="POST" action="ActionFile.php">
<input type="text" name="name" id="name" value="">
<input type="submit" value="find name">
</form>
and create a file called ActionFile.php that will contain the variables and the connection to the DB and the select.

get the text of the selected option using php [duplicate]

This question already has answers here:
Get Text From <option> Tag Using PHP
(6 answers)
Closed 8 years ago.
in the following:
<form action="test.php" method="POST">
<select id="test" name="test">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
</form>
in test.php, I can get 1 or 2 as follow:
$result=$_POST['test'];
How can I get the text of the selected option (i.e. "Test One" or "Test Two") using php
This is not something that can be done through PHP alone. The PHP script can only "see" the information which is posted (the value for the selected option that is posted). You can use javascript to alter a hidden input field with the text contents of a selected option, and this will be included in the $_POST array:
<form action="test.php" method="POST">
<select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
<input type="hidden" name="test_text" id="text_content" value="" />
</form>
This will make the $_POST['test_text'] available with the selected index (but you should also force the onchange() function when the page loads so that it will be populated even if the user leaves the select field at the default value.
Only the value of the form control will be sent to the server. An easy way to send both is to simply include both in the the value:
<select name="test">
<option value="1|Test one">Test one</option>
<option value="2|Test two">Test two</option>
</select>
And then:
$test = explode('|', $_POST['test']);
Then you'll end up with $test[0] being "1" and $test[1] being "Test one".
You can't; that information is not sent back to the server. You will need to look at how you generated the HTML in the first place and get the text from there.
It is not sent so the only way to get it is having an array mapping values to titles in your PHP code.
This is a solution I could use except I already have the onChange calling a function to show a hidden block based on the selected building type in a statement.
I have building types I need the user to select. Several building types have a unique set of questions, e.g. Bank Branch, Data Center, Courtroom, etc. But there are many that have the same set of questions which I call Other Type. Each option has their associated value, e.g. Bank Branch has a value of "Bank_Branch", Data Center has "Data_Center", but Other Type has a value of "Other_Type". The text for "Other_Type" differs based on the building type, such as Convention Center, Museum, Performing Arts, etc. So I need the value "Other_Type" to "show" the questions in the "Other_Type" DIV block while using the text value to send in an email identifying the type of building, e.g. Convention Center, Museum, Performing Arts, etc.
Any way to use PHP to get the text value of the selected item? I'm already using the following inside the HTML
var sele = document.getElementById('building_type');
var seleVal = sele.options[sele.selectedIndex].value;
var seleTxt = sele.options[sele.selectedIndex].text;
document.getElementById("other_text").innerHTML = seleTxt;
I'm not seeing a way to do this.
SOLVED: I can simply create the hidden div and in my check for Other Type set the innerHTML for the hidden div.
The text for the selected option is not passed through the HTML form. If you want the text, you have to store that in a hidden HTML input on your page, or you need to add some business logic in your PHP code to translate the value of ìd into the text (through a switch statement, or by querying a database, etc.)
Unfortunately when you submit a form and a variable it only takes one parameter which is it's value. You would need to make the value. Test One the value in order for it to pass on to the PHP script. What is the purpose of value="1" cause you can probably use it in a different attribute?
Im not a experienced php programer, but u can check the value selected in SELECT tag with php.
i dont know why people say u can not.
if ( $_POST['test'] == 1)
{ do something }
else
{ option value 2 is selected, do something else }
Im sure this will work.
Try it if 1, 2 not needed. you will get required text as value
<form action="test.php" method="POST">
<select id="test" name="test">
<option value="Test One">Test One</option>
<option value="Test Two">Test Two</option>
</select>
</form>

$_POST for disabled select

<select class="txtbx1" name="country" disabled>
<option value='FR' >FRANCE</option><option value='CH' selected>SWITZERLAND</option>
</select>
the above code is inside a form whose method is post
but echo $_POST['country'] is showing nothing.. on the other hand if I remove disabled from select $_POST['country'] is showing the correct result
This is how the disabled attribute works. When a form control is disabled, the value will be ignored when the form is submitted and the key will not be present in $_POST (or $_GET).
If you want the value to be present in the submitted data, but you don't want the user to be able to change the value on the page (which I imagine is what you are trying to acheive) use readonly="readonly" instead of disabled="disabled".
EDIT
The <select> element does not have a readonly attribute. The above information still stands as it will work for <input>s and <textarea>s.
The solution to your problem here would be to disable the select and use a hidden input to send the value back to the server - e.g.
When the select is enabled:
<select class="txtbx1" name="country">
<!-- options here -->
</select>
...and when it is disabled:
<select class="txtbx1" name="country_disabled" disabled="disabled">
<!-- options here, with appropriate value having `selected="selected"` -->
</select>
<input type="hidden" name="country" value="value_of_field" />
This is the correct behavior. disabled disables the element, and does not send it's value when a form is POSTed.
You can use JavaScript to un-disable the form before you submit it. Something like this (untested):
document.getElementById('myForm').addEventListener('submit', function() {
for(var i = 0; i < this.children.length; i++){
var child = this.children[i];
if(child.disabled){
child.disabled = false;
}
}
});
How your form tag looks like? You may have forgotten the method="post" attribute...
If your goal is have a "readonly select" - that is show the user what the choices are without allowing it to change and have it sent with the POST variables, you want to use "selected" on the option with the current value, and "disabled" on all the other options. This will essentially show the select with all its options but only allow the current one to be selected.
I find this more helpful than simply having a disabled select.
As others have noted, you still need to insure that the server side doesn't accept changes to the field (as with any readonly field).

PHP: Need a double check on an error in this small code

I have this simple Select box that is suppose to store the selected value in a hidden input, which can then be used for POST (I am doing it this way to use data from disabled drop down menus)
<body>
<?php
$Z = $_POST[hdn];
?>
<form id="form1" name="form1" method="post" action="test.php">
<select name="whatever" id="whatever" onchange="document.getElementById('hdn').value = this.value">
<option value="1">1Value</option>
<option value="2">2Value</option>
<option value="3">3Value</option>
<option value="4">4Value</option>
</select>
<input type="hidden" name ='hdn' id="hdn" />
<input type="submit" id='submit' />
<?php
echo "<p>".$Z."</p>";
?>
</form>
</body>
The echo call works for the last 3 options (2,3,4) but if I select the first one it doesnt output anything, and even if i change first one it still doesnt output anything.
Can someone explain to me whats going on, I think it might be a syntax issue.
EDIT: As mentioned in first paragraph: I am using the hidden field instead of just using the value of selected because i plan on disabling the select drop down in my actual website, and when you disable the menu the POST data is no longer accessible. By putting it into a hidden field you can still access the information
The first one is the default, so when you "change" to the first one, it hasn't actually changed and the event does not fire.
You should be reading the value directly from the select and not depending on JS to set a hidden field though. The JS is just pointless, unreliable complexity.
Does anything else in your client-side application depend on that hidden field? If the ONLY purpose of the hidden field is to duplicate the selected value of the dropdown, then there's absolutely no reason to use an onchange handler.
Best solution would be to set an onsubmit handler on the form itself to copy the dropdown value into the hidden field.
<form ... onsubmit="document.getElementById('hdn').value = document.getElementById('whatever').value">
Try your post access like this:
<?php
if (array_key_exists('hdn', $_POST)) {
$Z = $_POST['hdn'];
}
else {
$Z = 1;
}
?>
change your input:
<input type="hidden" name='hdn' id="hdn" value= <?php echo "$Z"; ?> />
this.value has no value. That is why $_POST['hdn'] doesn't have a value when you initially load the form.
As #David said, if you use Firefox you can see the post data for hdn is empty/null.

Categories