Ok, i have creted my own drop down box. I have a Div, and in the div i have a span. I need to grab that inner span text. How do i do that? The span's text does change. I am 15, so give me a break! I need some help! This is were the values are picket.
<div>
<select id="test_select" style="display:none;">
<option value="1">Graphic Designer</option>
<option value="2">Animator</option>
<option value="7">Announcer</option>
<option value="11">Web Developer</option>
<option value="12">Judge</option>
</select>
</div>
That is what needs to be gotten:
<div class="custom-select">
<span>Select Option</span>
<ul>
</ul>
</div>
Ok, you're going to need more than just PHP to get this done; you'll need JavaScript as well.
Let's start with your HTML. I'm assuming your rendered output looks like the following and I won't question why you're doing it this way.
<div id="dropdown">
<span>Option One</span>
<span>Option Two</span>
<span>Option Three</span>
</div>
So there's my guess at your HTML.
To post a value back to PHP, you're also going to need a way to capture the selected value in an input field that can be posted with a form. A hidden input will probably be the best option for you.
<input type="hidden" name="dropdown-selection" id="dropdown-selection" />
So that's our markup done. Next you'll need to grab the selected option from your div and stick it into the hidden field. I'm assuming you've coded something to render your div to look and behave exactly like a dropdown (ok, I'll bite. Why ARE you doing it this way?).
This is the JavaScript code using jQuery (we only use jQuery on StackOverflow. Just kidding; that's not true. Well, maybe it's a little bit true)
<script type="text/javascript">
$(function () {
$('#dropdown-selection').val($('#dropdown span:visible').text());
});
</script>
Now, as long as you've ensured that the hidden field is contained within a form that posts back to your target PHP page, you'll have the value of the span tag available to you.
I've made quite a few assumptions about how you've gone about setting up your page here. Correct me on any parts that don't tie into reality.
Related
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
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>
So I have written this code for a form (search form) and it looks like this:
<form action="" method="get">
<select >
<option value="name">Client Name:</option>
<option value="email">Client Email:</option>
<option value="tnx_id">Transaction ID:</option>
<option value="amount">Amount:</option>
</select>
<input type="text" name="svalue" placeholder="Search"></input>
<input type="image" name="submit" src="img/search_btn.png"></input>
</form>
When user clicks Search I want the url to be formatted this way:
http://localhost/pp/index.php?search&name=haha
But instead I get:
http://localhost/pp/index.php?search=name&svalue=haha&submit.x=0&submit.y=0
I'm only having difficulties formating the url correctly because the rest of the
code is good, I cannot get rid of that &submit.x=0&submit.y=0 at the end of the
url too. Do you have any ideas guys?
Basically my desired url format gets the selected option (name, email, tnx_id, amount)
and assigns svalue's value to it.
I'd appreciated your help,
Thank you in advance,
Thanks.
That is how forms work. Each control gives a name=value pair in the submitted data.
You could hack it with JavaScript, but that becomes fragile. Give the <select> element a name and change the server side script so that it takes the two pieces of data as separate pieces of data.
I cannot get rid of that &submit.x=0&submit.y=0 at the end of the url too
Remove the name attribute from the image map so it won't be a successful control.
<input type="image" src="img/search_btn.png">
Other improvements you should make:
<input> is an empty element. It should not have an end tag. If you are writing XHTML then use the empty element syntax.
placeholder should not be used as a replacement for <label>
Add an alt attribute to the image map
Use JavaScript to create the URL. If you're using jQuery:
var url = 'http://localhost/pp/index.php?search&'
+ encodeURIComponent($('#id-of-your-select-element').val())
+ '='
+ encodeURIComponent($('#id-of-your-svalue-element').val());
Edit: As for getting rid of submit.x and submit.y, see #Quentin's answer.
in your php code you can add this:-
if(isset($_GET['name']))
{
$searchString="&cs=".$_GET['name'];
}
then use this:
http://localhost/pp/index.php?".$searchString."/
i think this will help you to get the right thing. if you want to use pagination with that,i have a code for you which i used before and it works great. just try it once!
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>
I have a modification of the dynamic code here embedded within a PHP form, with span elements such as
<span level='0'></span>
<span level='1'></span>
<span level='2'></span>
in the form.
Each span will contain a <select> dropdown that needs to be POSTed
Each span is filled using a JS function exactly like the one in the link, which calls the shown ajax.php file to fill the dropdowns.
These values do not POST with my form, presumably because SPANs may not be processed as part of the POST array. Is there a way I can modify this to work?
I haven't checked the details of the link, but it seems you could use a form element such as instead of <span> to post the data that you need, and just use JS to set the value of this element accordingly.
Edit: An example. I may be misinterpreting your question. What is the data that is important to you - The level at which the user selected? So, if I make a selection from the <span level='1'></span> <select>, you need to know that I chose level 1? In this case, since you're pulling that from the DB through AJAX anyway, I would check it on the server side using PHP to determine the chosen level. Otherwise, you can do something similar to the following.
Note: This will only set the hidden element to the level of the last changed <select>.
<form>
<span level="0">
<select onchange="javascript: $('#input_span_level').val('0');"></select>
</span>
<span level="1">
<select onchange="javascript: $('#input_span_level').val('1');"></select>
</span>
<input id="input_span_level" type="hidden" name="span_level" value="-1" />
</form>