I'm new at coding
Just looking for some help on this matter,
My code is a snippet from a table that I am designing
What i'm wanting to do is have the date input box disabled unless the first drop down = 'NO', the drop down data is pulled from an SQL query
<td><select name="DATEACC">
<?php
$options = array(' ','YES','NO');
$output = '';
for( $i=0; $i<count($options); $i++ ) {
$output .= '<option '
. ( $Row['custdateaccepted'] == $options[$i] ? 'selected="selected"' : '' ) . '>'
. $options[$i]
. '</option>';
}
echo $output;?>
</select></td>
<td><input type="date" name="ADVDATE" value="<?php echo $Row['dateadvised'];?>"></td>
Can anyone help?
Just disable your date input at the start
<input type="date" id="ADVDATE" name="ADVDATE" value="<?php echo $Row['dateadvised'];?>" disabled>
and enable it using js/jquery when you have your desired value in dropdown
$('[name=DATEACC]').change(function(){
if($(this).val() == 'DESIRED_VALUE')
$('[name=ADVDATE]').enable();
});
You need to use javascript for this. If you have a unique row id $rowid as 1,2,3 etc
<td><select name="DATEACC" onChange="dateEnable(this)" id="DATEACC_$Row['id']">
<?php
$options = array(' ','YES','NO');
$output = '';
for( $i=0; $i<count($options); $i++ ) {
$output .= '<option value="'.$options[$i].'"'
. ( $Row['custdateaccepted'] == $options[$i] ? 'selected="selected"' : '' ) . '>'
. $options[$i]
. '</option>';
}
echo $output;?>
</select></td>
<td><input id="ADVDATE_$Row['id']" type="date" name="ADVDATE" value="<?php echo $Row['dateadvised'];?>"></td>
in javascript
function dateEnable(dropdown){
var id = dropdown.id;
var rowid = id.split('_');
var myselect_value = dropdown.value;
if(myselect_value == 'NO') {
document.getElementById('ADVDATE'+'_'+rowid[1]).disabled = true;
} else {
document.getElementById('ADVDATE'+'_'+rowid[1]).disabled = false;
}
}
Without using jQuery you could try:
javascript
function enabletextfield(e){
var el=typeof( e.target )!=='undefined' ? e.target : e.srcElement;
var value=el.options[ el.options.selectedIndex ].value;
var txtfield=document.querySelectorAll('input[type="date"][name="ADVDATE"]')[0];
txtfield.disabled = value=='NO' ? false : true;
}
html
<select name='DATEACC' id='DATEACC' onchange='enabletextfield(event)'>
<option value='MAYBE'>MAYBE
<option value='NO'>No
<option value='Yes'>Yes
</select>
<input type="date" name="ADVDATE" value="Some value or other" disabled=true />
Obviously change the name of the select menu to suit and add the correct value from the db to the date input box
Related
I am pulling form data from home.php into results.php and using $_GET['xxxxx'] to pre-populate fields of another form. I can populate input fields okay, but how would you compare an option field and $sale_type to selected if equal?
home.php
<form action="results.php" method="GET">
<input id="address" name="address" type="text" class="form-control1"/>
<select type="text" name="sale_type" placeholder="Sale Type">
<option value="Sale">For Sale</option>
<option value="Rent">To Rent</option>
</select>
<input name="submit" type="SUBMIT" value="Next" class="form-control1">
</form>
results.php
Option fields are already populated to ensure the form will work if it's used without results from home.php. I need to compare $sale_type value with the option of name="sale_type and if equal change that option value to selected.
$address = $_GET['address'];
$sale_type = $_GET['sale_type']; ?>
<form method="POST">
<input id="address" name="address" type="text" value='<?php echo $address; ?>'>
<select type="text" name="sale_type" placeholder="Sale Type">
<option value="Sale">For Sale</option>
<option value="Rent">To Rent</option>
</select>
<button type="submit" id="filter">Search</button>
<input type="hidden" name="action" value="ek_search">
</form>
What I'd like results to do
$address = $_GET['address'];
$sale_type = $_GET['sale_type']; ?> //If value is Sale
<form method="POST">
<input id="address" name="address" type="text" value='<?php echo $address; ?>'>
<select type="text" name="sale_type" placeholder="Sale Type">
<option value="Sale" selected>For Sale</option> //Change to selected if equal
<option value="Rent">To Rent</option>
</select>
<button type="submit" id="filter">Search</button>
<input type="hidden" name="action" value="ek_search">
</form>
You could check and then marked as selected when condition satisfy. One of the example is :
<option value="Sale" <?php if(condition) echo "selected" ?> >For Sale</option>
You'd compare the variable to value in the option tag like so:
<option value="Sale" <?= ($sale_type === 'Sale') ? 'selected' : ''; ?>>For Sale</option>
Another, option, if you will, is to set an array of sale_types. Instead of checking each individual option value, you could loop through the options and check there. Like this:
$sale_types = [
'Sale' => 'For Sale',
'Rent' => 'To Rent',
'SomeOtherType' => 'Something Else'
];
Then in select element:
<? foreach ($sale_types as $sale_value => $sale_option): ?>
<option value="<?= $sale_option; ?>" <?= ($sale_type === $sale_option) ? 'selected' : ''; ?>><?= $sale_value; ?></option>
<? endforeach; ?>
If you're going to do very much of this at all, my recommendation is to build a PHP function. Otherwise, you end up with a bunch of mixed HTML / PHP to check conditions.
This allows you to set up and display a dropdown quickly any time you need to.
Something like this would do what you want and would be re-usable:
// Function to generate the HTML for a select
function dropdown_array( $name, $value, $array, $placeholder = '' ) {
$temp_input = '<select name="' . $name . '"';
$temp_input .= ( $placeholder ) ? ' placeholder="' . $placeholder . '"' : '';
$temp_input .= '>' . PHP_EOL;
if ( is_array( $array ) ) {
foreach ( $array as $val => $text ) {
$temp_input .= '<option value="' . $val . '"';
if ( $value === $val ) {
$temp_input .= ' selected';
}
$temp_input .= '>' . $text . '</option>' . PHP_EOL;
}
}
$temp_input .= '</select>' . PHP_EOL;
return $temp_input;
}
Then, in your situation, usage would look like so:
$array = array( 'Sale' => 'For Sale', 'Rent' => 'To Rent' );
echo dropdown_array( 'sale_type', $sale_type, $array, 'Sale Type' );
I have the following form:
<form action="#" method="POST" enctype="multipart/form-data">
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i) {
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i) {
echo "\t", '<option value="', $i, '">', $i, '</option>', "\n";
}
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info"
name="submit_form" value="Click to start chat! " />
The way it works is that a user can select one of the two options from gender and age and search for a user. It is not required for both options to be completed to search.
However, if none of the options are selected, I want it to stay on the current page (`random_chat.php), and echo a message.
Here is my approach:
$refined_gender = isset($_POST['gender']) ? escape($_POST['gender']) : '';
$age_from = isset($_POST['age_from']) ? escape($_POST['age_from']) : '';
$age_to = isset($_POST['age_to']) ? escape($_POST['age_to']) : '';
if (isset($_POST['submit_form'])){
if (empty ($refined_gender) || empty($age_from) || empty ($age_to)) {
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else {
// search user
}
Currently, when I have both search options empty, the message echos, which is good. But the message also echos when I am searching for a male user, or have age not empty, which shouldn't happen.
you need to change || to &&, (Some other changes are also stated as comment):-
<form method="POST" enctype="multipart/form-data"> <!-- remove action ='#' -->
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i) {
if(isset($_POST['age_from']) && $_POST['age_from'] == $i){ // to make selected value to be selected even after form submit
echo "\t", '<option value="', $i. '" selected ="selected">', $i, '</option>', "\n";
}else{
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
}
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i) {
if(isset($_POST['age_to']) && $_POST['age_to'] == $i){// to make selected value to be selected even after form submit
echo "\t", '<option value="', $i. '" selected ="selected">', $i, '</option>', "\n";
}else{
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
}
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info" name="submit_form" value="Click to start chat! " />
<form>
<?php
$refined_gender = isset($_POST['gender']) ? $_POST['gender'] : ''; // escape gives me undefined function error at my end so i removed it, but if it is working at your end thhen keep it up as it is.
$age_from = isset($_POST['age_from']) ? $_POST['age_from'] : '';
$age_to = isset($_POST['age_to']) ? $_POST['age_to'] : '';
if (empty ($refined_gender) && empty($age_from) && empty ($age_to)) { // check always with form posted value not submit button value and also cange || to &&
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else {
// search user
}
?>
The if condition does not reflect what you want fix it like this
if (empty ($refined_gender) && empty($age_from) && empty ($age_to)) {
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else { // search user }
I have this form:
<form method="post">
<fieldset>
<select name="taskOption" required>
<option value="" disabled selected>Choose group</option>
<?php
while($hej < count($hejsan)) {
echo '<option value=' . $hej . '>' . $hejsan[$hej] . '</option>';
$hej++;
}
?>
</select>
<input type="submit" value="Find bookings">
</fieldset>
</form>
When I press the submit button the select/option will reset to the default, how can I change this and make it stay as the one I selected?
Try this
if (isset($_POST['taskOption'])) {
$Option = $_POST['taskOption'];
}
while($hej < count($hejsan)) {
if($hej==$_POST['taskOption']){
$selected = "selected=selected";
}else{
$selected = "";
}
echo '<option value="' . $hej . '" '.$selected .'>' . $hejsan[$hej] . '</option>';
$hej++;
}
You will need to look at the option you have selected and make the corresponding option selected. Since your form leads to the same page, you can just look at the contents of $_POST.
P.S.: also, you will find it useful to echo a newline after each option to make the output more readable.
Try this:
<?php
$taskOption = '';
if (isset($_POST['taskOption']) {
$taskOption = $_POST['taskOption'];
}
?>
<form method="post">
<fieldset>
<select name="taskOption" required>
<option value="" disabled selected>Choose group</option>
<?php
while($hej < count($hejsan)) {
if ($hey == $taskOption) {
echo "<option value={$hey} selected>{$hejsan[$hej]}</option>";
} else {
echo "<option value={$hey}>{$hejsan[$hej]}</option>";
}
$hej++;
}
?>
</select>
<input type="submit" value="Find bookings">
</fieldset>
</form>
At a if else statement to it. If the option value is equal to the select, then add the selected="selected"
This is an example code. could be that the operator is wrong, im always confused with the greater then and smaller then operators.
<?php
$option_value = 5;
for($i=0; $i > 10; $i++){
if($option_value == 5){
echo '<option value="'.$i.'" selected="selected">Item number'.$i.' is selected</option>';
} else {
echo '<option value="'.$i.'">Item number'.$i.'</option>';
}
}
?>
my problem is that i need to make dependable categories with php but i want them to take their options from txt files any ideas how i can do both?
The idea is that they choose one category at first drop down menu and then choose one category at second but i want the categories to be able to change with txt files..
any advice is really apreciated!
Thx for your time!
this is an idea of what i made
<tr>
<td height="67" align="right">ΚΑΤΗΓΟΡΙΑ</td>
<td><label>
<select name="ΚΑΤΗΓΟΡΙΑ" id="ΚΑΤΗΓΟΡΙΑ" class="update" >
<option value="ΚΟΙΝΟΧΡΗΣΤΑ" selected="selected">ΚΟΙΝΟΧΡΗΣΤΑ</option>
<option value="ΘΕΡΜΑΝΣΗ">ΘΕΡΜΑΝΣΗ</option>
<option value="ΑΝΕΛΚΥΣΤΗΡΑΣ">ΑΝΕΛΚΥΣΤΗΡΑΣ</option>
<option value="ΕΙΔΙΚΕΣΔΑΠΑΝΕΣ">ΕΙΔΙΚΕΣ ΔΑΠΑΝΕΣ</option>
<option value="ΙΔΙΟΚΤΗΤΩΝ">ΙΔΙΟΚΤΗΤΩΝ</option>
</select>
<select name="ΚΑΤΗΓΟΡΙΑ2" id="ΚΑΤΗΓΟΡΙΑ2" class="update" disabled="disabled">
<option value="ΘΥΡΩΡΟΣ" selected="selected">ΘΥΡΩΡΟΣ</option>
<option value="ΕΙΔΗΚΑΘΑΡΙΟΤΗΤΑΣ" selected="selected">ΕΙΔΗ ΚΑΘΑΡΙΟΤΗΤΑΣ</option>
</select>
</label></td>
</tr>
Try this: The code is commented enough to be self explaining
<?php
$list1 = file("/path/to/options1.txt");/// read the first options list file
$selectedOption = $list1[0]; // default selected value is the first option as written in the code snipet above
if( isset($_GET["option1"]) && !empty( $_GET["option1"] ) ) {
$selectedOption = urldecode( $_GET["option1"] ) ; // if the user changed the selection we update the second list
}
$list2 = file("/path/to/{$selectedOption}.txt");/// read the second options file based on the first element in the first list (since it will be the default selected value)
$currentUrl = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
?>
<script type="text/javascript">
function load(){
location.href = "<?php echo "$currentUrl";?>" + "?option1=" + document.getElementById("option1").value ;
}
</script>
<tr>
<td height="67" align="right">ΚΑΤΗΓΟΡΙΑ</td>
<td><label>
<select name="option1" id="option1" class="update" onchange="javascript:load()" >
<?php
foreach( $list1 as $_ ){
$selected = "";
if( trim( $selectedOption == trim( $_ ) ) )
$selected = "selected=\"selected\"";
echo "<option value=\"". urlencode( $_ ) ."\" $selected >$_</option>\n";
}
?>
</select>
<select name="option2" id="option2" class="update">
<?php
$selected = "selected=\"selected\"";
foreach( $list2 as $_ ){
echo "<option value=\"". urlencode( $_ ) ."\" $selected >$_</option>\n";
$selected = ""; // only the first element will be marked as selected
}
?>
</select>
</label></td>
</tr>
Using Wordpress I have created a multiple select box so that users can select categories to exclude. When the page initially loads I see my default values pre-selected. However when I select new values and save... I only see the word "Array" echoed and nothing selected?
<select class="amultiple" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>[]" multiple="multiple" size="8">
<?php
global $options;
foreach ($options as $value) {
if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] );
}
}
$categories = &get_categories('type=post&orderby=name&hide_empty=1');
if ($categories) {
$ex_cat = implode(',', $tt_cat_exclude);
foreach ($categories as $category) {
$selected = (in_array($ex_cat, $category->cat_ID)) ? ' selected="selected"' : '';
echo '<option value="' . $category->cat_ID . '"' . $selected . '>' . $category->cat_name . '</option>' . "\n";
}
}
?>
</select>
<br />For testing purposes, print variables: <?php echo $ex_cat; ?>
http://i48.tinypic.com/k9e3qq.gif
You should use implode()
Like so
$ex_cat = implode(',', $tt_cat_exclude);
This will return a comma separated list
This line should be
$selected = (in_array($category->cat_ID, $ex_cat)) ? ' selected="selected"' : '';
Changed to
$selected = (in_array($category->cat_ID, $tt_cat_exclude)) ? ' selected="selected"' : '';
Since the $ex_cat is a string and cannot be used in in_array()
The $ex_cat is now redundant i guess.
Looks like tt_cat_exclude is missing it's opening $
name="tt_cat_exclude[]" means you're defining an array, so it's normal for the output to be "array"
for testing try print_r (outputs the whole architecture of the variable)
or var_dump (outputs the var type too)
When you postback, the field tt_cat_exclude is an array of the values that you've set - because you've name it tt_cat_exclude[] with a [] behind.
Example:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select class="amultiple" id="tt_cat_exclude" name="tt_cat_exclude[]" multiple="multiple" size="8">
<option value="1">TestingA</option>
<option value="2">TestingB</option>
<option value="3">TestingC</option>
<option value="4">TestingD</option>
<option value="5">TestingE</option>
</select>
<input type="submit" value="Submit" />
</form>
<br/><br/>For testing purposes: <?php
if(isset($_POST['tt_cat_exclude'])){
var_dump($_POST['tt_cat_exclude']); // outputs an array of the selected values
}
?>