how to get dropdown value from left join table - php

I have problem when I get value from left join table, database from product table (https://ibb.co/zbpJP0z )and database from farm table !(https://ibb.co/gMKySpP )
I've tried to get the value from dropdown list, but the value is always from joining table
this is my form code
<form class="mb-2" id="addproductform" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="serial-number">Serial Number:</label>
<select class="serial-number form-control" name="serial-number" class="custom-select mb-3" id="newval">
<option>Serial Number</option>
<?php $q=mysqli_query($link,"SELECT produk.nama FROM produk LEFT JOIN farm ON produk.SN=farm.SN WHERE username=\"$_SESSION[username]\"");
while($d=mysqli_fetch_row($q)) {
echo "<option value=$d[0]>$d[0]</option>";
} ?>
</select>
</div>
</form>
..........................
and this is a jquery code
$(document).ready(function(e){
$("#save_addPlant").click(function(e){
e.preventDefault();
console.log("submit productform");
$("select.serial-number").change(function(){
var tp = $(this).children("option:selected").val();
});
namafarm=$("option:selected").val();
console.log(namafarm);
});
});
How can I take a value from farm.SN, while I display produk.nama in the dropdown list?

There are 2 places that need change in your code.
Your are using 2 class attributes in your select tag. The second
class attribute wont work. So you have to combine the 2 class
attributes into 1.
You are only selecting produk.nama column in your select query. You
must also select produk.SN column.
Replace your select block with the code below & check whether it works.
<select class="custom-select mb-3 form-control serial-number" name="serial-number" class="" id="newval">
<option>Serial Number</option>
<?php
$q = mysqli_query( $link, "SELECT produk.SN,produk.nama FROM produk INNER JOIN farm ON produk.SN = farm.SN WHERE username = \"$_SESSION[username]\"" );
while($d=mysqli_fetch_row($q)) {
echo "<option value=$d[0]>$d[1]</option>";
}
?>

Related

States list drop down is giving first value even after selecting any value from the list upon form submission

Below is my complete code to populate states list based on selected country. When I select any country from country drop down, it returns proper states list in states drop down. This part is working fine.
When I select country USA, states drop down is filled with states of USA and default selected state is Alska. Now if I change state value from Alaska to 'Texas' and submits form, it just sends value 0 to submit page.
I think this 0 value is coming from <option value="0">-- Select State --</option> in id=before_get_state. So how can I resolve this issue? Please help.
Country Drop Down
GetRecordSet is used to fetch records from MySQL database
CheckSelected is used to display specific value as default selected value in dropdown
<form name="frm_shipping_address" id="frm_shipping_address" novalidate>
<div class="control-group form-group">
<div class="controls">
<label>Select Country</label><span class="text-help-form"> * </span>
<?php
$str_query_select="";
$str_query_select="SELECT * FROM " .$STR_DB_TABLE_NAME_COUNTRY. " WHERE visible='YES' ORDER BY title ASC";
$rs_list_country=GetRecordSet($str_query_select);
?>
<select name="cbo_country" id="cbo_country" class="form-control" onChange="get_state(this.value);" required="" data-validation-required-message="Select Country" >
<option value="0">-- Select Country --</option>
<?php
while(!$rs_list_country->EOF()==true) { ?>
<option value="<?php print($rs_list_country->fields("pkid"))?>" <?php print(CheckSelected($rs_list_country->fields("pkid"),$int_user_countrypkid)); ?>><?php print($rs_list_country->fields("title")); ?></option>
<?php $rs_list_country->MoveNext(); } ?>
</select>
</div>
</div>
<button id="btn_continue" class="btn btn-primary btn-lg btn-block"><b>SUBMIT </b></button>
</form>
State Drop Down
<div class="control-group form-group">
<div class="controls">
<label>Select State</label><span class="text-help-form"> * </span>
<div id="before_get_state">
<select name="cbo_state" id="cbo_state" class="form-control">
<option value="0">-- Select State --</option>
</select>
</div>
<div id="after_get_state"></div>
</div>
</div>
jQuery / AJAX
jquery.min.js, jqBootstrapValidation.js files are included in the page
<script>
function get_state(countrypkid) {
var int_countrypkid = countrypkid;
var dataString = "countrypkid="+int_countrypkid;
$.ajax({
type: "POST",
url: "./product_address_get_state_p.php",
data: dataString,
success: function(html)
{
$("#before_get_state").hide();
$("#after_get_state").html(html);
}
});
}
</script>
product_address_get_state_p.php Page Code
$int_statepkid = 0;
if(isset($_SESSION['userpkid']) && $_SESSION['userpkid'] != "")
{
$str_query_select = "SELECT statepkid FROM t_user WHERE pkid=".$_SESSION['userpkid'];
$rs_list_user = GetRecordSet($str_query_select);
$int_statepkid = $rs_list_user->fields("statepkid");
}
$int_countrypkid = 0;
if(isset($_POST["countrypkid"]))
{
$int_countrypkid = trim($_POST["countrypkid"]);
}
$str_query_select = "SELECT * FROM " .$STR_DB_TABLE_NAME_STATE. " WHERE masterpkid=".$int_countrypkid." AND visible='YES' ORDER BY title ASC";
$rs_list_state = GetRecordSet($str_query_select);
echo "<select class='form-control' name='cbo_state' id='cbo_state'>";
while(!$rs_list_state->EOF()==true)
{
echo "<option value='" .$rs_list_state->fields('pkid'). "' ".CheckSelected($rs_list_state->fields('pkid'),$int_statepkid).">".$rs_list_state->fields('title')."</option>";
$rs_list_state->MoveNext();
}
echo "</select>";

How to auto selected save value from database in to select HTML tag

How to auto select the save value fetched from database in to select HTML tag.
I have a list of rooms with Hotel name , room type , facilities , descriptions in a table. i want to edit that record . when i click on edit button it fetch room_id to edit row accordingly, all other values successfully auto fetched in textboxes except values in select tag.
Here is my code to fetch values from database and echo to corresponding textboxes , except select box . i would like to auto select value in select box.
$query = "SELECT * from room where room_id = '$id'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
while($row=mysqli_fetch_assoc($result))
{
$room_id=$row['room_id'];
$room_type_id=$row['room_type_id'];
$facilities = $row['facilities'];
$long_description=$row['long_description'];
}
?>
<form action="Edit_Room_Script.php" method="post" enctype="multipart/form-data">
<label class="form-label">Hotel Name</label>
<select class="form-control" name="name" id="name">
<?php
$sel_cus = "select hotel_name from hotels ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['hotel_id'];?>"><?php echo $row['hotel_name'];?></option><?php}?>
</select>
<label class="form-label">Room Type</label>
<select class="form-control" name="room_type" id="room_type">
<?php
$sel_cus = "select Room_Type_Id,Room_Type_Name from room_type ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['Room_Type_Id'];?>">
<?php echo $row['Room_Type_Name'];?></option>
<?php
}
?>
</select>
<label class="form-label">Facilities</label>
<input class="form-control" type="text" id="facilities" value="<?php echo $facilities;?>" name="facilities" autocomplete="Off" required >
<button class="btn btn-success btn-cons" type="submit" name="update" id="update"> Update Room</button>
</form>
i have a row in my html table is like below
Sr Hotel Name Roomtype Facility Action
1 Serena Super ABC Edit
When i click to edit button it take me to edit from where facility value successfully auto fetched from database and set in text box but hotel name and room type is not set. In select tag for hotel name and room type it populates all the hotel name and room type except serena and super how could i achieve this Please guide with some code.
Thank you
Some modifications:
1) Get $hotel_id
2) To show <select> element selected, you need to add selected="selected" attribute in your <option> tag.
If you have a drop down HTML:
<select class="form-control" name="room_type" id="room_type">
<option value="1">Villa</option>
<option value="2">Terrace</option>
<option value="3" selected="selected">Gallery</option>
<option value="4">Delux</option>
</select>
Drop down will show Gallery selected.
In your case, you can show it by:
<?php
$sel_cus = "select Room_Type_Id,Room_Type_Name from room_type ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['Room_Type_Id'];?>"
<?php if ($room_type_id == $row['Room_Type_Id']) { echo 'selected="selected"';}?>>
<?php echo $row['Room_Type_Name'];?></option>
<?php
}
?>
3) Also, first fetch the arrays from database and then loop over them in the <select> element as writing fetching code directly in <select> is a bad practise and in case of any issue, it my expose your database field names.

How to insert data from multiple select dropdown values into database?

I have a multiple select dropdown with checkbox. But I am not sure how to insert it into database and the next time the page opens, it displays the current value from the database.
How can I insert values from my multiple select dropdown into database and display it as checked?
<div class="form-group">
<div align="left">
<div class="formlist">Purchase Group :
<select id="purchase_group" name="purchase_group" class="form-control" multiple="multiple">
<?php
$query_pgr = "SELECT * FROM purchasing_group WHERE pgr_enable=1 ORDER BY pgr_name";
$rs_pgr = DB_Query($query_pgr);
while ($row_pgr = DB_FetchRow($rs_pgr)) {
$pgr.='<option value='.$row_pgr["pgr_id"].'>' .$row_pgr["pgr_name"].' </option>';
}
mysql_free_result($rs_pgr);
echo $pgr;
?>
</select>
</div>
</div>
</div>
this is the display of my multiple dropdown.
$('#btnSelected').click(function () {
var selected = $("#purchase_group option:selected");
var message = "";
selected.each(function () {
message += $(this).text() + " " + $(this).val() + "\n";
});
alert(message);
});
Every multiple dropdown plug-in hides the <select> with their own dropdown, what ever selection you do can be fetched in php like:
$selection = $_REQUEST['purchase_group'];
here $selection is an array you can use foreach() to get its values and insert it into database.
Note: To hold multiple values, the name of the <select> must be an array like:
<select id="purchase_group" name="purchase_group[]" class="form-control" multiple="multiple">

Displaying database contents from drop down selection

In my website I have a page to display testimonials. I wrote this code to display my all testimonials from database.
This is my code so far :
while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) {
$testimonial = $row['testimonial'];
//echo $testimonial;
$mytestimonial = nl2br($testimonial);
$city = $row['city_name'];
$name = $row['name'];
$url = $row['web_address'];
$imageName = $row['image_name'];
$type = $row['membership_type'];
}
With this code I can get all my testimonials to the page. Its pretty working for me. My problem is now I need to filter my testimonials according to its type. I have 3 different kind of testimonials in my database. (tutor, institute, student)
I am going to use a select box to filter the data. When selecting an option from select box I need to display testimonials according to that selected type.
<div class="filter-box">
<div id="select_box">
<form method="post" action="">
<div class="variation2">
<label>Filter By</label>
<select class="select">
<option>Tutor</option>
<option>Institute</option>
<option>Student</option>
</select>
</div>
</form>
</div>
</div>
Can anyone get me going in a direction here?
Thank You
Why not reload the select box dynamically (using AJAX) once the user has selected one of the 3 options and display your required testimonial.I guess that will solve your problem.
So you want to then select the testimonials based on their type
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
FROM testimonials
INNER JOIN city ON city.city_id = testimonials.city_id
WHERE type = '$type'
ORDER BY date_added DESC LIMIT $start, $display";
Now you also want to get the type from the user
<select name="type" class="select">
With the POST
$type = $_POST['type'];
Auto submit the form on change example
Javascript:
<script type="text/javascript">
function submitform(){
document.frmType.submit();
}
</script>
Form:
<form name="frmType" method="post" action="">
<select name="type" class="select" onchange="submitform()">
Further examples can be found here
You have a couple options. The first is with ajax.
Second is submitting the form onchange or onsubmit:
<select class="select" name="type" onchange="document.forms[0].submit();">
<option value="Tutor">Tutor</option>
<option value="Institute">Institute</option>
<option value="Student">Student</option>
</select>
And your query:
$type = '';
if(!empty($_POST) && isset($_POST[type])){
$type = " WHERE testimonials.type = '".$_POST[type]."'";
}
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
FROM testimonials
INNER JOIN city ON city.city_id = testimonials.city_id
".$type."
ORDER BY date_added DESC LIMIT $start, $display";
Of course make sure to check if the form was submitted.
add a class by the name to each of the testimonial divs
<div class="student">testimonial goes here</div>
now use jquery to hide/show the categories at one shot.
$('.student').show();
i hope it helps

Make Postback to php file, when select value in <select>

I'm making a website, where I should be able to delete some brands from a database, the brands is stored in categories.
In my form, I have a that echo's out all the categories in my database.
The next should then contain all brands that is within' my selected category.
My categories contains a categoryID which is the value of the in the first dropdown - The brands is having it's own ID, but also having the categoryID, from the category they're in.
How would you do this? - And can you give an example, it might need javascript or jquery, which is fine with me.
This is my code:
<form action="deleteBrand.php" method="post">
<fieldset class="delete">
<legend>Delete Brand</legend>
<div>
<label for="categoryid">Category:<br />
<?php
$stmt = $db->prepare("SELECT categoryid, category FROM categories");
$stmt->execute();
echo "<select name='categoryid' onchange='this.form.submit()'>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='{$row['categoryid']}'>{$row['category']}</option>";
}
echo "</select>";
?>
</label>
</div>
<div>
<label for="brand">Brand Name:<br />
<?php
$stmt = $db->prepare("SELECT brands.id, brand FROM brands WHERE brands.categoryid = ")
?>
</label>
</div>
<div>
<input type="submit" value="Delete Brand" />
</div>
</fieldset>
</form>
I'm still missing the last dropdown, but if I just can get the value from the selected item from the first dropdown, into a variable, it shouldn't be hard for the rest of the code!
Thank you a lot
The below code is an example of how you can get the selected option value for a dropdown box. Depending on your needs, you can either preload your category id->brand mappings into a javascript variable that you can refer to, or use an ajax request to get the appropriate brands for your second combo box on demand, when the box is clicked.
<script>
function getSelected(obj)
{
alert(obj.options[obj.selectedIndex].value);
// Now use AJAX or preloaded variable to figure out
// what brands map to this category, and populate
// your second combobox
}
</script>
<form action="deleteBrand.php" method="post">
<label for="categoryid">Category:<br />
<select name='categoryid' onchange=getSelected(this)>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
</form>

Categories