Setting HTML drop down list option - javascript

I am trying to set value from php the option selected in a drop down list in html.
can anyone tell me how to give the "selected" attribute as the answer is generated dynamically
<select form="onsite_assessment" class="form-control" id="onsite_assessment">
<option value="no">NO</option>
<option value="yes">YES</option>
</select>
In the above code if the user has selected No in the previous session. i want to display that option by default.

This is just some general code which can be shaped for your needs. As I can't see all your code I cannot give a FULL answer. Basically you need to store the users' choice and retrieve this in a variable whenever needed, either string or Boolean (true/false). In the example I will show this using a string.
<?php
$user_choice = 'yes';
?>
<select form="onsite_assessment" class="form-control" id="onside_assessment">
<option value="no" <?php if($user_choice == 'no'){echo 'selected'; }>NO</option>
<option value="yes" <?php if($user_choice == 'yes'){echo 'selected'; }>YES</option>
</select>

Related

Make selected option persistent for session

I have a filter that uses a select for the user to pick a value.
Using javascript, is it possible to set the selection as a "session" variable so that when the user reloads/revisits that page their previous filter is already picked?
This should not persist after the user closes their browser, just like a PHP session would.
Sample Select box markup:
<select name="filter-with" class="filter-with">
<option value="">Select . . .</option>
<option value="demo">Session</option>
<option value="ambassador">Ambassador</option>
<option value="customer_coordinator">Customer Coordinator</option>
<option value="account_prime">Account Prime</option>
<option value="marketing_prime">Host</option>
</select>
Thanks!

PHP Simple HTML DOM and Dropdown Element Selected Option

I'm generating dropdown inputs with php simple html dom. I have some values from database and according them i will add 'selected' value into my select->option dom elements.
<select name="myDropDown" id="someID">
<option value="someValue1">Value1Value</option>
<option value="someValue2">Value2Value</option>
<option value="someValue3">Value3Value</option>
</select>
this is the default one. i would like to add value like this, check on option 2 :
<select name="myDropDown" id="someID">
<option value="someValue1">Value1Value</option>
<option value="someValue2" selected>Value2Value</option>
<option value="someValue3">Value3Value</option>
</select>
and while doing that i want to use my plugin.
<?php
$html = new simple_html_dom();
$html->load('
<select name="myDropDown" id="someID">
<option value="someValue1">Value1Value</option>
<option value="someValue2">Value2Value</option>
<option value="someValue3">Value3Value</option>
</select>
');
echo $html;
?>
Everything works nicely until here. Yeah now i need to insert selected into my second option. I don't know how to do this with PHP Simple HTML DOM, or i'm missing something in the documentation : http://simplehtmldom.sourceforge.net/manual.htm#section_access
What i have tried so far and got many errors in my php section :
<?php
$html = new simple_html_dom();
$html->load('
<select name="myDropDown" id="someID">
<option value="someValue1">Value1Value</option>
<option value="someValue2">Value2Value</option>
<option value="someValue3">Value3Value</option>
</select>
');
//here's where i'm trying to reach the child nodes :
$ret = $html->getElementById('someID');
$ret->children(2)->GOTTADOSOMETHINGHEREANDIREALLYDUNNO;
echo $html;
?>
By the way, if you offer another easy way to do this, i'd appreciate.
Thank you in advance !
With minimal research you can figure this out for yourself.
$ret->children(2)->selected = true;
Why you use simple_html_dom and not simply a template file (with PHP)?
<select name="test" <?php if($selected){echo 'selected'}?> />
Or even better Smarty.
<select name="test" {if $selected}selected{/if} />
Simple_html_dom is a class for parsing complex html documents like exchanging image urls or something like that. I cant' see any reason why you need to use this class.
for getting Dropdown Element Selected Option with Simple HTML DOM
just try this simple and easy method
$element = $html->find('#selectIDGoesHere',0)->find('option');
foreach($element as $elemen) {
echo "Display text:".($elemen->plaintext)."<br>";
echo "value:".($elemen->value)."<br>";
}

Dynamically change text in php

hey guys i am building a form in which i want when a user select an option from the dropdown menus a text to be displayed and calculate the price.
The dropdown menu is:
<select id="recipient" name="recipient" tabindex="6" class="selmenu">
<option value="staff">Site Staff</option>
<option value="editor">Editor-in-Chief</option>
<option value="technical">Tech Department</option>
<option value="pr">Public Relations</option>
<option value="support">General Support</option>
</select>
and the other dropdown menu is:
<select id="recipient" name="recipient" tabindex="6" class="selmenu">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
</select>
What i want to do is when the user selects something from the first and the second menu i want a textfield to change dynamically... can anyone point me the way?
only ajax/jquery could do that in combination of php step might be...
onchange of any dropdown make a ajax request to php script.
return the json response from php script
use this response to either manipulate your form or display data on page.
you don't need to use php for this, it can all be done with jquery/javscript
an example using jquery is below (i had to rename your second select as you had re-used an ID):
$('.selmenu').change(function() {
$('#output').text($('#recipient option:selected').text() + ' ' + $('#recipient2 option:selected').text());
})
output is the id of your textbox.
on a select value being changed, output will be filled with the selected value in both select controls

Linking 2 select lists that are on different pages in HTML

I have 2 php html forms, both consisting of 1 select list. In the first form, I have a select list that asks the user to select their State. According to the state selection on the first page, when the user clicks submit, the second selection list should show the postcodes for that state.
I am having difficulty in performing this task as they 2 select lists are on 2 different pages, i know how to use javascript if they are on the same but i am a bit stumped as they are on different pages. Im not sure how to link them together.
State* <select id="state" name="state"">
<option value="">Select</option>
<option value="OKC">OKC</option>
<option value="NYC">NYC</option>
<option value="CAL">CAL</option>
</select><br />
So if the user selects OKC from first list on the first page, the second list on the second page should show only postcodes for OKC
If you absolutely need to have 2 different PHP pages for your select elements, you could use an HTTP parameter. For example you could have the first form have a submit button which takes you to the second PHP page:
firstpage.php:
<form action="secondpage.php" method="get">
<select id="state" name="state">
<option value="">Select</option>
<option value="OKC">OKC</option>
<option value="NYC">NYC</option>
<option value="CAL">CAL</option>
</select>
<input type="submit" value="Next page" />
</form>
The submit button would take your browser to the secondpage.php but according to your state selection, it would attach a parameter, for example secondpage.php?state=OKC.
On secondpage.php you could use the parameter to your advantage:
$state = $_GET['state'];
Use PHP statements to dynamically generate your <select> element:
<?php
$state = $_GET['state'];
$postcodes = null;
if($state == "NYC")
{
$postcodes = array("postcode1", "postcode2");
}
if($state == "OKC")
{
$postcodes = array("postcode3", "postcode4");
}
?>
<form action="anotherpage.php" method="get">
<select id="postcode" name="postcode">
<?php
foreach ($postcodes as $p)
{
echo '<option value="'.$p.'">'.$p.'</option>';
}
?>
</select>
<input type="submit" value="Next page" />
</form>
Of course there are more efficient ways to go about this, but this should give you an idea.

Dropdown auto select for page to page

How can I auto select from drop down.
Like where customer/reserve.php then click the dropdown and goes to customer/reserve_time&date.php
I'm comfortable using PHP if there's any tutorials can you link it to me
I search in Google but not a single one comes out.
I think you wants to redirect to another page whenever user changes dropdown value. So, you can do a javascript function call on dropdown change event.
<select name="dropdown" onchange="redirectPage(this.value);">
<option value="url1">Option A</option>
<option value="url2">Option B</option>
</select>
Your javascript function starts here:
<script type="text/javascript">
function redirectPage(value)
{
window.location.href = value;
}
</script>
Note: url1 and url2 are the link for the pages. Try This...
You need get in the second page the value selected for the user at the first page.
If you are using GET
$value = $_GET['param'];
If you are using POST
$value = $_POST['param'];
-
<select>
<option value="foo" <?php if($value=="foo") echo "selected"; ?>>bar</option>
<option value="foo1" <?php if($value=="foo1") echo "selected"; ?>>bar2</option>
</select>
UPDATE: Change page and pass parameter when select option.
<select onchange="window.location.href='other.php?value=' + this.value">
<option value="">-- Select one --</option>
<option value="men">Men</option>
<option value="woman">Woman</option>
</select>

Categories

Resources