So, I have the following div and I want to show it just if the item type is equal to "Mobile Phone" or Tablet, selected on the same page with a combo box (also an input but instead of writing it's a combobox), but I just can't figure it out how to wrap this div inside an if condition based on what is selected on the combo box, can anyone help me?
Something like this:
if item = "MobilePhone" or item = "Tablet" --------> Show div
But I have Php code already inside the div.. that's why i just can't do it
<div class="control-group <?php echo !empty($imeiError)?'error':'';?>">
<label class="control-label"><strong>IMEI</strong></label>
<div class="controls">
<input name="imei" type="text" placeholder="IMEI" value="<?php echo !empty($imei)?$imei:'';?>">
<?php if (!empty($imeiError)): ?>
<span class="help-inline"><?php echo $imeiError;?></span>
<?php endif;?>
</div>
</div>
And that's how i select the input that I want
<div class="control-group <?php echo !empty($itemError)?'error':'';?>">
<label class="control-label"><strong>Item</strong></label>
<div class="controls">
<select name="item">
<option value="MobilePhone" <?php echo !empty($item) && $item == 'MobilePhone' ? 'selected' : ''; ?>>MobilePhone</option>
<option value="Tablet" <?php echo !empty($item) && $item == 'Tablet' ? 'selected' : ''; ?>>Tablet</option>
<option value="Computer" <?php echo !empty($item) && $item == 'Computer' ? 'selected' : ''; ?>>Computer</option>
<option value="HeadPhones" <?php echo !empty($item) && $item == 'HeadPhones' ? 'selected' : ''; ?>>HeadPhones</option>
</select>
</div>
You already seem to be checking for $item in your second code, so assuming that $item is set you can use this code to show the div only if "MobilePhone" or "Tablet" is selected:
<?php if ($item == "MobilePhone" || $item == "Tablet") : ?>
<div class="control-group <?php echo !empty($imeiError)?'error':'';?>">
<label class="control-label"><strong>IMEI</strong></label>
<div class="controls">
<input name="imei" type="text" placeholder="IMEI" value="<?php echo !empty($imei)?$imei:'';?>">
<?php if (!empty($imeiError)): ?>
<span class="help-inline"><?php echo $imeiError;?></span>
<?php endif;?>
</div>
</div>
<?php endif ?>
If your div has display:none in your css.
Use select's onchange="myFunc()"
And in your myFunc() look for selected value and do whatever you want.
document.getElementById('yourDivId').style='display:block' sets your div visibility.
<select onchange="myFunction()" id="select"><!--selections with different values --></select>
<script> function myFunction() {
var selected=document.getElementById("select").value;
if(selected=="Mobile")
document.getElementById("yourDivId").style="display:block"; }
</script>
you can use a variable $html to organize html in if condition like below code.
<?php
$item = "MobilePhone";
$html = "";
if ($item == "MobilePhone" || $item == "Tablet") {
$html .= '<div class="control-group';
echo !empty($imeiError)?'error':'';
$html .= '<label class="control-label"><strong>IMEI</strong></label><div class="controls">
<input name="imei" type="text" placeholder="IMEI" value="'.!empty($imei)?$imei:''; '">';
if (!empty($imeiError)):
$html .= '<span class="help-inline">';
echo $imeiError;
$html .= '</span>';
endif;
$html .='</div></div>';
}
?>
Related
Hi what i would like to do in my dropdown is to put a condition if($formdata[0]['Phase']==1){ to show only the option values from $ahk 1x in dropdown but if($formdata[0]['Phase']==3){ to show only the option values from $ahk 3x in dropdown here is my code
<div class="form-group col-sm-6 col-xs-12">
<label for="d13"><?php echo $t[$tmpvalues[0]['Lang']]['d13'];?></label>
<select class="form-control el-text-box" name="d13" onchange="this.form.submit()">
<?php for ($j=0; $j<sizeof($ahk); $j++){
$selected="";if($ahk[$j]==$formdata[0]['d13']){$selected="selected";}
?>
<option value="<?php echo $ahk[$j];?>" <?php echo $selected; ?>><?php echo $ahk[$j];?></option>
<?php
}
?>
</select>
</div>
values of $ahk are
$ahk[0]="1X10A";
$ahk[1]="1X20A";
$ahk[2]="1X30A";
$ahk[3]="1X40A";
$ahk[4]="1X50A";
$ahk[5]="1X60A";
$ahk[6]="1X70A";
$ahk[7]="1X80A";
$ahk[8]="3X10A";
$ahk[9]="3X20A";
$ahk[10]="3X30A";
$ahk[11]="3X40A";
$ahk[12]="3X50A";
$ahk[13]="3X60A";
$ahk[14]="3X80A";
$ahk[15]="3X100A";
$ahk[16]="3X160A";
$ahk[17]="3X200A";
$ahk[18]="3X300A";
$ahk[19]="3X400A";
$ahk[20]="3X500A";
$ahk[21]="3X1000A";
$ahk[22]="3X1600A";
$ahk[23]="3X2000A";
$ahk[24]="3X2600A";
$ahk[25]="3X3200A";
Use an if statement to check if the array value matches the phase.
<div class="form-group col-sm-6 col-xs-12">
<label for="d13"><?php echo $t[$tmpvalues[0]['Lang']]['d13'];?></label>
<select class="form-control el-text-box" name="d13" onchange="this.form.submit()">
<?php
$prefix = $formdata[0]['Phase'] . "X";
foreach ($ahk as $option) {
if (substr($option, 0, 2) == $prefix) {
$selected = $option == $formdata[0]['d13'] ? "selected" : "";
?>
<option value="<?php echo $option;?>" <?php echo $selected; ?>><?php echo $option;?></option>
<?php
}
}
?>
</select>
</div>
enter image description hereI'm creating a program, using the dropdown list.
so here is the form "Pilih Unit Mesin", where the machine unit that has the number "Torsi" in the database automatically will appear numbers in the form of "Torsi"
The problem is how if the machine unit has no numbers in database, i hope in the form "Torsi" can be input manually number
Please help, thank you
<label>Pilih Unit Mesin</label>
<select class="form-control" name="id_unit" id="id_unit" required="">
<option value=""><b>nama proyek - unit mesin</b></option>
<?php foreach ($mesin as $key => $value) :?>
<?php if ($value['status_unit'] === '0'): ?>
<option <?php echo $unit_selected == $value['id_unit'] ? 'selected="selected"' : '' ?>
value="<?php echo $value['id_unit'].'">'.' - '.$value['nama_project'].' - '.$value['unit_mesin'].'</option>'; ?>">
<?php else: ?>
<?php endif; ?>
<?php endforeach?>
</select>
</div>
<label>Torsi</label>
<select class="form-control" name="torsi" id="torsi" required="">
<?php foreach ($poross as $key => $val) :?>
<?php if ($val['torsi_poros'] != null): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="<?php echo $val['id_unit'] ?>" value="<?php echo $val['torsi_poros'] ?>"><?php echo $val['torsi_poros'] ?></option>
<span class="input-group-addon">Kg.mm</span>
<?php elseif ($val['torsi_poros'] != ""): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="form-group input-group">
<input class="form-control" name="torsi" type="text" required="" pattern="^(\+|-)?[0-9].*$">
<span class="input-group-addon">Kg.mm</span>
</div>
<?php endif; ?>
<?php endforeach?>
</select><br>
enter image description here
Screenshot 1:
Screenshot 2:
Screenshot 3:
enter image description here
You could combine multiple array functions to check whether specific key from multidimensional array is not empty :
<label>Torsi</label>
<?php
if (empty(array_filter(array_column($poross, 'torsi_poros')))) {
?>
<input type="number" name="torsi" id="torsi" class="form-control" >
<?php } else { ?>
<select class="form-control" name="torsi" id="torsi" required="">
<?php foreach ($poross as $key => $val) :?>
<?php if ($val['torsi_poros'] != null): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="<?php echo $val['id_unit'] ?>" value="<?php echo $val['torsi_poros'] ?>"><?php echo $val['torsi_poros'] ?></option>
<span class="input-group-addon">Kg.mm</span>
<?php elseif ($val['torsi_poros'] != ""): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="<?php echo $val['id_unit'] ?>" value="<?php echo $val['torsi_poros'] ?>"><?php echo $val['torsi_poros'] ?></option>
<span class="input-group-addon">Kg.mm</span>
</div>
<?php endif; ?>
<?php endforeach?>
</select>
<?php } ?>
<br>
Using array_column to return the values from a single column in the array, then array_filter to discard empty array.
I'm trying to show an error message if a checkbox has not been selected. I've managed to get it done input field, but unsure of how to get it done with a checkbox. This is what I have so far:
<?php
# check if data has been posted
if (!empty($_POST)) {
# keep track validation errors
$author_error = null;
$categories_error = null;
# keep track of post values
$author = $_POST['author'];
$categories = $_POST['categories'];
# validate input
if (empty($author)) {
$author_error = 'Please select author';
$valid = false;
}
if (empty($categories)) {
$categories = 'Please select categories';
$valid = false;
}
# if data is valid, insert into the database
if ($valid) {
}
}
?>
<div class="control-group <?php if (!empty($author_error)){ echo 'error'; } ?>">
<label class="control-label">Author</label>
<div class="controls">
<select name="author" id="author">
<option value="">Select one</option>
<?php $sql2 = 'SELECT id, name FROM author';
foreach ($dbConnection->query($sql2) as $data2) { ?>
<option value="<?php echo $data2['id']; ?>"
<?php if(isset($author) && $author == $data2['id']) { echo 'selected'; } ?>>
<?php echo $data2['name']; ?>
</option>
<?php } ?>
</select>
<?php if (!empty($author_error)) { echo '<span class="help-inline">' . $author_error . '</span>'; } ?>
</div>
</div>
<div class="control-group <?php if (!empty($categories_error)){ echo 'error'; } ?>">
<fieldset>
<legend class="control-label">Categories:</legend>
<?php $sql3 = 'SELECT id, name FROM category';
foreach ($dbConnection->query($sql3) as $data3) { ?>
<div class="controls">
<label for="category<?php echo($data3['id']);?>">
<input type="checkbox" name="categories" id="categories" value="<?php echo($data3['id']); ?>"
<?php if(isset($_POST['categories']) && in_array($data3['id'], $_POST['categories'])) { echo 'checked'; } ?>>
<?php echo($data3['name']); ?>
</label>
</div>
<?php } ?>
</fieldset>
<?php if (!empty($categories_error)) { echo '<span class="help-inline controls">' . $categories_error . '</span>'; } ?>
</div>
Where am I going wrong with the categories field?
Check it like:
isset($_POST['categories'])
On line #62 you are checking if $categories_error is empty but you are using variable $categories for storing error message on line #19
I used selection box with multiple selection now the problem is that it is selectable with ctrl+click of mouse. It is work properly but not that much prefrble to me and lookes like simple selection box and user cant get that its multiple selector not single.so thats why i want it with check box so user easly get it is multiple selector.please give apropriate solution thanks in advanced...
<select class="selopt" id="selPreLoc" name="SelPreLoc[]" multiple="multiple" size=5>
<option label="No Preference">No Preference</option>
<?php
//<option value=-1 selected>No Preference</option>
while ($rec = mysql_fetch_array($GetCityRecord)) {
if ($rec['City_Id'] == 30 || $rec['City_Id'] == 34 || $rec['City_Id'] == 35) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
?>
<option value="<?php $rec['City_Id']; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $rec['City']; ?>
</option>
<?php
}
foreach ($others as $ind => $val) {
?>
<option value="<?php echo $ind; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $val; ?>
</option>
<?php }
?>
</select>
<label class="formtxt" valign="bottom">Use Ctrl + Click to multi-select.</label></td>
I got many other solution with using div or other.
but i just want it with select option only is it posible if yes how .andi can fetch the result in mysql and i want that result with comma seprator in mysql.
My code is not tested, since i do not have the data, but based on your logic, you can use it like this:
$theOthers = array(30, 34, 35);
while ($rec = mysql_fetch_array($GetCityRecord)) {
if (in_array($rec['City_Id'], $theOthers)) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php $rec['City_Id']; ?>" <?php echo $checked; ?> /> <?php echo $rec['City']; ?> <br />
<?php
}
foreach ($others as $ind => $val) {
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php echo $ind; ?>" <?php echo $checked; ?> /> <?php echo $val; ?> <br />
<?php
}
NOTE: I improved your code a littlebit with the $theOthers array, i think it's more readable.
When form is submitted, let's var_dump($_POST["city"]);
I have a form that has a select field that loads the options dynamically from a db result query. Here is what the code looks like. See the description text input afterwards? I need the code to return the description of the item selected under productID. How do I go about this? Thanks very much for all replies.
<div class="row-fluid">
<div class="span3">
<label>SKU</label>
<?php echo '<select name="ITEM" id="user" class="textfield1">';
while($res= mysql_fetch_assoc($sql))
{
echo '<option value="'.$res['productID'].'">';
echo $res['SKU'] ;
echo'</option>';
}
echo'</select>';
?>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<label>Description</label>
<input type="text" name="description" value=""/>
</div>
</div>
You can do this in 2 ways:
First way is by redirecting the page having a $_GET parameter which will contain the product id:
<div class="row-fluid">
<div class="span3">
<label>SKU</label>
<?php echo '<select name="ITEM" id="user" class="textfield1"
onchange="document.location=\'my-page.php?pid=\' + this.value">';
while($res= mysql_fetch_assoc($sql))
{
echo '<option value="'.$res['productID'].'"';
// LATER EDIT
if(isset($_GET['pid']) && $_GET['pid'] == $res['productID'])
echo 'selected="selected"';
// END LATER EDIT
echo '>';
echo $res['SKU'] ;
echo'</option>';
}
echo'</select>';
?>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<label>Description</label>
<?php
if(isset($_GET['pid']) && is_numeric($_GET['pid'])) {
$sql = mysql_query("SELECT description
FROM products
WHERE product_id='" . mysql_real_escape_string($_GET['pid']) . "'");
$row = mysql_fetch_assoc($sql);
}
?>
<input type="text" name="description" value="<?=$row['description']?>"/>
</div>
</div>
Second way is to have an ajax call and fill description input dynamically, without refresing the page
// this is the JS code
$(document).ready(function(){
$('#user').change(function(){
$.POST("my-ajax-call-page.php",
{pid: $("#user").val()},
function(data){
$('input[name="description"]').val(data.description);
}, "json");
});
});
and your my-ajax-call-page.php should be like this:
<?php
include("mysql-connection.php");
$sql = mysql_query("SELECT description
FROM products
WHERE product_id='" . mysql_real_escape_string($_POST['pid']) . "'");
$row = mysql_fetch_assoc($sql);
echo json_encode("description" => $row['description']);
?>
You will find many examples and documentation for using jQuery library on jQuery library website
<div class="row-fluid">
<div class="span3">
<label>SKU</label>
<?php echo '<select name="ITEM" id="user" class="textfield1" onchange="showDesc()">';
$desHTML = "";
echo "<option value='0'>Please select</option>"
while($res= mysql_fetch_assoc($sql))
{
echo '<option value="'.$res['productID'].'">';
echo $res["SKU"] ;
echo'</option>';
$desHTML .="<div class'descBox' id='".$res['productID']."' style='display:none'>".$res['description']."</div>";
}
echo'</select>';
?>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<label>Description</label>
<?php echo $desHTML; ?>
</div>
</div>
Now create one js function and call on onchange of select box.
Js function Hint:
$(".descBox").hide();
$("#"+selectedItemValue).show();
Let me know if you need any help for JS function.
You haven't shown us your SQL query, but I assume that you have a column named description and you are selecting this column in your query too.
So then, you can use jQuery to insert description of selected item to input
<div class="row-fluid">
<div class="span3">
<label>SKU</label>
<?php echo '<select name="ITEM" id="user" class="textfield1">';
$js_array = array(); // This variable will contain your Javascript array with descriptions
while($res= mysql_fetch_assoc($sql))
{
echo '<option value="'.$res['productID'].'">';
echo $res['SKU'] ;
echo'</option>';
// Fill your array with descriptions; ID of item will be the index of array
$js_array[$res['productID']] = $res['description'];
}
echo'</select>';
?>
<script>
var description;
<?php
foreach($js_array as $description => $id)
{
echo("description['".$id."'] = '".$description."';\n");
}
?>
$(document).ready(function(){
$('#user').change(function(){
$("#description").val(description[$('#user').val()]);
})
});
</script>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<label>Description</label>
<input type="text" name="description" id="description" value=""/>
</div>
</div>
Be sure to not forget to add id attribute to your input type="text"