First of all this one its just a part of my code, what I'm trying to do here is to send data from DB to html input by clicking a button, other text-input is doing good but in my combo-box I got the problem of not showing anything, I tried to use the same way of other input(add the echo inside the value) its not working on with me, any ideas ??
The code is working without any ERROR
<?php
$query10s = "select payement.ID_PAYEMENT,DATE_PAYEMENT,TYPE_DE_PAYEMENT,PRIX,MOIS_PAYEMENT from eleve,payement,payer where eleve.CIN_ELEVE = payer.CIN_ELEVE and payement.ID_PAYEMENT = payer.ID_PAYEMENT and eleve.CIN_ELEVE = '$InputCIN' group by payement.ID_PAYEMENT";
$resultAf10 = mysqli_query($con,$query10s);
if(mysqli_num_rows($resultAf10)>=0){
while($rows = mysqli_fetch_assoc($resultAf10)){
$ID_PAYEMENT = $rows['ID_PAYEMENT'];
$DATE_PAYEMENT = $rows['DATE_PAYEMENT'];
$TYPE_DE_PAYEMENT = $rows['TYPE_DE_PAYEMENT'];
$PRIX = $rows['PRIX'];
$MOIS_PAYEMENT = $rows['MOIS_PAYEMENT'];
}
}
ID PAYEMENT<input type='text' name='ID_PAYEMENT' class="form-control" value="<?php if(isset($ID_PAYEMENT)){echo $ID_PAYEMENT;}?>">
<div style="font-size:9px;display:none;" class="col-md-12 showAj1">
DATE PAYEMENT<input type='text' name='DATE_PAYEMENT' class="form-control date" value="<?php if(isset($DATE_PAYEMENT)){echo $DATE_PAYEMENT;}?>">
TYPE DE PAYEMENT<br>
<select class='form-control' style="font-size:11px;" name="TYPE_DE_PAYEMENT" value="<?php if(isset($TYPE_DE_PAYEMENT)){echo $TYPE_DE_PAYEMENT;}?>">
<option>Mentuel</option>
<option>Frais inscription</option>
<option>Droit examen</option>
</select>
PRIX<input type='text' name='PRIX' class="form-control col-md-12" value="<?php if(isset($PRIX)){echo $PRIX;}?>"><span style="margin-left:280px;margin-top:-22px;position:absolute;font-size:10px;">DH</span>
MOIS_PAYEMENT<select class="form-control" style="font-size:11px;" name="MOIS_PAYEMENT" value="<?php if(isset($MOIS_PAYEMENT)){echo $MOIS_PAYEMENT;}?>">
<option>Janvier</option>
<option>Février </option>
<option>Mars</option>
<option>Avril </option>
<option>Mai </option>
<option>Juin</option>
<option>Juillet </option>
<option>Août </option>
<option>Septembre </option>
<option>Octobre </option>
<option>Novembre</option>
<option>Décembre</option>
</select><br>
?>
Related
Hi I'm working on the code now and the ticket system sending a notification to a user that selects him about.
I want a notification to select more than one user and more than one user.
I tried to use this for Multi Select more than one user selection, again when I say save too, a single user is registering on this can you help me ?
<div class="form-group select-placeholder">
<label for="assigned" class="control-label">
<?php echo _l('ticket_settings_assign_to'); ?>
</label>
<select name="assigned" id="assigned" class="form-control selectpicker" data-live-search="true" data-none-selected-text="<?php echo _l('dropdown_non_selected_tex'); ?>" data-width="100%">
<option value=""><?php echo _l('ticket_settings_none_assigned'); ?></option>
<?php foreach($staff as $member){ ?>
<option value="<?php echo $member['staffid']; ?>" <?php if($member['staffid'] == get_staff_user_id()){echo '';} ?>>
<?php echo $member['firstname'] . ' ' . $member['lastname'] ; ?>
</option>
<?php } ?>
</select>
</div>
If you want to be able to select multiple you have to add the multiple attribute to the box.
<select multiple>
To receive the values in your code as an array you should change the name from name="assigned" to name="assigned[]"
This will send the multi selected data back as an array to PHP or whichever language you are using
<select multiple name="assigned[]" id="assigned" class="form-control selectpicker" data-live-search="true" data-none-selected-text="<?php echo _l('dropdown_non_selected_tex'); ?>" data-width="100%">
You have to put the multiple attribute in your <select> tag.
Note: The javascript there is used to avoid holding the ctrl-key while selecting multiple options.
window.onmousedown = function (e) {
var el = e.target;
if (el.tagName.toLowerCase() == 'option' && el.parentNode.hasAttribute('multiple')) {
e.preventDefault();
// toggle selection
if (el.hasAttribute('selected')) el.removeAttribute('selected');
else el.setAttribute('selected', '');
// hack to correct buggy behavior
var select = el.parentNode.cloneNode(true);
el.parentNode.parentNode.replaceChild(select, el.parentNode);
}
}
<select name="option-list" size="5" multiple>
<option value="option-1">Option</option>
<option value="option-2">Option</option>
<option value="option-3">Option</option>
<option value="option-4">Option</option>
<option value="option-5">Option</option>
<option value="option-6">Option</option>
<option value="option-7">Option</option>
<option value="option-8">Option</option>
<option value="option-9">Option</option>
</select>
I organized the code this way. But I couldn't get a successful result.
<div class="form-group select-placeholder">
<label for="assigned" class="control-label">
<?php echo _l('ticket_settings_assign_to'); ?>
</label>
<select multiple name="assigned[]" id="assigned" class="form-control selectpicker" data-live-search="true" data-none-selected-text="<?php echo _l('dropdown_non_selected_tex'); ?>" data-width="100%">
<option value=""><?php echo _l('ticket_settings_none_assigned'); ?></option>
<?php foreach($staff as $member){ ?>
<option value="<?php echo $member['staffid']; ?>" <?php if($member['staffid'] == get_staff_user_id()){echo '';} ?>>
<?php echo $member['firstname'] . ' ' . $member['lastname'] ; ?>
</option>
<?php } ?>
</select>
</div>
In my modal window, I have a select where a user can select a breakfast meal from my database. By selecting a meal and submitting it, a record is saved in the database. However, I would like to achieve that when the user enters into the same modal window, whatever meal they have selected before hand will appear as the default option. I have created a query that checks to see if a meal has already been selected (optionquery1). Any ideas to achieve this? Thanks
On a side note, I'm not sure that I'm setting the date php variable currently. The day, month and year is stored in the html hidden inputs but not sure how to extract the values.
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<div class="modal-body">
<form action="my_planner.php" method="post">
<!--<span class="test5"></span>-->
<!--<span class="test"><input type="text" name="id_disabled" value="" disabled/>-->
<input class="test" type="text" name="id1" value="" style="text-align:center; border:0px; font-weight:bold; font-size: 25px;"/>
<hr class="my-4">
<input type="hidden" name="id" value=""/>
<input type="hidden" name="month" value=""/>
<input type="hidden" name="year" value=""/>
<br>
<p id="breakfastTag"><br>Breakfast:</p>
<select id="breakfast" name="breakfast">
<option value="" disabled selected>Select your breakast</option>
<?
$meal='breakfast';
$date='<input type="hidden" name="id" value=""/>'.'<input type="hidden" name="month" value=""/>'.'<input type="hidden" name="year" value=""/>';
$query = $db->prepare("select * from recipe_category where categoryID=1");
$query->execute();
while ($results = $query->fetch()) {
$recipeID=$results["recipeID"];
$query3 = $db->prepare("select * from recipe where id=:recipeID");
$dbParams3=array('recipeID'=>$recipeID);
$query3->execute($dbParams3);
$breakfast = $query3->fetchAll();
$optionquery = $db->prepare("select * from user_planner where userID=:userID and date=:date and meal=:meal");
$optionParams= array ('userID'=>$thisUser, 'date'=>$date,'meal'=>$meal);
$optionquery->execute($optionParams);
while ($results12 = $optionquery->fetch()) {
$selectedmeal = $results12['recipeID'];
}
$optionquery1 = $db->prepare("select * from recipe where id=:recipeID");
$optionParams1= array ('recipeID'=>$selectedmeal);
$optionquery1->execute($optionParams1);
while ($results123 = $optionquery1->fetch()) {
$selectedrecipe = $results123['name'];
}
foreach($breakfast as $breakfast): ?>
<option value="<?= $breakfast['id']; ?>"><?= $breakfast['name']; ?></option>
<?php endforeach;} ?>
</select>
Of course you can, you need to use the selected attribute on the <option> you want to show.
First remove the selected attribute from the first <option>:
<option value="" disabled>Select your breakast</option>
Then in the foreach loop you need an if condition:
$results123 = $optionquery1->fetch();
$selectedrecipe = $results123['id'];
foreach($breakfast as $breakfast): ?>
<option value="<?= $breakfast['id']; ?>" <?php if ($breakfast['id'] == $selectedrecipe) echo 'selected' ?>><?= $breakfast['name']; ?></option>
<?php endforeach;} ?>
Notice I have removed the while loop since we are sure Mysql will return only one record (WHERE ID = <ID>).
I have two drop downs .one have static value and second get values from db. I want to that if value is selected from 1st drop down then relevant values loaded in 2nd drop down. I have tried. but its load all the data from database according to user.for example when user select from request type dropdown having value inquiry.then 2nd drop down load only the values which have catType Inquiry.and if he select the complaint then complaint data must be shown.I have been tried but all the data is loaded ,or only one data is loading.any body help me in this regard.Thanks in Advance. Here is My Code
<div class="col-md-4">
<div class="form-group">
<label for="requesttype"><?php echo $requestField; ?></label>
<select class="form-control" required="" id="requesttype" name="requesttype" onchange="fcrActionChange(this);">
<option value="">Select Request Type</option>
<option value="Inquiry">Inquiry</option>
<option value="Complaint">Complaint</option>
<option value="Service Request/FCR">Service Request/FCR</option>
<option value="Verification Call">Verification Call</option>
</select>
<span class="help-block"><?php echo $requestHelp; ?></span>
</div>
</div>
$("#requesttype").change(function() {
$("#catId).load("navigation.php?requesttype=" + $("#requesttype").val());
});
</script>
<div class="col-md-4">
<div class="form-group">
<label for="catId"><?php echo $categoryField; ?></label>
<select class="form-control" name="catId" id="catId">
$tcat = "SELECT catId, catName FROM categories WHERE userId = ".$userId." AND isActive = 1 AND catType = ".$_GET['requesttype'];
$rest = mysqli_query($mysqli, $tcat) or die('-2'.mysqli_error());
while ($tcatrow = mysqli_fetch_assoc($rest)) {
echo "<option value="$tcatrow['catId'] >";
echo clean($tcatrow['catName'])."</option>";
}
</select>
<span class="help-block"><?php echo $categoryHelp; ?></span>
</div>
</div>
</div>
Your code is pretty confusing but anyway, the important part is within your ajax request and your PHP file
Ex. you have a div id secondOpt to be filled from the query made by the requesttype.
**Note I added a userId input field to pass the value for the SQL query in the navigation.php
<select class="form-control" required="" id="requesttype" name="requesttype" onchange="fcrActionChange(this);">
<option value="">Select Request Type</option>
<option value="Inquiry">Inquiry</option>
<option value="Complaint">Complaint</option>
<option value="Service Request/FCR">Service Request/FCR</option>
<option value="Verification Call">Verification Call</option>
</select>
<input type="hidden" id="userId" value="<?php echo $userId;?>">
<div id="secondOpt"></div>
After this, you will have an ajax request below, you can use $.post from jQuery and render the returned data on the secondOpt element
$('#requesttype').change(function(){
$.post("navigation.php",{requesttype: $(this).val(),userId: $('#userId').val()},function(options)
{
$('#secondOpt').html(options);
});
});
And for the navigation.php UPDATED
//don't forget your config file here to connect with the database
$userId = mysql_real_escape_string($_POST['userId']);
$requesttype = mysql_real_escape_string($_POST['requesttype']);
$output = "<select id='catId'>";
$tcat = "SELECT catId, catName FROM categories WHERE userId = ".$userId." AND isActive = 1 AND catType = ".$requesttype;
$rest = mysqli_query($mysqli, $tcat) or die('-2'.mysqli_error());
while ($tcatrow = mysqli_fetch_assoc($rest)) {
$output.="<option value=".$tcatrow['catId']." >";
$output.=clean($tcatrow['catName'])."</option>";
}
$output.="</select>";
echo $output;
How would I utilise my code for a select input type to use data within a database? I've got a connection to my datebase that works fine it terms of writing data and also retrieving data to a text box etc. Yet to workout how it works for a select type though.
Im currently using :
<select name="product" width="100" style="width: 245px">
<option value="laptop">laptop</option>
<option value="keyboard">keyboard</option>
</select>
How could I change my code so that when a user selects the drop down menu from the select it lists all options from a specific row in my products table?
Updated code:
if(isset($_POST['product'])){
$choice = $_POST['product'];
}
$query="SELECT * from loanproducts WHERE product = '$choice'";
while($row = fetch_assoc($query)){
echo $row[product];
}
endwhile;
if (isset($_POST['addloan'])):
$username=$_SESSION['username'];
$product=$_POST['product'];
$date_collected=$_POST['date_collected'];
$date_return=$_POST['date_return'];
$returned=$_POST['returned'];
$my_query="INSERT INTO loans VALUES ('','$username','$product','$date_collected','$date_return','$returned')";
$result= mysqli_query($connection, $my_query);
if ($result):
header ('location: homepage.php?confirm=New loan successfully created');
else :
echo "<b>This didn`t work, error: </b>";
echo mysqli_error($connection);
endif;
endif;
HTML:
<form method=post action="addloan.php" enctype="multipart/form-data">
<input type="text" name="username" autocomplete="off" size="30" value="<? php echo $_SESSION['username']; ?>" disabled>
<br><br>
<select name="product" width="100" style="width: 245px">
<option value="" disabled selected>Product?</option>
<option value="laptop">laptop</option>
<option value="keyboard">keyboard</option>
</select>
<br><br>
<input type="date" name="date_collected" autocomplete="off" placeholder="Collection Date?" size="30">
<br><br>
<input type="date" name="date_return" autocomplete="off" placeholder="Return Date?" size="30">
<br><br>
<select name="returned" width="100" style="width: 245px">
<option value="" disabled selected>Returned?</option>
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
<br><br>
<input type="submit" name="addloan" value="Create Loan" >
</form>
Given no code to work with (consult my "footnotes"), this is the logic you can use.
Use a WHERE clause from values pulled from a POST/GET array and name attribute(s).
HTML:
<form method="post" action="handler.php">
<select name="product" width="100" style="width: 245px">
<option value="laptop">laptop</option>
<option value="keyboard">keyboard</option>
</select>
<input type="submit" name="submit">
</form>
PHP:
if(isset($_POST['product'])){
$choice = $_POST['product'];
}
Query: N.B.: query() is an example here. Consult my "footnotes".
SELECT * FROM table where col_x = '$choice'
or actual columns:
$query = query($con, "SELECT col_1, col_2, col_3 FROM table where col_x = '$choice'");
Then you would loop over results; fetch_assoc() is an example here. Consult my "footnotes".
while($row = fetch_assoc($query)){
echo $row['col_1'] . " " . $row['col_2'] . " " . $row['col_3'];
}
Footnotes:
The API used to connect with is unknown, so you will need to escape that value and use the respective functions to connect/query with. As is the db schema being unknown.
You will need to adjust accordingly.
The following is my HTML code
<div class="col-md-6">
<label for="martial_status">Martial Status</label>
<select name="martial_status" id="martial_status" class="form-control" value="<?php echo set_value('martial_status'); ?>">
<option value="">Select Martial Status</option>
<?php foreach ($martial_status as $status) { ?>
<option value="<?php echo $status['id'] ?>"><?php echo $status['status_name'] ?></option>
<?php } ?>
</select>
Even though i have used the form_validation in controller side, the value is not getting set in drop down once the form fails submitting. How to set the previous value selected in drop down on form failure. Where am I going wrong.
You cannot set the value straightly to select tag. You need to set the selected attribute to option tag which you want it to be get selected. Just like it below....
<div class="col-md-6">
<label for="martial_status">Martial Status</label>
<select name="martial_status" id="martial_status" class="form-control" value="<?php echo set_value('martial_status'); ?>">
<option value="">Select Martial Status</option>
<?php foreach ($martial_status as $status) { ?>
<option value="<?php echo $status['id'] ?>" <?php if ($status['id'] == set_value('martial_status')) echo "selected = 'selected'"?>><?php echo $status['status_name'] ?></option>
<?php } ?>
</select>
On generating the options tag check which option got selected and echo selected = 'selected' in the attribute of that option tag.