How to display input field if select value is null in php - 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\">";
}
?>

Related

How to create the select and radio HTML tag dynamically?

I am creating a form and in the form, all the input fields will display dynamically. I am able to create the text, email, password, checkbox but not able to create the select and radio. I have to use the select tag for country and radio for gender.
I am using CodeIgniter.
<?php
$example_from_name = array('name' => 'user_from_view'); //assigning form name
echo form_open('formbuilder_control/example_from_view',$example_from_name);
foreach ($data as $key) {
$exp_fields_name=$key->fields_name; //here I am getting the name of field name
$exp_fields_type=$key->fields_type; //here I am getting the input type
?>
<div class="form-group row label-capitals">
<label class="col-sm-5 col-form-label"><?php echo $exp_fields_name;?></label>
<div class="col-sm-7">
<input type="<?php echo $exp_fields_type;?>" name="<?php echo $exp_fields_name;?>" placeholder="<?php echo $exp_fields_name;?>" class="form-control" />
<?php echo form_error($exp_fields_name); ?>
</div>
</div>
<?php
}
?>
<div class="form-buttons-w btn_strip">
<input type="submit" value="Save" class="btn btn-primary margin-10">
</div>
<?php echo form_close(); ?>
You don't have enough dynamic data for some of the fields. You will have to pass more from your database. A few of the fields will require values (select, radio, checkbox) Here is the basic structure (as the others were trying to say):
<?php
$example_from_name = array('name' => 'user_from_view'); //assigning form name
echo form_open('formbuilder_control/example_from_view',$example_from_name);
foreach ($data as $key) {
$exp_fields_name=$key->fields_name; //here I am getting the name of field name
$exp_fields_type=$key->fields_type; //here I am getting the input type
echo "<div class=\"form-group row label-capitals\">";
echo "<label class=\"col-sm-5 col-form-label\">$exp_fields_name</label>";
echo "<div class=\"col-sm-7\">";
if(in_array($exp_fields_type,['text','email','password'])){
echo "<input type=\"$exp_fields_type\" name=\"$exp_fields_name\" class=\"form-control\" />";
}elseif($exp_fields_type=='checkbox'){
echo "<input type=\"$exp_fields_type\" name=\"$exp_fields_name\" value=\"[something needed here]\" class=\"form-control\" /> $exp_fields_name";
}elseif($exp_fields_type=='select'){
echo "<select name=\"$exp_fields_name\">";
echo "<option></option>"; // you will have to determine a way to populate the options
// ... more options
echo "</select>";
}elseif($exp_fields_type=='radio'){
echo "$exp_fields_name <input type=\"$exp_fields_type\" name=\"$exp_fields_name\" value=\"[something needed here]\" class=\"form-control\" />";
}else{
echo "Whoops, uncaught field type!";
}
echo form_error($exp_fields_name);
echo "</div>";
echo "</div>";
}
echo "<div class=\"form-buttons-w btn_strip\">";
echo "<input type=\"submit\" value=\"Save\" class=\"btn btn-primary margin-10\">";
echo "</div>";
echo form_close();
We, volunteers, cannot help you with the missing components of your design. You will need to figure out how you are going to pass the necessary options/values to the necessary html form elements.
If you have known field names, you might try writing the conditionals using $exp_fields_name instead of $exp_fields_type to cover a few exceptional cases. Perhaps exchange the above if-block with the following:
if($exp_fields_name=='gender'){
echo "<input type=\"radio\" name=\"gender\" value=\"male\" class=\"form-control\" /> Male<br>";
echo "<input type=\"radio\" name=\"gender\" value=\"female\" class=\"form-control\" /> Female";
}elseif($exp_fields_name=='country'){
echo "<select name=\"country\" class=\"form-control\" />";
echo "<option>India</option>";
echo "<option>Sri Lanka</option>";
echo "<option>Japan</option>";
// ...continue as needed
echo "</select>";
}else{ // all other types default to input tag
echo "<input type=\"$exp_fields_type\" name=\"$exp_fields_name\" class=\"form-control\" />";
}
I assume "checkbox" inputs will have multiple inputs, so you may need to write a custom check for that group as well.
you have to use IF or switch using this type:
<?php
if($exp_fields_type == 'text'){
$input = '<input type="text" name="'.$exp_fields_name;.'" value="" placeholder="" />';
}else if($exp_fields_type == 'radio'){
$input = '<input type="radio" name="'.$exp_fields_name;.'" value="" placeholder="" />';
}
?>
Your code will works only for input tags. So you should check the type before creating the tag
for example :
<?php
if($exp_fields_type == 'select'){
$input = '<select name="'.$exp_fields_name.'" placeholder="" >
<option value="">option 1 </option>
</select>';
}else if($exp_fields_type == 'textarea'){
$input = '<textarea name="'.$exp_fields_name.'"></textarea>';
}else {
$input = '<input type="'.$exp_fields_type.'" name="'.$exp_fields_name.'" value="" placeholder="" />';
}
?>

PHP onchange event of select box

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>

Retain select option inside php if statements

I am attempting to create a single PHP page that opens a salesman or customer window based off a hidden input field in a form being posted. The tricky part for me is I also need two select option dropdowns that appear based on the same hidden input field being posted but ALSO retain their value after submit.
I know how to retain the values of a form like so..
<form action="" method="post">
<select name="newyear">
<option <?php if ($sqlyear == '2015cust') { ?>selected="true" <?php }; ?>value="2015cust">2015</option>
<option <?php if ($sqlyear == '2016cust') { ?>selected="true" <?php }; ?>value="2016cust">2016</option>
</select>
</form>
But when I try and implement this method inside my PHP if statements it does not retain the selected option.
if ($open == 'salesman_window'){
echo "This is salesman dropdown menu";
echo "$sqlyear";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='newyear_trigger' value=''>";
echo "<input type='hidden' name='window' value='salesman_window'>";
echo "<select name='newyear'>";
echo "<option if ($sqlyear == '2015users') { selected='true' }; value='2015users'> 2015</option>";
echo "<option if ($sqlyear == '2016users') { selected='true' }; value='2016users'>2016</option>";
echo "</select>";
echo "<button class='documentation_button'>Submit</button>";
echo "</form>";
}
if ($open == 'customer_window'){
echo "this is customer dropdown menu";
echo "$sqlyear";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='newyear_trigger' value=''>";
echo "<input type='hidden' name='window' value='customer_window'>";
echo "<select name='newyear'>";
echo "<option if ($sqlyear == '2015cust') { selected='true' }; value='2015cust'>2015</option>";
echo "<option if ($sqlyear == '2016cust') { selected='true' }; value='2016cust'>2016</option>";
echo "</select>";
echo "<button class='documentation_button'>Submit</button>";
echo "</form>";
}
?>
I have tried breaking up the option with multiple echo ""; and tried a few ".." of these in there as well but either get syntax errors or it just doesn't retain the selection. Everything works in regards to showing the correct dropdown menu based on what is posted but I just cannot get the select option to retain. The $sqlyear is for sure getting the correct value each time the dropdown option is selected so I know it isn't that either. Can anyone help?
A couple of options:
Use a ternary operator in your echo:
echo "<option" . (($sqlyear == '2015users') ? " selected='true'" : '')
. " value='2015users'>2015</option>";
echo "<option" . (($sqlyear == '2016users') ? " selected='true'" : '')
. " value='2016users'>2016</option>";
Keep using the if, but without opening/closing PHP tags as much:
<option <?php if ($sqlyear == '2015users') echo " selected='true'"; ?> value='2015users'>
2015</option>
<option <?php if ($sqlyear == '2016users') echo " selected='true'"; ?> value='2016users'>
2016</option>

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>";

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>";

Categories