I can't select the top option from the drop-down only the options under the top.
<form>
<select name="car_license" class="form-control" id="car_license" onchange="change()">
<?php for ($i = 0; $i < $size; $i++) { ?>
<option value="<? echo $data[$i]['license'] ?>"><? echo $data[$i]['license'] ?></option>
<? } ?>
</select>
</form>
<script>
function change(){
document.getElementById("myform").submit();
}
</script>
$license = $_POST['car_license'];
Your code seem not complete but will suggest you try this.
<form method="post">
<select name="car_license" class="form-control" id="car_license"
onchange="change()">
<?php for ($i = 0; $i < $size; $i++) {
?>
<option value="<? echo $data[$i]['license'] ?>"><? echo $data[$i]
['license'] ?></option>
<? } ?>
</select>
</form>
<script>
function change(){
//use ajax to submit using "post" method
}
</script>
Related
I just want to insert the value or the name of PROVINCE, CITY and BARANGAY to the database. I'm new in PHP i'm a little confused about this one.
PHP
<?php
session_start();
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"mknr2");
if(isset($_POST['submit'])){
$province = $_POST['province'];
$city = $_POST['city'];
$barangay = $_POST['barangay'];
$sql = "INSERT INTO users(PROVINCE, CITY, BARANGAY) VALUES ('$province','$city','$barangay')";
if($link->query($sql) === TRUE){
echo "Sent!";
}
else{
echo "Error:" . $sql . "<br>" . $link->error;
}
}
?>
HTML
<h2 style="font-size:20px;font-weight:bolder;font-family:arial;margin-left:2%;">Address</h2>
<!-- PROVINCE -->
<select id="provincedd" name="province" onchange="change_province()" class="province">
<option>Select</option>
<?php
$res=mysqli_query($link,"select * from province");
while($row=mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
<?php } ?>
</select>
<!-- PROVINCE -->
<select id="city" name="city" class="city">
<option>Select</option>
</select>
<select id="barangay" name="barangay" class="barangay">
<option>Select</option>
</select>
AJAX.php
<?php
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"mknr2");
if(isset($_GET["province"]))
{
$province=$_GET["province"];
$res=mysqli_query($link,"select * from city where province_id=$province");
echo "<select id='citydd' onchange='change_city()'>";
echo "<option>"; echo "Select"; echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value='$row[id]' selected >"; echo $row["name"]; echo "</option>";
}
echo "</select>";
}
if(isset($_GET["city"]))
{
$city=$_GET["city"];
$res=mysqli_query($link,"select * from barangay where city_id=$city");
echo "<select>";
echo "<option>"; echo "Select"; echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value='$row[id]' selected>"; echo $row["name"]; echo "</option>";
}
echo "</select>";
}
?>
JAVASCRIPT
<script type="text/javascript">
function change_province()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?province="+document.getElementById("provincedd").value,false);
xmlhttp.send(null);
city.style.display = "block";
document.getElementById("city").innerHTML=xmlhttp.responseText;
if(document.getElementById("provincedd").value=="Select")
{
document.getElementById("barangay").innerHTML="<select><option>Select</option></select>";
}
}
function change_city()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?city="+document.getElementById("citydd").value,false);
xmlhttp.send(null);
document.getElementById("barangay").innerHTML=xmlhttp.responseText;
}
</script>
you have set Inside set value to option value and insert this option value in database .
<select name="id" required="" onchange="change_province()" class="province">
<option value="">SELECT CITY</option>
<?php
$res=mysqli_query($link,"select * from province");
while($row=mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
<?php } ?>
</select>
data insert
<?php
session_start();
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"mknr2");
if(isset($_POST['submit'])){
$province = $_POST['province'];
$city = $_POST['city'];
$id= $_POST['id'];
$sql = "INSERT INTO users(PROVINCE, CITY, BARANGAY) VALUES ('$province','$city','$id')";
if($link->query($sql) === TRUE){
echo "Sent!";
}
else{
echo "Error:" . $sql . "<br>" . $link->error;
}
}
?>
Whats wrong with your code?
your select box will not have any option to select
as i can see in code
(a)you have not assigned any values to it.
<select id="city" name="city" class="city">
<option>Select</option>
</select>
<select id="barangay" name="barangay" class="barangay">
<option>Select</option>
</select>
(b)your javascript to call ajax.php which render select box with options will never executed as change_province,change_city functions will not be executed on any event
in this case if your form will be submitted without selecting province,on server side your post array will be like following
["submit"=>"value of submit","province"=>"","city"=>"","barangay"=>""]
now if you take look on your code to insert value your query for inserting data will be like this
INSERT INTO users(PROVINCE, CITY, BARANGAY) VALUES '','','')
causing to insert new record with id(in case id is auto-increment and these values are nullable)
What should you do?
(a)instead of making ajax call to fill select box you should render them at once inside form view
formview.php
<h2 style="font-size:20px;font-weight:bolder;font-family:arial;margin-left:2%;">Address</h2>
<!-- PROVINCE -->
<select id="provincedd" name="province" onchange="change_province()" class="province">
<option>Select</option>
<?php
$res=mysqli_query($link,"select * from province");
while($row=mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row["id"]; ?>"><?php echo $row["name"];?></option>
<?php } ?>
</select>
<!-- PROVINCE -->
<?php
$res=mysqli_query($link,"select * from city");
echo "<select id='citydd' onchange='change_city()'>";
echo "<option>"; echo "Select"; echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value='$row[id]' selected >"; echo $row["name"]; echo "</option>";
}
echo "</select>";
$res=mysqli_query($link,"select * from barangay");
echo "<select>";
echo "<option>"; echo "Select"; echo "</option>";
while($row=mysqli_fetch_array($res))
{
echo "<option value='$row[id]' selected>"; echo $row["name"]; echo "</option>";
}
echo "</select>";
?>
(b)inside your code to insert data ,you should put some server side(also front-end) validation before making insert to data table(you can search google for it),this will prevent any empty entry to be inserted.
hope this will help you.
I have a dropdown list which filled with the data fetched from db. I need to assign the selected index value of this dropdown list as the ID of a button.
Is it really possible?
my code
<select name="customer" onchange="getCustomer()" id="customer" class="form-control" style="width: 180px !IMPORTANT;">
<option value="">--Select Customer--</option>
<?php
$sql = mysqli_query($db,"SELECT * FROM customer ");
while($row= mysqli_fetch_array($sql))
{
?>
<option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
<?php
}
?>
</select>
</div>
<button name="custbt" id="1" class="label-text col-form-label userinfo">Show customer details</button>
<script>
function getCustomer()
{
var e = document.getElementById("customer");
var cust = e.options[e.selectedIndex].value;
// alert(cust);custbt
//document.getElementByName("custbt").id=cust;
document.getElementByName(custbt).id=cust;
}
</script>
Might be this will solve your problem. I have added a class on btn call CustomerModalBttn .
Try this
<select name="customer" id="customer" class="form-control" style="width: 180px !IMPORTANT;">
<option value="">--Select Customer--</option>
<?php
$sql = mysqli_query($db,"SELECT * FROM customer ");
while($row= mysqli_fetch_array($sql)){
?>
<option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
<?php
}
?>
</select>
<button name="custbt" id="1" class="label-text col-form-label userinfo CustomerModalBttn">
Show customer details
</button>
<script>
$(document).ready(function(){
$('#customer').on('change',function(){
$('.CustomerModalBttn').attr('id',$(this).val());
});
});
</script>
IDs must begin with a letter.
<button name="custbt" id="a1" class="label-text col-form-label userinfo">Show customer details</button>
var customer = document.getElementById("customer");
var a1 = document.getElementById("a1");
customer.addEventListener("change",function(){
a1.removeAttribute("id");
a1.setAttribute("id",customer.value);
});
Just add some condition if ($row['custid'] == $_GET['custid']) echo 'selected', if you pass the customer id from url, and then get it with $_GET param.
...
while($row= mysqli_fetch_array($sql))
{ ?>
<option <?php if ($row['custid'] == $_GET['custid']) echo 'selected'; ?> value="<?php echo $row['custid'];?>">
<?php echo $row['customername'];?>
</option>
<?php } ?>
...
I am trying to create a list populated with the columns from a table previously selected from another list of tables. But, I can't get the query to work.
I've appended a sample of my code that is not working. If i replace ".$search_query." with a table name it works. But, I want it to use the table selected by the user.
<!-- Table Select -->
<form name="myform" action="" method="post">
<select id="parentTable_{{$index}}" name="parentTable_{{$index}}" ng-model="parentTable_$index" type="selectable" onchange="submitform();">
<option style="display:none" value="users">select a table</option>
<?php
$count=1;
$sel_query="SHOW TABLES";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row["Tables_in_yamanagolddb"]; ?>"><?php echo $row["Tables_in_yamanagolddb"]; ?></option>
<?php $count++; } ?>
</select>
</form>
<!-- Index reference -->
<?php $search_query = mysqli_real_escape_string($con, $_POST['parentTable_{{$index}}']); ?>
<!-- Field Select -->
<select ng-model="parentField_$index" type="selectable">
<option style="display:none" value="">select a field</option>
<?php
$count=1;
$sel_query="select * from information_schema.columns where table_name = '".$search_query."' and table_schema = 'yamanagolddb'";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row["COLUMN_NAME"]; ?>"><?php echo $row["COLUMN_NAME"]; ?></option>
<?php $count++; } ?>
</select>
<script>
function submitform()
{
document.myform.submit();
}
</script>
<form method="post" id="employee" action="change.php">
<select id="employeename" name="employeename" onchange="this.form.submit()">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
?>
</select>
</form>
The form will be submitted when select value is changed,
Once the form is submitted, it will redirect to the same page. How do I set the selected option value with the one I just selected?
Change your form, especially the <select> dropdown list in the following way,
<form method="post" id="employee" action="change.php">
<select id="employeename" name="employeename" onchange="this.form.submit()">
<?php
while ($row = mysql_fetch_array($result)){
$output = "<option value='".$row['name']."'";
if($_POST['employeename'] == $row['name']){
$output .= " selected='selected'";
}
$output .= ">".$row['name']."</option>";
echo $output;
}
?>
</select>
</form>
<form method="post" id="employee" action="change.php">
<select id="employeename" name="employeename" onchange="this.form.submit()">
<?php
while ($row = mysql_fetch_array($result))
{
if($_POST['employeename']==$row['name'])
echo "<option value='".$row['name']."' selected>".$row['name']."</option>";
else
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
?>
</select>
</form>
just marked as selected if you find selected key in available list.
this is my table in the database
tbl_job_title
ID | Job_title | description |
1 | Accountant | Handle Accounts |
2 | Dev. Support | Support Centre |
when selecting the job title from the table, i'm using a drop down menu and here is my code.
<select id="job_title" >
<option value=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
and I have a textbox that will populate if i select a job_title
<input type="text" id="catch_value">
How do I populate the textbox when I select example is "Accountant", the textbox value must be "Handle Accounts".
Here is the short & sweet code I have made just for you, feel free to use it!
Not required Ajax...
CODE -
<select id="job_title" >
<option value="" class="job" role=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>" class="job<?php echo $row['id']; ?>" role="<?php echo $row['description']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
Jquery -
$('#job_title').on('change',function(){
var m = this.value;
var n = $('.job'+m).attr('role');
$('#catch_value').val(n);
return false;
});
eg - fiddle
<select id="job_title" onchange="javascript:callajaxvalue(this.value);">
<option value=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
<script>
function callajaxvalue(id)
{
$.ajax({
type: "POST",
url: "some.php",
data: { id: id}
success: function(data, textStatus ){
if(data)
{ $("#catch_value").val(data);}
},
})
}
</script>
some.php
$_POST['id'];
$query = "get data from table";
execute query.
echo $row['description'];
For codeigniter
function callajaxvalue(id)
{
$.ajax({
type: "POST",
url: "my_controller/update/"+id,
data: { id: id}
success: function(data, textStatus ){
if(data)
{ $("#catch_value").val(data);}
},
})
}
my_Controller.php
public function update()
{
$id = $this->input->post('id');
$data['discription'] = call model function for get data form database
echo json_encode($data);
}
$('#job_title').change(function(){
$('#catch_value').val('Handle Accounts');
});
see fiddle:fiddle
or you can add if statments
$('#job_title').change(function(){
var jt = $('#job_title').val();
if(jt == "Something")
{
$('#catch_value').val('Handle Accounts');
}
if (jt == "SomethingElse")
{
$('#catch_value').val('somethingelse');
}
});
This is the right answer :
<select id="job_title" onchange='populate()'>
<option value=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
<script>
function populate(){
var jobttl= document.getElementById('job_title').value();
if(jobttl){
document.getElementById('catch_value').value;
}
}
</script>