What Im trying to do is select item name where is ID and if item selected it automatically adds that item serial number to serial number input.
I think it should be used ajax maybe but I dont know how exactly.
Item Name:<br />
<div class="select-holder">
<i class="fa fa-caret-down"></i>
<?php
if($_items->count_items() == 0)
echo '<select name="item-name" disabled><option value="no">You need to add a item first</option></select>';
else{
echo '<select name="item-name" id="change-item">';
$items = $_items->get_items_dropdown();
while($item = $items->fetch_object()) {
echo "<option value=\"{$item->id}\">{$item->name} - {$item->serialnumber}</option>";
}
echo '</select>';
}
?>
</div>
Serial Number:<br />
<div class="ni-cont">
<input type="text" name="item-serialnumber" class="ni" disabled/>
</div>
I have also other inputs like description, price and etc.. to fetch these inputs with these values.
example: i need get data to my field from select option id (value)
<input type="text" name="item-serialnumber" value="<?php echo $_items->get_item_serialnumber($id); ?>" class="ni" disabled/>
<input type="text" name="item-serialnumber" value="<?php echo $_items->get_item_description($id); ?>" class="ni" disabled/>
That value is already shown inside your select-box you can simply use onchange event and then use $(this).find("option:selected").text().split('-')[1] this will give serial no then put it inside your input-box using $('[name=item-serialnumber]').val(somevalue) .Also ,you can send id via ajax to your server page and fetch result.
Demo Code :
$("#change-item").on("change", function() {
var value = $(this).find("option:selected").text().trim().split('-')[1]
$("[name='item-serialnumber']").val(value)
var id = $(this).val();
//use ajax :
$.ajax({
url: "somepgaename.php", //the page containing php script
type: "post", //request type,
dataType: 'json',
data: {
id: id //send id as well
},
success: function(data) {
console.log(data.yourfieldname);
//put inside your input fields values like :
// $("[name='item-serialnumber']").val(data.serialno)
}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Item Name:<br />
<div class="select-holder">
<i class="fa fa-caret-down"></i>
<select name="item-name" id="change-item">
<option value="1">Abc-123</option>
<option value="2">Abc2-122</option>
<option value="3">Abc2-132</option>
</select>
</div>
Serial Number:<br />
<div class="ni-cont">
<input type="text" name="item-serialnumber" class="ni" disabled/>
</div>
Then , at your server end get the id pass via ajax using $_POST['id'] then fetch result using select query and send them back to ajax using echo json_encode($somearray); i.e:
$id = $_POST['id'];
$stmt = $pdo->prepare("SELECT * FROM tablename WHERE id=:id");
$stmt->execute(['id' => $id]);
//put fetch result inside array..send back to ajax
//echo json_encode($somevalue);
Related
whenever i select an option in my select box it automatically display the value in the text box in the create.php
here is the code of the create.php
<div class="form-group">
<label for="sub_category">Category:</label>
<select class="form-control select_group" id="sub_category_1" name="sub_category[]" onchange="getSubCategoryData(1)">
<option value=""></option>
<?php foreach ($sub_category as $k => $v): ?>
<option value="<?php echo $v['id'] ?>"><?php echo $v['name'] ?></option>
<?php endforeach ?>
</select>
</div>
<div class="form-group">
<label for="markup">Markup:</label>
<input type="text" class="form-control" id="markup" name="markup" disabled autocomplete="off" placeholder="Markup">
<input type="hidden" class="form-control" id="markup_value" name="markup_value" autocomplete="off">
</div>
then it's working fine just like i wanted to.
as you can see in this photo i the category is a select box and whenever i select an option the markup will be displayed based on the category data
script in create.php
function getSubCategoryData(row_id)
{
var sub_category_id = $("#sub_category_"+row_id).val();
if(sub_category_id == "") {
$("#markup").val("");
$("#markup_value").val("");
} else {
$.ajax({
url: base_url + 'products/getSubcatById',
type: 'post',
data: {sub_category_id : sub_category_id},
dataType: 'json',
success:function(response) {
$("#markup").val(response.markup);
$("#markup_value").val(response.markup);
}
});
}
}
bet when it comes to editing in a scenario of i want to change the category because i input an mistake data it does not process, in my edit.php wherein i can edit the fields in this module i can edit the name the cost and etc.. but i cannot edit the category whenever i try to change the category to others it does not allow me to click it and the markup value stays what value i have in the early data entry
example is that i have 2 category 1 is starter and 2 is drinks my first category has a markup of 50 and the second one is 60 so whenever i data entry an data and i want to change it's markup it does not allow me to change the category and my markup and i can't seem to know what is my problem here.
here is my edit.php code
<div class="form-group">
<label for="sub_category">Category:</label>
<?php $sub_category_data = json_decode($product_data['sub_category_id']); ?>
<select class="form-control select_group" id="sub_category_1" name="sub_category[]" onchange="getSubCategoryData(1)">
<?php foreach ($sub_category as $k => $v): ?>
<option value="<?php echo $v['id'] ?>" <?php if(in_array($v['id'], $sub_category_data)) { echo 'selected="selected"'; } ?>><?php echo $v['name'] ?></option>
<?php endforeach ?>
</select>
</div>
<div class="form-group">
<label for="markup">Markup:</label>
<input type="text" class="form-control" id="markup" name="markup" disabled
value="<?php echo !empty($this->input->post('markup_value')) ?:$product_data['markup'] ?>"
autocomplete="off" placeholder="Markup">
<input type="hidden" class="form-control" id="markup_value" name="markup_value"
value="<?php echo !empty($this->input->post('markup_value')) ?:$product_data['markup'] ?>"
autocomplete="off">
</div>
and the ajax or script in the edit.php:
function getSubCategoryData(row_id)
{
var sub_category_id = $("#sub_category_"+row_id).val();
if(sub_category_id == "") {
$("#markup").val("");
$("#markup_value").val("");
} else {
$.ajax({
url: base_url + 'products/getSubcatById',
type: 'post',
data: {sub_category_id : sub_category_id},
dataType: 'json',
success:function(response) {
$("#markup").val(response.markup);
$("#markup_value").val(response.markup);
}
});
}
}
and yes i can see the data input earlier if i input an category 1 with a markup with 50 it does save in the field but whenever i edit it and change the category i cannot click on the select box and the markup does not change.
Please watch this video:
https://drive.google.com/file/d/1lFNCyxoFegyTJNoULrSs59ZsYa_rjNs3/view?usp=sharing
i'm having a hard time clicking the category when i tried to change and the markup does not change
in case the video does not work here i uploaded it in yt
https://youtu.be/52SYwU8y30A
Your base_url should be declared as javascript variable. You can assign PHP base_url() to base_url variable like this
var base_url = "<?php echo base_url() ?>;
url: base_url + "products/getSubcatById",
OR you can directly add like this too without declaring javascript variable
url: "<?php echo base_url(); ?>products/getSubcatById",
I have done searching this question here but there is no suitable answer for my problem. So to be more specific, I have 2 select option which is floor and room. Both selection displayed based on database. So is there a syntax that can get the selected floor id so I can do my query to display the specific room for the selected floor before submit? Here's my code :
<form name="form" method="POST">
<div class="form-group">
<div><label>Floor Name</label></div>
<select class="form-control" id="floorid" name="existfloorname">
<option>None</option>
<?php
$result = $conn->query("select * from floors");
while ($row = $result->fetch_assoc())
{
?><option id="<?php echo $row['floorid'];?>" value="<?php echo $row['floorname']; ?>"><?php echo $row['floorname']; ?></option><?php
}
?>
</select>
</div>
<div class="form-group">
<div><label>Room Name</label></div>
<select class="form-control" name="existroomname">
<option>None</option>
<?php
$result = $conn->query("select * from rooms where floorid = '".GETSELECTEDFLOORID."'");
while ($row = $result->fetch_assoc())
{
?><option value="<?php echo $row['roomname']; ?>"><?php echo $row['roomname']; ?></option><?php
}
?>
</select>
</div>
<input type="submit" name="delete" value="Delete">
</form>
This is how the form looks like
You need to work with AJAX for this. AJAX allows the sending of data from the front(JS) to the back(PHP). JQuery makes this easy by providing you with the .ajax() method. This is how you need to do this:
Note: You will need to use jQuery to do it this way
First, re-arrange your HTML in this manner, notice the select ids:
<form name="form" method="POST">
<div class="form-group">
<div><label>Floor Name</label></div>
<select class="form-control" id="floor_select" name="existfloorname">
<option>None</option>
<?php
$floors = $conn->query("select * from floors");
while ($row = $floors->fetch_assoc()){ ?>
<option value="<?= $row['floorid']; ?>"><?= $row['floorname']; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<div><label>Room Name</label></div>
<select class="form-control" id="room_select" name="existroomname">
<option>None</option>
</select>
</div>
<input type="submit" name="delete" value="Delete">
</form>
Second, create your jQuery ajax call:
$(document).on("change", 'select#floor_select', function(e) {
var floor_id = $(this).val();
$.ajax({
type: "POST",
data: {floor_id: floor_id},
url: 'get_room_list.php',
dataType: 'json',
success: function(json) {
var $el = $("select#room_select");
$el.empty(); // remove old options
$el.append("<option>Please Select</option>");
//iterate through your results and add to your dropdown
$.each(json, function(k, v) {
$el.append("<option value='" + v.id + "'>" + v.roomname + "</option>");
});
}
});
});
Third, create the PHP script that the AJAX call above is looking for.
Call it get_room_list.php:
<?php
$result = $conn->query("select * from rooms where floorid = '" . GETSELECTEDFLOORID . "'");
while ($row = $result->fetch_assoc()) {
$results[] = $row;
}
header('Content-Type: application/json');
echo json_encode($results);
Once all the above is done, you should be able to obtain the information you need. Every time the "Floor Name" select is changed, JS will send a POST request (via AJAX) to PHP and PHP will return the "rooms" result in JSON format, which you then iterate to create your dropdown.
Hope this all makes sense.
Good luck.
You may need AJAX to get list room each floor.
I have textarea and three checkboxs in my mysql db by default textarea value as null and checkboxs values as zero(0).
when i enter some text in textarea i'm updating text value in my mysql db but i gotta stuck in checkbox things can some one suggest how to update checkbox checked value in mysql db
say for example if checkbox is checked/clicked i should be able to update my mysql db with value '1' if not the value will be '0'
https://jsfiddle.net/07wmpjqf/1/
db structure
ID TEXT ABC XYZ LMN
1 NULL 0 0 0
Thanks!
html
<div>
<textarea class="lb_text" rows="6" cols="50" placeholder="Add text here..."></textarea>
</div>
<div>
<label>
<input type='checkbox'>ABC
</label>
</div>
<div>
<label>
<input type='checkbox'>XYZ
</label>
</div>
<div>
<label>
<input type='checkbox'>LMN
</label>
</div>
<div>
<input type="submit" class="lb_save" value="submit">
</div>
php
if(isset($_POST['lb_text']))
{
$live_blog = mysqli_real_escape_string($_POST['lb_text']);
$sql = "update demo set text='".$live_blog."'";
$result = mysqli_query($con, $sql);
}
}
jquery
$(function(){
$(".lb_save").click(function(){
var lb_text = $('.lb_text').val();
if(lb_text == '')
{
alert("Enter Some Text...");
$('.lb_text').focus();
}
else
{
$.ajax({
type: "POST",
url: "index.php",
data:{
lb_text:lb_text,
},
success:function(response){
alert('successfully updated');
}
});
}
return false;
});
});
add id to your check box and capture th value in jquery.
<div>
<label>
<input type='checkbox' id="abc">ABC
</label>
</div>
<div>
<label>
<input type='checkbox' id="xyz">XYZ
</label>
</div>
<div>
<label>
<input type='checkbox' id="lmn">LMN
</label>
</div>
and change your ajax data like this,
data: {
lb_text: lb_text,
abc: $("#abc").is(':checked') ? 1 : 0,
xyz: $("#xyz").is(':checked') ? 1 : 0,
lmn: $("#lmn").is(':checked') ? 1 : 0,
},
and your query like this,
$live_blog = mysqli_real_escape_string($_POST['lb_text']);
$abc = $_POST['abc']
$xyz = $_POST['xyz']
$lmn = $_POST['lmn']
$sql = "update demo set text='".$live_blog."',ABC='".$abc."',XYZ='".$xyz."',LMN='".$lmn."'";
I have a list of radio tags with a diffrent id(post-1,post2 etc).
I am trying to preforme get request with the specific post id by clickign the radio.
This is the html:
$("#pro").click(function(){
var post = $(this).val();
alert(post);
$.get("<?php echo get_template_directory_uri(); ?>/jquery.php",{type: 'post', userid :'<?php echo get_current_user_id();?>', postid: '<?php echo $post->ID; ?>', pro : "pro"},function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
<input type = "radio" value = "<?php echo $post->ID;?>" class="pro-radio" name="pro-<?php echo $post->ID;?>" id="pro" <?php echo $GLOBALS['lock']; ?> <?php echo $pro_select; ?>> <label for="pro" class="pro-radio" >pro</label>
and this is the jquery:
When I press one of them, all of the radios oprate the jquery.
Thank you.
I think you reverted name and ids,
try this html
<input type = "radio" value = "<?php echo $post->ID;?>" class="pro-radio" id="pro-<?php echo $post->ID;?>" name="pro" <?php echo $GLOBALS['lock']; ?> <?php echo $pro_select; ?>> <label for="pro" class="pro-radio" >pro</label>
try adding a different class for each radio button and select by class name
var post = $('.className').val();
alert(post);
I think your HTML is a bit weird. You use the same id but different name attributes for your radios. Assume, you have this HTML:
<input type="radio" name="pro" value="1" id="pro-1"> <label for="pro-1">pro 1</label>
<input type="radio" name="pro" value="2" id="pro-2"> <label for="pro-2">pro 2</label>
<input type="radio" name="pro" value="3" id="pro-3"> <label for="pro-3">pro 3</label>
Then use this JS to get the selected value:
$('input[name=pro]').change(function() {
if ($(this).is(':checked')) {
alert($(this).val());
}
});
Please note, that the radios are selected by their (identical) name, but not by the (different) id. Then you have to verify the current element is the selected one using .is(':checked') and finally I use change() instead of click() if the element is selected via keyboard input.
Check it out here: http://jsfiddle.net/seofu1t4/
Can anybody help me on this. For showing the rezults from an MySql database i have to select school, class, subject, and exam. But listing all the classes or all the exams is not very practical so i want to do another function that when i choose some schools in the first select box it shows me in the second select box only the classes of the selected schools.
My code is:
<div id="allselects">
<form action="viewing.php" method="post" name="filt">
<div class="multsarrange">
<h1 class="choosetext" >Chose Schools</h1>
<select class="multipleselect" name="schools[]" size="8" multiple="multiple" id="shkll">
<?php
$sql = "SELECT * FROM schools ";
$scc=mysql_query($sql);
while ($db_f = mysql_fetch_assoc($scc)) {
$schcd=$db_f['schoolcode'];
$schc=$db_f['schoolname'];
echo "<option value=$schcd >$schc</option>";
}
?>
</select>
</div>
<div class="multsarrange" id="clasaajax">
<h1 class="choosetext" >Chose an Classes</h1>
<select class="multipleselect" name="classes[]" size="8" multiple="multiple" ">
<?php
$c = "SELECT * FROM classes ";
$cl=mysql_query($c);
while ($db_f = mysql_fetch_assoc($cl)) {
$clsc=$db_f['schoolID'];
$claid=$db_f['classID'];
$clay=$db_f['year'];
$clanm=$db_f['className'];
$name=schoolidton($clsc)." ".$clay." ".$clanm;
echo "<option value=$claid >$name</option>";
}
?>
</select>
</div>
<div class="multsarrange">
<h1 class="choosetext" >Chose Subjects</h1>
<select class="multipleselect" name="subjects[]" size="8" multiple="multiple">
<?php
$sb = "SELECT * FROM subjects ";
$sbi=mysql_query($sb);
while ($db_f = mysql_fetch_assoc($sbi)) {
$sbnm=$db_f['subjectName'];
$sbid=$db_f['subjectID'];
echo "<option value=$sbid >$sbnm</option>";
}
?>
</select>
</div>
<div class="multsarrange">
<h1 class="choosetext" >Chose Exams</h1>
<select class="multipleselect" name="exams[]" size="8" multiple="multiple">
<?php
$e = "SELECT * FROM exams ";
$ex=mysql_query($e);
while ($db_f = mysql_fetch_assoc($ex)) {
$id=$db_f['examID'];
$sub=$db_f['subjectID'];
$desc=$db_f['description'];
$year=$db_f['year'];
$data=$db_f['data'];
$exnam=subidton($sub)." - ".$year." - ".$desc." - ".$data;
echo "<option value=$id >$exnam</option>";
}
?>
</select>
</div>
<div id="longsubmit">
</br></br>
<input name="submit" type="submit" value="View" />
</div>
</form>
</div>
What you need to do is the following :
Setup an event listener on the select to listen for the change event - see here
Process the change event by sending the selected value to the PHP script - see here
Using PHP get the selected value and query the database as required (you already do that bit)
Send the associated HTML output or JSON or XML if you just creating a new select list - this is a simple echo
Write the output to the screen using JavaScript - this is either creating a new element based on the reply from the PHP function or inserting the HTML response
There are lots of things within this process - each with multiple options - i suggest you attempt to tackle each one and come back with specific questions if you get stuck
use ajax on abc.php get values or ids of school and make the tags you need and return back the results to the relevant id of html
function get_options(table,id_field,name_field,where_field,field_value,select_id,edit_id){
$('#'+select_id).html('<option>Loading...</option>');
$.ajax({
type: "POST", url: "abc.php", data: "&table="+table+"&id_field="+id_field+"&name_field="+name_field+"&where_field="+where_field+"&field_value="+field_value+ "&edit_id=" + edit_id +"&get_option=1",
complete: function(data){
//alert(data.responseText);
$('#'+select_id).html(data.responseText);
}
});
}
You have to use AJAX inorder to achieve this.
check this
http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php