Is there a better way to structure this, using html and php?
If I had a billion values, this would be a bad way to set them up, not to mention time consuming as well?
I am a newb, and I am pretty sure there is a better way, however, my way of doing it seems to be the long way, as in long division, I am pretty sure there are easier methods of setting this up, and that's what I am looking for, or asking?
<select name="dimes" >
<option value=" ">--Select Dimes---</option>
<option value=".10">10c</option>
<option value=".20">20c</option>
<option value=".30">30c</option>
<option value=".40">40c</option>
<option value=".50">50c</option>
<option value=".60">60c</option>
<option value=".70">70c</option>
<option value=".80">80c</option>
<option value=".90">90c</option>
<option value="1.00">$1.00</option>
</select>
Thank you for not flaming the Newb, I am still learning.
Do you mean for generating the list?
Something similar to this should suffice:
<?php for($i=0.1;$i<=1;$i+=0.1): ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
If you prefer to use mixed PHP & HTML, you can use such construction:
<select name="dimes" >
<option value=" ">--Select Dimes---</option>
<?php foreach($dates as $key=>$value): ?>
<option value="<?php echo $key; ?>">
<?php echo $value; ?>
</option>
<?php endforeach; ?>
</select>
Note, that you have to define and fill $dates array before using it in foreach statement. You can fill $dates with any data your want. In this example array has to be something like: array('0.1'=>'0.1', '0.2'=>'0.2');
But also your can go futher and use result of MySQL queries to fill array with keys and values. See foreach for more details.
Related
I don't know if it is possible ...
But basically in my drop down list i want to have something of the sort:
[$value_________________$value] _ = space
Hope that is clear enough ... i know how to make the whole text go to the right but just wondering if there is a trick or something idk.
Currently my code is below:
<select name="lesson_type" id="lesson_type">
<option value="" disabled="disabled" selected="selected">Please select a module type.</option>
<?php
while($row=mysqli_fetch_array($result)){
?>
<option value="<?php echo $row['module_code'];?>">
<?php echo $row['module_name'];
echo $row['module_code'];?>
<option>
<?php
}
?>
</select><br />
Im trying to figure out how I can use both standard select option value together with a while loop select option value from mysql database. Right now I have this but won´t work any suggestions?
<option value="All">All</option>
<option value="<?php echo $row_list['name'] ;?>"><?php echo $row_list['name'];?></option>
<select ...>
<option value="standard">...</option>
<?php while(...) { output more options here } ?>
</select>
You can try this:
<?php
$cars=array("Mercedes", "Audi");//Let's have simple arrray with two objects
?>
<select>
<option value="All"> All</option>
foreach($cars as $car){//Let's iterate through array elements
?>
<option value="<?php echo $car;?>"> <?php echo $car;?> </option>
<?php
}
?>
</select>
There are 29+ days in a month, I am creating a form using select and option. But this means I will need 31 options for day, 12 for month and a bunch more for the year.
Is there a better way to do it? I'm not looking for plug-ins, I am trying to make some tidy code to replace some old and messy code. Can I use a PHP for loop? And how would I go about doing this? What would it look like (in terms of mixing up the PHP and HTML variables and functions)?
This is not the best way to do this, but is what your asking.
Look:
<select name="d">
<?php for ($i = 1; $i <= 31; $i++): ?>
<option value="<?php echo str_pad($i, 2, '0', STR_PAD_LEFT) ?>"><?php echo $i ?></option>
<?php endfor ?>
</select>
<select name="m">
<?php for ($i = 1; $i <= 12; $i++): ?>
<option value="<?php echo str_pad($i, 2, '0', STR_PAD_LEFT) ?>"><?php echo $i ?></option>
<?php endfor ?>
</select>
and if you need to take the number of the days in the current/specific month use
$current_month = date('m');
$number_of_day_in_month = cal_days_in_month(CAL_GREGORIAN, $current_month , date('Y'));
If you are using HTML5, then create an <input type="date">.
Safari, Opera and Chrome already do support this input type. In browsers with no date-input-support, such an input degrades to a simple text field.
The specs: http://www.w3.org/TR/html-markup/input.date.html
Browser support: http://caniuse.com/#feat=input-datetime
You MUST implement validation on server-side (in PHP in your case) anyway - so text field is not a tragedy. You may also use simple JavaScript to validate user input before submitting the form data as well.
If you want nice calendar widget in all (A-grade-)browsers, you'll need to use a plugin or implement this yourself in JavaScript.
If your trying to simply use HTML (no plugin), this should get you started:
<select name="Month">
<option value="January">January</option>
<option value="February">February</option>
<option selected value="March">March</option>
</select>
<select name="Day">
<option value="1">1</option>
<option value="2">2</option>
<option selected value="3">3</option>
</select>
<select name="Year">
<option value="2013">2013</option>
<option value="2012">2012</option>
<option selected value="2011">2011</option>
</select>
Here's the jsfiddle
But I would recommend a JavaScript solution, like jQuery's datepicker (http://jqueryui.com/datepicker/).
I'm using search with combo box
Here is my combo box source code
<select name="salary" class="styled">
<option selected="selected" value=''>Any</option>
<option value="10000">10,000</option>
<option value="20000">20,000</option>
<option value="30000">30,000</option>
<option value="40000">40,000</option>
<option value="50000">50,000</option>
<option value="60000">60,000</option>
<option value="70000">70,000</option>
<option value="80000">80,000</option>
<option value="90000">90,000</option>
<option value="100000">100,000</option>
<option value="110000">110,000</option>
<option value="120000">120,000</option>
<option value="130000">130,000</option>
<option value="140000">140,000</option>
<option value="150000">150,000</option>
</select>
I would like to select value based on $salary=$_GET['salary']; if $salary empty I need to select first one as selected
To set a default value for an HTML select element, you need to give the appropriate <option> element the selected attribute. It would look like this:
<option value='50000' selected='selected'>50,000</option>
In your PHP code, you would add this by setting a string for each option, to either "selected='selected'" or blank, depending on whether that option is the one you want to have selected.
This is obviously far easier to put into a loop rather than writing the same code twenty times, so you would need to rewrite your output to create a loop for creating your options.
Hope that helps.
You can do something like this on creation...[not tested]
<?php
$options = array(
'10000' => '10,000',
...
'150000' => '150,000'
); //From MySQL
array_unshift($options, '' => 'Any');
$salary = isset($_GET['salary'])?$_GET['salary']:'';
?>
<select name="salary" class="styled">
<?php foreach ($options as $value=>$text){ ?>
<option <?php if($value == $salary){echo 'selected="selected"'; }?> value="<?php echo $value; ?>"><?php echo $text; ?></option>
<?php } ?>
</select>
If I understand your question correctly you want that if the person doesn't select any thing you want 10000 to be automatically selected for him. And I also assume that the options in the select list are static. So..
<?php
if($_GET['salary']=='')
$salary = 10000;
else
$salary = $_GET['salary'];
?>
In my dropdownlist I have two different values for each option. How can I retrieve both? Let me illustrate what I mean.
<select name="my_ddl">
<option value="<?php $value_Id ?>"><?php $value_text ?></option>
<option value="<?php $value_Id ?>"><?php $value_text ?></option>
</select>
When the form is posted, I want to be able to get both the $value_id and $value_text of the selected option. How can I do this?
$_POST['my_ddl'] only gets one value not both.
In asp.net I could do this simply by referring to my_ddl.Value and my_ddl.Text.
Thank you!
Strictly, this is not possible.
What you could do is use a delimiter in your value attribute:
<select name="my_ddl">
<option value="<?php echo $value_Id ?>|<?php echo $value_text ?>"><?php echo $value_text ?>
</option>
</select>
And...
<?php
list($id, $text) = explode('|', $_POST['my_ddl']);
//...
?>
Another strange way of doing it is:
<select name="my_ddl">
<option value="<?php echo $value_Id ?>[<?php echo $value_text ?>]">
<?php echo $value_text ?>
</option>
</select>
Then when you process it you can do this or maybe even something more simple:
foreach ($_POST['my_dd1'] as $value_Id => $value_text) {
$value_Id = $value_Id;
$value_text = $value_text;
}
Because php treats the [] as meaning the string is an array and so you instantly have an associative array. I agree though that if you put it there in the first place you ought to be able to just look it up again in the code rather than rely on this.
If you're using PHP to populate the text for the <option> then you can probably just look the value up on the server. Perhaps you just need to use the $value_id to look up the text in a database table?
If not, you could include a hidden field on your form and use JavaScript to update that hidden field with the text every time a new value is selected.
You cannot get value_text from POST data. One solution is to populate the hidden field after choosing the option via JavaScript.