PHP onchange event of select box - php

I am new to PHP and this community
I was trying to use onchange function in php select
but it seems to be not working
I searched for solutions but basically are all with JS and the value goes to the URL, but my value is going to use in php for loop
don't want to get the parameter in URL
please help
echo "<select onchange='myFunction(this.value)'>";
echo "<option value=''>select your number</option>";
echo "<option value='5'>5</option>";
echo "<option value='10'>10</option>";
echo "<option value='15'>15</option>";
echo "</select>";
//////////////////////////////////////////////////////
function myFunction($numbers){
for($count=0;$count<$numbers;$count++)
echo $count;
}
by the way, inside for loop contains some MySQL so that's why i'm using PHP

function change_function(element){
document.location.href = element.value
}
<select class="form-contro-drop" name="val" onChange="change_function(this);" >
<option value="ur.php?val=1">1</option> <option value="ur.php?val=2">2</option></select>
Hope this helps!

I think you can coding like this
<?php
header('contact-type:text/javascript;charset=utf-8');
echo "<select onchange='myFunction(this.value)'>";
echo "<option value=''>select your number</option>";
echo "<option value='5'>5</option>";
echo "<option value='10'>10</option>";
echo "<option value='15'>15</option>";
echo "</select>";
?>
<script>
function myFunction($numbers){
for($count=0;$count<$numbers;$count++)
echo $count;
}
</script>

Related

How to display input field if select value is null in php

How can I display that when select value is null I want to display input field to enter customer own choice. I am new to php so anyone can please suggest me how to do it? Here is my reference code.
<select class="form-control" name="outlet">
<option>Select Outlet</option>
<?php
$values = explode(",",$camp_outlet);
foreach(explode(",",$camp_outlet) as $value){
?>
<option value="<?php echo "$value";?>">
<?php echo "$value";?>
</option>
<?php
}
?>
<?php
$theData = explode(",",$camp_outlet);
if (count($theData) > 0 ){
echo "<select class=\"form-control\" name=\"outlet\">";
echo "<option>Select Outlet</option>";
foreach($theData as $value){ // loop through the data and add the options to the select input
echo "<option value=\"$value\">$value</option>";
}
echo "</select>";
} else {
echo "<input type=\"text\" class=\"form-control\" name=\"outlet\">";
}
?>

Prevent double options in dropdown list

in my dropdown list I insert a value from a mySQL query. This Value is my selected Option.
The problem is, that he duplicate my selected Value (it is logical because I create "6 Options" but the user only should have "5 Options").
How can I prevent this?
This is my Code:
echo "<select id='qty' name='qty' onchange='this.form.submit()'>";
echo "<option value='".$row['a_qty']."' selected>".$row['a_qty']."</option> ";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
echo "<option value='3'>3</option>";
echo "<option value='4'>4</option>";
echo "<option value='5'>5</option>";
echo "</select>";
echo "<script>
And my Output:
Try as below :
echo "<select id='qty' name='qty' onchange='this.form.submit()'>";
for($i=1;$i<=5;$i++)
{
$selected = '';
if($i==$row['a_qty'])
$selected = 'selected="selected"';
echo "<option value='$i' $selected>$i</option>";
}
echo "</select>";

How to add auto increment number to select tag?

$x = date("");
function add_nol($number,$add_nol)
{
while (strlen($number)<$add_nol)
{
$number = "0".$number;
}
return $number;
}
for($y=10;$y<=50;$y++)
{
echo "<select name='id'>";
echo "<option value='". $x."".add_nol($y,3)."'>" . $x."".add_nol($y,3) ."</option>";
echo "</select>";
}
I want to add auto increment numbers into select tag, but I am getting drop down for each numbers from the above code. How to fix this error?
Two things to remember:
The <select> element is used to create a drop-down list.
The <option> tags inside the <select> element define the available options in the list.
With your current code, the generated HTML markup would be similar to this:
<select name='id'>
<option value='010'>010</option>
</select>
<select name='id'>
<option value='011'>011</option>
</select>
...
This is incorrect. You are creating a separate dropdown on each loop iteration. You only need one <select>tag - they should go outside the loop.
echo "<select name='id'>";
for($y=10;$y<=50;$y++)
{
echo "<option value='". $x."".add_nol($y,3)."'>" . $x."".add_nol($y,3) ."</option>";
}
echo "</select>";
Demo
You are repeatedly creating the select tag inside the loop , thats y the select boxes are created instead of option tags. Edit the for loop as:
echo "<select name='id'>";
for($y=10;$y<=50;$y++){
echo "<option value='". $x."".add_nol($y,3)."'>" . $x."".add_nol($y,3) ."</option>";
}
echo "</select>";

php dropdown population with first option blank

can someone tell me is there a possibility to add a "blank option" once the dropdown is populated?
echo '<select name="dropdown" style="width:150px">';
while($rec=mysql_fetch_array($run1))
{
$value = $rec['route'];
echo "<option value=\"$value\">$value</option>";
}
echo '</select>';
Why can't you do it beforehand?
echo '<select name="dropdown" style="width:150px">';
echo '<option value=""></option>';
while($rec=mysql_fetch_array($run1))
{
$value = $rec['route'];
echo "<option value=\"$value\">$value</option>";
}
echo '</select>';
Or if you really need it after (don't know why), just add a selected option to the <option>
echo '<select name="dropdown" style="width:150px">';
while($rec=mysql_fetch_array($run1))
{
$value = $rec['route'];
echo "<option value=\"$value\">$value</option>";
}
echo '<option value="" selected="selected"></option>';
echo '</select>';
After your while block, just add an additional echo:
echo "<option value=\"$value\"></option>";
As already mentioned, it's generally best to do this before your while loop, so
the default is blank. That way you can catch when/if a user forgets to select an
option.
simple trick, add extra line after the while loop.
while($rec=mysql_fetch_array($run1))
{
$value = $rec['route'];
echo "<option value=\"$value\">$value</option>";
}
echo "<option value=\"$value\"></option>";

Show/ hide dynamic combobox

I have two comboboxes which are generated by queries from mysql.
When user choose value in first combobox i need to display second combobox, but if nothing is choosen second combox should not be displayed.
The code:
<?php
echo "<form method= \"post\" name=\"formcombo\" action=''>";
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Choose main category</option>";
while($cat2 = mysql_fetch_array($query2)) {
if($cat2['category_id']==#$category)
{
echo "<option value='$cat2[category_id]'>$cat2[category_name]</option></br>";}
else
{
echo "<option value='$cat2[category_id]'>$cat2[category_name]</option>";
}
}
echo "</select></br>";
echo "<select name='subcat'><option value=''>Choose subcategory</option>";
while($cat = mysql_fetch_array($query1)) {
echo "<option value='$cat[subcat_name]'">$cat[subcat_name]</option>";
}
echo "</select>";
echo "<input type=\"submit\" value =\"Submit\">";
echo "</form>";
?>
I use javascript to generate combobox values:
<script type="text/javascript">
function reload(form){
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='main.php?cat=' + val ;
}
</script>
If you are okay with jQuery js library, it is easy.
see this jQuery plugin http://plugins.jquery.com/project/selectchain
try the demo provided in this page and use the plugin to accomplish.

Categories