modal form with dropdown to mysql not submitting - javascript

Can't find posts/answers that work for me. Everything in my page and modal works fine. I'm able fill the form out and select items in my dropdown the way i want. Issue is when i submit the form the page returns error 500 and says it's not working. Any help would be greatly appreciated, new to coding.
Following is one of the html dropdown items (i have three in the form + 2 input fields. As input fields they all worked fine and the form worked perfectly until i converted three to dropdowns):
<tr>
<td>
<label class="col-form-label col-form-label-sm">Department</label>
</td>
<td width="10"></td>
<td>
<select type="text" name="proj_dept" class="form-control form-control-sm" id="validationDefault01" required>
<option value="" selected disabled>Select</option>
<?php $result3 = $dbConn->query("SELECT * FROM a0_lists WHERE list_group='Departments' ORDER BY list_code ASC");
while($row3 = $result3->fetch(PDO::FETCH_ASSOC)) {
echo "<option value=".$row3['list_code'].">".$row3['list_code']."</option>";
} ?>
</select>
</td>
</tr>
Action php for form (again, this worked before I converted a couple fields to dropdowns:
`<?php require_once('../db.php');
$proj_name= $_POST['proj_name'];
$proj_desc= $_POST['proj_desc'];
$proj_priority= $_POST['proj_priority'];
$proj_status= $_POST['proj_status'];
$proj_dept= $_POST['proj_dept'];
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO a0_lists (proj_name, proj_desc, proj_priority, proj_status, proj_dept)
VALUES ('$proj_name', '$proj_desc', '$proj_priority', '$proj_status', '$proj_dept')";
$conn->exec($sql);
echo "<script>alert('Successfully Added!'); window.location='../proj/proj.php'</script>"; ?>`

Related

jquery select multiple options show and hide input type number with placeholder

<div class="form-group"><label for="unit_of">Unit of Measure</label>
<select class="form-control-sm selectpicker" multiple="multiple" id="unit_of" name="unit_of[]" data-selected-text-format="count">
<?php $sql = "select * from unit_of";
$res = mysqli_query($db_handle, $sql);
while ($list = mysqli_fetch_assoc($res)) {
$unit_of = $list['unit_of'];
?>
<option><?= $unit_of; ?></option>
<?php
}
?>
</select>
</div>
<input type="text" class="form-control-sm" id="rates" name="rates" >
<input type="text" class="form-control-sm" id="rates1" name="rates1" style="display:none">
<input type="text" class="form-control-sm" id="rates2" name="rates2" style="display:none">
In the dropdown
Per Minute
Per Page,
Per Hour,
Per Day,
Per Month,
Per Item,
Per Contract,
Others
I want onchange multiple select hide and show input types number e.g.(name="per_page") e.g.("Enter Rate Per Page") for three input type
Thanks in Advance
If I understand correctly, there are 2 questions here really:
How to restrict a multiple select to a max of 3 selections?
There are multiple ways to achieve this, but my recommendation would be to validate this right before the form is submitted. If you're using a form validation library, try adding the code in there. If not, then you can add a custom submit event handler that will validate the selections. Maybe something like:
$("form").on("submit", function(e) {
var selectionCount = $("#unit_of option:selected").length;
if (selectionCount > 3) {
e.preventDefault();
alert("You may only select up to 3 options.");
}
});
How to add a placeholder to the multiple select?
You can add a placeholder option just before the PHP code adds the other options, like so:
<select class="form-control-sm selectpicker" multiple="multiple" id="unit_of"
name="unit_of[]" data-selected-text-format="count">
<option value="">Enter Rate Per Page</option>
<?php $sql = "select * from unit_of";
$res = mysqli_query($db_handle, $sql);
while ($list = mysqli_fetch_assoc($res)) {
$unit_of = $list['unit_of'];
?>
<option><?= $unit_of; ?></option>
<?php
}
?>
</select>
Of course, with this approach, you'll need to add extra validation code to make sure that the placeholder option is not one of the selected options.

How do I populate textbox from a combobox selected

i want a combobox which populates itself from a database column, when a user selects an option from the combobox it will populate textboxes, which are on the same page, with information related to the selection in the combobox.
Below is the code I used to populate a textbox when I selected the select nama from the dropdown combobox. but it doesnt work, the textbox still blank.
I have the drop down working and also it fills a JavaScript array using names but I just cannot work out how to use the array to show in the fields.
i understand it will have to use javascript and Onchange function, can anyone point me in the right direction?
cheers and thank in advance :)
<script src="js/jquery.js"></script>
<script type="text/javascript">
var selected = new Array();
<?php
$query1 = "SELECT * FROM inventori";
$result1 = mysql_query($query1) or die(mysql_error());
// build javascript array
while($row1=mysql_fetch_array($result1)){
echo 'selected['.$row1['selectnama'].'] = new Array();';
echo 'selected['.$row1['selectnama'].']["NAMA_BAHAN_BAKU"] = "'.$row1['NAMA_BAHAN_BAKU'].'";';
echo 'selected['.$row1['selectnama'].']["SATUAN"] = "'.$row1['SATUAN'].'";';
}
?>
function showname() {
var selectnama = document.formInventori.selectnama.value;
document.formInventori.SATUAN.value = selected[selectnama]["SATUAN"];
}
window.onload=function() {
showname();
}
</script>
<form role="form" action="" method="post" name="formInventori">
<div class="form-group">
<label for="NAMA_BAHAN_BAKU">Nama Bahan Baku</label>
<select class="form-control" name="selectnama" onchange="showname()">
<option value="">Pilih Nama Bahan Baku</option>
<?php
$query1 = "SELECT * FROM inventori";
$result1 = mysql_query($query1) or die(mysql_error());
// build javascript array
while($row1=mysql_fetch_array($result1)){
echo '<option value="'.$row1['ID_BAHAN_BAKU'].'">'.$row1['NAMA_BAHAN_BAKU'].'</option>'; }
?>
</select>
</div>
<div class="form-group">
<label for="SATUAN">Satuan Barang</label>
<input type="text" class="form-control" readonly="" id="SATUAN" name="SATUAN" value="">
</div>
</form>
I would use AJAX to do that, i would save the value selected from the combobox and then pass it to a php through ajax so the php executes a query where the selected value is the search "key", then print the result throught php

select option dropdown onchange update variable

Here is my dilemma. A multi-select drop-down displays pre-selected options. When the user makes any changes, I'm running some code in the PHP action file (called through a submit button). I'm trying to accomplish this using a flag and updating the flag in the onchange event of "select option". The rest is working fine. I just wanted to execute the queries in action file only when the user changed selections in drop down rather than every time the form is submitted. However I'm unable to pass the flag successfully. I know I'm missing something basic and have spent two days looking at it. Any help is appreciated.
The relevant code is pasted below:
//Initialize flag
<?php
...
$multi_prog_change_flag = '0';
...
?>
//Hidden Form Element
<form action="update_u.php" method="post" id="cForm" name="cForm" onsubmit="return validateForm()" autocomplete="off">
...
<?php echo '<input type="hidden" id="email" value="'.$email.'"> ';?>
<?php echo '<input type="hidden" id="multiprogchangeflag" name="multiprogchangeflag" value="'.$multi_prog_change_flag.'"> ';?>
...
// Drop-down Element where update is triggered
<tr id="odd">
<td><select id="programName" name="programName[]" onchange="updateflag();>" multiple="multiple" size=10>
..
<option name="drop1" value ="<?php echo $data['Program_Id'] ?>" <?php if ($data[curr_prog] == '1') echo 'selected="selected"'; ?>> <?php echo $data['Program_Name'];?></option>
..
</select>
<input type="hidden" id="pNames" name="pNames" value="" />
</td>
</tr>
// Function to update flag
function updateflag() {
<document.getElementById("multiprogchangeflag").value = "1";
}
// update_u.php PHP Action File
$multi_prog_flag=mysql_real_escape_string($_POST['multiprogchangeflag']);
if ($multi_prog_flag == '1'){
$multi_prog_delete_query=mysql_query("UPDATE multi_prog_access SET is_act = '0', Updated_by = '$updatedby', update_timestamp = NOW() WHERE user_id = '$useid'");
$count = count($multi_prog_names);
for($i=0;$i<$count;$i++){
$multi_prog_ID=$multi_prog_names[$i];
$multi_prog_update_query=mysql_query("INSERT INTO multi_prog_access (user_id, Prog_id, created_by, is_act, Update_timestamp) VALUES ('$ids','$multi_prog_ID', '$updatedby', '1', NOW())");
}
}

How to get id of selected item in database populated combobox in PHP

I have a problem regarding obtaining id from Combo Box populating name of customer from a database containing field id & name. I want to get the id of name selected by user from Combo Box. Following is the code i have applied to populating Combo Box:
<input type="text" class="input-field" id="registerNumber" name="register_number"
list="register_number" placeholder="Select Register No" /></label>
<!-- Populating Editable ComboBox -->
<datalist id="register_number" >
<?php
$result_set= mysqli_query( $connection, "Select id, customer_name from
service_register ");
while($result = mysqli_fetch_assoc($result_set)){
echo "<option value=\"{$result["customer_name "]}\"
id=\"{$result["id"]}\"></option>";
}
?>
You dont have to set the type of the input to text and you should wrap the input with a form tag. The form tag will have the url of the server side code that will handle the form submission. A submit button should be provided for the user. For clarity you should visit this example http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_datalist
<form action="server_code.php" method="get">
<input class="input-field" id="registerNumber" name="register_number"
list="register_number" placeholder="Select Register No" /></label>
<!-- Populating Editable ComboBox -->
<datalist id="register_number" >
<?php
$result_set= mysqli_query( $connection, "Select id, customer_name from
service_register ");
while($result = mysqli_fetch_assoc($result_set)){
echo "<option value=\"{$result["customer_name "]}\"
id=\"{$result["id"]}\"></option>";
}
?>
<input type="submit">
</form>

how to use .change to disable select

i really need a quick answer for this.
how can i disable a select option once value has changed?
it must be available on firstload, then after the user tries to select it first time, it is the disabled for further change. so the user can choose a value only once.
HERE is my code which is dynamic?
<TABLE id="dataTable" width="350px" >
<TR>
<TD><INPUT type="checkbox" name="chk[]"/></TD>
<TD>
<select name="ItemNo[]" id="select" value="ItemNo" onChange="loadXMLDoc(this.value)">
<?php
$sql2="select * from jewelry_system.item where NumStored !='0' order by ItemName asc";
$result2 = mysql_query($sql2);
while($row2=mysql_fetch_array($result2)){
?>
<option value="<?php echo $row2['ItemNo']?>"> <?php echo $row2['ItemName'];?> Php:<?php echo $row2['SalePrice'];?> </option>
<?php } ?>
</select>
</TD>
</TR>
</TABLE >
what do i put in the javascript?
thanks in advance
This should do what you want if it's in your onChange event
this.disabled = true;
Without extending the loadXMLDoc method or adding another method to disable the field the cleanest method would probably be to append this.disabled = true to your select.onchange declaration
<select name="ItemNo[]" id="select" value="ItemNo" onChange="loadXMLDoc(this.value);this.disabled = true;">

Categories