How to insert the value of <option> to the database - javascript

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.

Related

PHP query of columns with value from <select> MySQL

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>

Populate a text box using a selected value in a dependent dropdown

I have this two tables in my database
1 - tbl_category
2 - tbl_shelf_place
(I want to achieve this kind of output where the selected category_name from the drop-down (book_category) with a value of category_id will show the shelf_code in a textbox instead of a DROPDOWN, based on the equivalent shelf_id).
-------------------------- -------------------------
tlb_category tbl_shelf_place
--------------------------- -------------------------
category_id | category_name shelf_id| shelf_code
--------------------------- -------------------------
1 | General Works 1 | General Works Shelf
2 | History 2 | History Shelf
Below is my current code for the dependent dropdown.
My dropdown code in HTML
<select id="book_category" name="book_category" class="selectpicker" data-style="btn btn-danger" onChange="getState(this.value);" required>
<option disabled selected value=""> Choose Category
</option>
<?php
$query = "SELECT category_id , category_name FROM tbl_category";
$stmt = $mysqlconnection->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($category_id , $category_name);
while($stmt->fetch())
{
$getcategoryID = $category_id;
$getcategoryname = $category_name;
echo "<option value='{$getcategoryID}'>{$getcategoryname}</option>";
}
?>
</select>
<select id="book_shelf" name="book_shelf" class="selectpicker" data-style="btn btn-danger" required>
<option disabled selected value="">
</option>
</select>
My script code
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "dropdownrequest.php",
data:'category_id='+val,
success: function(data){
$("#book_shelf").html(data);
}
});
}
</script>
dropdownrequest.php
<?php
include_once ("../database/dbconnect.php");
if(!empty($_POST["category_id"])) {
$query ="SELECT * FROM tbl_shelf_place WHERE shelf_id = '" . $_POST["category_id"] . "'";
$results = $mysqlconnection->query($query);
?>
<?php
foreach($results as $shelf) {
?>
<option value="<?php echo $shelf["shelf_id"]; ?>"><?php echo $shelf["shelf_code"]; ?></option>
<?php
}
}
?>
You can use this code.
<select id="book_category" name="book_category" class="selectpicker" data-style="btn btn-danger" onChange="getState(this.value);" required>
<option disabled selected value=""> Choose Category
</option>
<?php
$query = "SELECT category_id , category_name FROM tbl_category";
$stmt = $mysqlconnection->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($category_id , $category_name);
while($stmt->fetch())
{
$getcategoryID = $category_id;
$getcategoryname = $category_name;
echo "<option value='{$getcategoryID}'>{$getcategoryname}</option>";
}
?>
</select>
<textarea id="book_shelf" name="book_shelf"></textarea>
In your JS.
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "dropdownrequest.php",
data:'category_id='+val,
success: function(data){
$("#book_shelf").val(data);
}
});
}
</script>
In you PHP code.
<?php
include_once ("../database/dbconnect.php");
if(!empty($_POST["category_id"])) {
$query ="SELECT * FROM tbl_shelf_place WHERE shelf_id = '" . $_POST["category_id"] . "'";
$results = $mysqlconnection->query($query);
$shelf_text = '';
foreach($results as $shelf) {
$shelf_text .= $shelf["shelf_id"]." - ".$shelf["shelf_code"];
}
echo $shelf_text;
exit;
}
?>
Hope this will give you and Idea. I haven't tested this code so please pardon if there is any syntax error. Also tweak the code in PHP file so you can show the shelf_code and shelf_id as per your requirements.
You can do it like this, one way to achieve this
PHP
<?php
include_once ("../database/dbconnect.php");
if(!empty($_POST["category_id"])) {
$query ="SELECT * FROM tbl_shelf_place WHERE shelf_id = '" .
$_POST["category_id"] . "'";
$results = $mysqlconnection->query($query);
foreach($results as $shelf){
echo $shelf["shelf_code"];
}
}
?>
No need to return option now. Just read the data returned and add it to your textbox element in HTML
HTML
<select id="book_category" name="book_category" class="selectpicker" data-
style="btn btn-danger" onChange="getState(this.value);" required>
<option disabled selected value=""> Choose Category</option>
<?php
$query = "SELECT category_id , category_name FROM tbl_category";
$stmt = $mysqlconnection->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($category_id , $category_name);
while($stmt->fetch())
{
$getcategoryID = $category_id;
$getcategoryname = $category_name;
echo "<option value='{$getcategoryID}'>{$getcategoryname}
</option>";
}
?>
</select>
// Add HTML Textbox here
<input type="text" id="book_shelf">
Rest of the code should be same.

Set option value from php variable

<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.

How to query table based on option value selected?

How do I hide 'role' selection option until 'project' is selected and then query role where project selected = $project?
I only want to select a role corresponding to its project. The table selectproject contains a row with $id_project, $project and $role. I would like to select the role based on first selecting a project. Ex: "select $role where selected = $project".
How do I use Java script or Ajax to accomplish this? Please provide code for I am still learning Java/Ajax.
<?php
$result4= mysqli_query($con, $idq);
echo '<select id="project" name="project" class="textBox">';
echo '<option value="">-Select-</option>';
while ($row = $result4->fetch_assoc()){
?>
<option value="<?php echo $row['project']; ?>"><?php echo $row['project']; ?></option>
<?php
}
echo "</select>";
?>
<?php
$result5= mysqli_query($con, $idq2);
echo '<select id="role" name="role" class="textBox2">';
echo '<option value="">-Select-</option>';
while ($row = $result5->fetch_assoc()){
?>
<option value="<?php echo $row['role']; ?>"><?php echo $row['role']; ?></option>
<?php
}
echo "</select>";
?>

Dynamic Dropdowns POST

I'm trying to post the values that I get from each dropdown menu, this is an n-level, however I'm going to use them maximum of 3. How can I post every value i get from each selection?
<?php
include('dbcon.php');
if($_REQUEST)
{
$id = $_REQUEST['parent_id'];
$query = "select * from ajax_categories where pid = ".$id;
$results = #mysql_query( $query);
$num_rows = #mysql_num_rows($results);
if($num_rows > 0)
{?>
<select name="sub_category" class="parent">
<option value="" selected="selected">-- Sub Category --</option>
<?php
while ($rows = mysql_fetch_assoc(#$results))
{?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['category'];?></option>
<?php
}?>
</select>
<?php
}
else{echo '<label style="padding:7px;float:left; font-size:12px;">No Record Found !</label>';}
}
?>
http://www.99points.info/2010/12/n-level-dynamic-loading-of-dropdowns-using-ajax-and-php/
change the select option of your like this
<select name="sub_category" class="parent">
<option value="" selected="selected">-- Sub Category --</option>
while ($rows = mysql_fetch_assoc(#$results))
{?>
echo '<option value="'.$row['id'].'">'.$rows['category'].'</option>';
<?php
}?>
</select>
<?php
}
else{echo '<label style="padding:7px;float:left; font-size:12px;">No Record Found !</label>';}
}
</select>

Categories