Displaying value in textbox for to edit using ajax and php - php

editModal.php
<div id="edit-modal" class="modal fade" tabindex="-1" role="dialog" aria- labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">
<i class="glyphicon glyphicon-edit"></i> Edit Profile
</h4>
</div>
<div class="modal-body">
<div id="modal-loader" style="display: none; text-align: center;">
</div>
<div id="dynamic-content">
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th>First Name</th>
<td id="txt_first">
<input id="result_table" type="text" class="form-control" name="Firstname" ng-model="name">
</td>
</tr>
<tr>
<th>Middle Name</th>
<td id="txt_middle" > </td>
</tr>
<tr>
<th>Last Name</th>
<td id="txt_last"></td>
</tr>
<tr>
<th>Email Add</th>
<td id="txt_emailadd"></td>
</tr>
<tr>
<th>Contact Number</th>
<td id="txt_cnumber"></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" value="send" name="submit" class="btn btn-primary">Save Changes</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div><!-- /.modal -->
</div>
$(document).on('click', '#getUser', function(e){
e.preventDefault();
var uid = $(this).data('id'); // get id of clicked row
$('#dynamic-content').hide(); // hide dive for loader
$('#modal-loader').show(); // load ajax loader
$.ajax({
url: '../model/editUser.php',
type: 'POST',
data: 'id='+uid,
dataType: 'json'
})
.done(function(data){
console.log(data);
$('#dynamic-content').hide(); // hide dynamic div
$('#dynamic-content').show(); // show dynamic div
$('#txt_first').html(data.First_Name);
$('#txt_middle').html(data.Middle_Name);
$('#txt_last').html(data.Last_Name);
$('#txt_emailadd').html(data.Email_Add);
$('#txt_cnumber').html(data.Contact_Number);
$('#modal-loader').hide(); // hide ajax loader
})
.fail(function(){
$('.modal-body').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
});
});
});
</script>
I wanted to display the value of the $('#txt_first').html(data.First_Name); on the input type="text" value = "php echo $firstName " But it seems that I'm doing it wrong. Is it possible for me to enclosed the table and the script in a ; segment?

It is input field so you need to do it with 'value' not 'html'. try this:
$('#txt_first').val(data.First_Name);

Created demo to display data.First_Name in input type ="text"
$('#result_table').val("test");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered">
<tr>
<th>First Name</th>
<td id="txt_first">
<input id="result_table" type="text" class="form-control" name="Firstname" ng-model="name">
</td>
</tr>
<tr>
<th>Middle Name</th>
<td id="txt_middle" > </td>
</tr>
<tr>
<th>Last Name</th>
<td id="txt_last"></td>
</tr>
<tr>
<th>Email Add</th>
<td id="txt_emailadd"></td>
</tr>
<tr>
<th>Contact Number</th>
<td id="txt_cnumber"></td>
</tr>
</table>

Related

How to stop foreach to repeat content?

I don't know PHP, so this might be a very basic question. I apologize in advance.
I'm working on a page that has a list of products and for each product, there is an option to delete it. The backend dev made it so it works great. However, they're not available at the moment and the client wants a modal that checks with the user if they're sure they want to delete a product before the action is complete.
I've included a modal. And just moved the delete action to the modal's 'Yes' button. However, because we have several products listed, it keeps repeating the button over and over. I realize this might be a basic fix, but I have no idea what I'm doing. Can someone please just take a look? Thank you!
Here's the (edited - I moved the action to the modal in this version) code:
<div class="container">
<div class="quotes">
<div class="col-md-12">
<div class="pages-header text-right">
New Product
</div>
</div>
<div class="col-md-12">
<div class="card rounded-0 border-0 active-products">
<div class="card-header d-flex justify-content-between align-items-center">
<h3 class="mb-0">Products</h3>
</div>
<table class="table text-center" cellspacing="0">
<thead>
<tr>
<th scope="col" class="text-left quote-name">Name</th>
<th scope="col">Category</th>
<th scope="col">Hardware Price</th>
<th scope="col">Yearly License Cost</th>
<th scope="col">Monthly License Cost</th>
<th scope="col">Type</th>
<th scope="col" colspan="2"></th>
</tr>
</thead>
<tbody>
#foreach($products as $product)
<tr>
<th scope="col" class="quote-name">
{{$product->name}}
</th>
<th scope="col">
{{$product->category}}
</th>
<th scope="col">
{{$product->hardware_price}}
</th>
<th scope="col">
{{$product->yearly_license_cost}}
</th>
<th scope="col">
{{$product->monthly_license_cost}}
</th>
<th scope="col">
{{$product->type}}
</th>
<th scope="col" class="actions-col">
Edit
</th>
<th scope="col" class="actions-col">
Delete
</th>
</tr>
#endforeach
</tbody>
</table>
{{ $products->links() }}
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
#foreach($products as $product)
<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
<form method="POST" action="{{route('product.destroy', $product)}}" id="delete-{{$product->id}}">
#csrf
#method('DELETE')
Delete
</form>
#endforeach
</div>
</div>
</div>
</div>
</div>
How do I tweak this?
You need to use pass data attr in delete button and append into modal form using js script.
Change into HTML
Delete
Add JS Script
$(document).on('click', '.deleteproduct', function(e) {
e.preventDefault();
var id = $(this).attr('data-id');
var product = $(this).attr('data-product');
$('#Deletefrm').attr('id','delete-'+id);
$('#Deletefrm').attr('action','/product.destroy/'+product);
});
$('#deleteModal').on('hidden.bs.modal', function(e) {
$('#Deletefrm').attr('id','');
$('#Deletefrm').attr('action','');
});
Change in Modal
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
<form method="POST" id="Deletefrm" action="" id="">
#csrf
#method('DELETE')
Delete
</form>
</div>
<div class="container">
<div class="quotes">
<div class="col-md-12">
<div class="pages-header text-right">
New Product
</div>
</div>
<div class="col-md-12">
<div class="card rounded-0 border-0 active-products">
<div class="card-header d-flex justify-content-between align-items-center">
<h3 class="mb-0">Products</h3>
</div>
<table class="table text-center" cellspacing="0">
<thead>
<tr>
<th scope="col" class="text-left quote-name">Name</th>
<th scope="col">Category</th>
<th scope="col">Hardware Price</th>
<th scope="col">Yearly License Cost</th>
<th scope="col">Monthly License Cost</th>
<th scope="col">Type</th>
<th scope="col" colspan="2"></th>
</tr>
</thead>
<tbody>
#foreach($products as $product)
<tr>
<td scope="col" class="quote-name">
{{$product->name}}
</td>
<td scope="col">
{{$product->category}}
</td>
<td scope="col">
{{$product->hardware_price}}
</td>
<td scope="col">
{{$product->yearly_license_cost}}
</td>
<td scope="col">
{{$product->monthly_license_cost}}
</td>
<td scope="col">
{{$product->type}}
</td>
<td scope="col" class="actions-col">
Edit
</td>
<td scope="col" class="actions-col">
Delete
</td>
<td>
<div class="modal fade" id="deleteModal-{{$product->id}}" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel-{{$product->id}}" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel-{{$product->id}}">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
#foreach($products as $product)
<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
<form method="POST" action="{{route('product.destroy', $product)}}" id="delete-{{$product->id}}">
#csrf
#method('DELETE')
Delete
</form>
#endforeach
</div>
</div>
</div>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
{{ $products->links() }}
</div>
</div>
</div>
<!-- Modal -->
</div>

Laravel: Displaying the list with same URL when clicking the html button

I'm facing an issue, please see the below screenshot.
Screenshot
In the screenshot, you can see its image upload page. By default, the Add Image button and the table which showing the lists should be display. The concept that I want to implement is when I click the Add Image button, the table lists should be hidden and have to show the image upload section. This all should be happen in the same URL.
Below is the code:
Route:
Route::post('/imageupload', 'Admin\ImageController#imageUploadPost')->name('image.upload.post');
Blade File:
<div class="panel panel-container">
<button type="button" class="btn btn-primary">Add Image</button>
</div>
<div class="panel panel-container">
<table class="table table-striped" id="slideshow">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
</div>
<div class="panel panel-container hide-img-upload">
<form action="{{ route('image.upload.post') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-6">
<input type="file" name="image" class="form-control">
</div>
<div class="col-md-6">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</form>
</div>
</div>
Controller:
public function imageUploadPost()
{
return back()
->with('success','You have successfully upload image.')
->with('image',$imageName);
}
Since, I'm new to Laravel, I couldn't able to see the solution.
Any help will be appreciated.
Try this
<div class="panel panel-container">
<button type="button" class="btn btn-primary"data-toggle="modal"data-target="#myModal">Add Image</button>
</div>
<div class="panel panel-container">
<table class="table table-striped" id="slideshow">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
</div>
<div class="modal" id="myModal" role="dialog"
aria-labelledby="myModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Upload Image </h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="container"></div>
<div class="panel panel-container hide-img-upload">
<form action="{{ route('image.upload.post') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-6">
<input type="file" name="image" class="form-control">
</div>
<div class="col-md-6">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</form>
</div>
</div>
</form>
</div>
</div>
or use javascript hidden show for this
this is more about html and javascript staff.
You can use this as hint.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<button onclick="alter()">Alter</button>
<div id="first">First</div>
<div id="sec">Second</div>
<script>
document.getElementById('sec').style.visibility = 'hidden';
function alter(){
if(document.getElementById('sec').style.visibility == 'hidden'){
document.getElementById('sec').style.visibility = 'visible';
document.getElementById('first').style.visibility = 'hidden';
}else{
document.getElementById('sec').style.visibility = 'hidden';
document.getElementById('first').style.visibility = 'visible';
}
}
</script>
</body>
</html>
Firstly define id for file input;
<input type="file" name="image" id="file_input" class="form-control">
After define id for list scope;
<div class="panel panel-container" id="list_scope">
Finally add JavaScript codes;
<script>
document.getElementById('file_input').onclick = function() {
document.getElementById('list_scope').style.display = "none";
};
</script>

use span to hide and show content

i am new in both php and jquery. i want to use span to hide and show content in html and php. can i use jquery? if yes, what should i do? i already put the jquery script to the bottom of page. this is my code :
<!-- reply -->
<span title="fas fa-reply" name="reply" data-id="1"
</span>
<!-- if span clicked show all these data... -->
<table class="table table-borderless" style="margin-top:50px;">
<thead class="bg-secondary pb-0">
<tr>
<th colspan="3" class="pb-0">
<p class="test text-light">Creating Reply <?php echo $rowAllPost["id"]; ?>
</p></th>
<th class="pb-0 text-right">
</th>
</tr>
</thead>
<th scope="row" style="width:15rem!important;">
<img src="<?php echo $rowViewUserNow["picture"];?>" class="rounded-circle" style=height:93px;width:81px;><br>
<p class="ml-3"> Online</p>
<p class="ml-2"> <?php echo $rowViewUserNow["username"];?>
</p>
<i class="fas fa-info-circle pr-2 pt-2"></i>Active
<td colspan="3" class="text-justify">
<form action="controller/doCheckPost.php" method="post">
<textarea name="description" class="form-control"></textarea>
</td>
</tr>
<tr class="bg-secondary pb-0" >
<th colspan="3" class="pb-0">
<button title="Cancel" class="btn" type="submit" name="cancel"><i class="fas fa-times"></i></th></button>
<th class= "pb-0 text-right">
<button title="Submit" class="btn" name="submit"><i class="fas fa-check"></i></button>
</form>
</th>
</th>
</tr>
</div>
</tbody>
</table>
The best way would be to first give an id to both the span and table elements. Then you can use an event handler in jQuery like this:
$('#spanId').on('click', function() {
$('#tableId').toggle();
});
Toggle will show the element if it's hidden and hide it if it's visible.

No data return in Modal when some row values are null

I am working on a project, where I have created a catalog for company. On some pages when user clicks on product id, a modal displays product detail. The modal works fine when there are values in rows. However, when there are null values, the modal comes back empty. How do I handle the code so that even if there are null values in some rows, data will still be displayed? Or is there an easier way to show show product detail without using modal/ajax/json.
MYSQL Query
if($_REQUEST['tid']) {
SELECT t.tool_id, power_source, sub_option, sub_type,
(purchase_price * 0.15) AS rental_price, (purchase_price * 0.4) AS
deposit_price, material,manufacturer, length, width_diameter, weight,
ac.accessory_description FROM tool t JOIN accessory ac on t.tool_id =
ac.tool_id WHERE tool_id='".$_REQUEST['tid']."'";
$resultset = mysqli_query($connection, $sql) or die("database error:".
mysqli_error($connection));
$data = array();
while( $rows = mysqli_fetch_assoc($resultset) ) {
$data = $rows;
}
echo json_encode($data);
} else {
echo 0;
}
?>
Ajax/json code
$(document).ready(function(){$(document).on
('click', '#getTool', function(e){ e.preventDefault();
var tid = $(this).data('id');
$('#tool-detail').hide();
$('#tool_data-loader').show();
$.ajax({url: 'toolData.php',
type: 'POST',
data: 'tid='+tid,
dataType: 'json',
cache: false
})
.done(function(data){
console.log(data.id);
$('#tool-detail').hide();
$('#tool-detail').show();
$('#tid').html(data.tool_id);
$('#tool_type').html(data.power_source);
$('#short_desc').html(data.sub_option + " " +data.sub_type);
$('#fullDesc').html(data.manufacturer+ " "+data.material+ ",
"+data.length+"in x " +data.width_diameter+"in,"+data.weight+"lbs");
$('#rental_price').html("$"+data.rental_price);
$('#deposit_price').html("$"+data.deposit_price);
$('#accessories').html("$"+data.accessory_description);
$('#tool_data-loader').hide();
})
.fail(function(){
$('#tool-detail').html('Error, Please try again...');
$('#tool_data-loader').hide();
});
});
});
Modal
<form action="productDetail.php" method="post">
<table>
<tr>
<td><input type="submit" name="productDetail" value="productDetail" id="productDetail" style="margin-left: 10px;"></td>
</tr>
</table>
</form>
<div id="tool-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">*</button>
<h4 class="modal-title">
<i class="glyphicon glyphicon-wrench"></i> Tool Details
</h4>
</div>
<div class="modal-body">
<div id="tool_data-loader" style="display: none; text-align: center;">
<img src="ajax-loader.gif">
</div>
<div id="tool-detail">
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-striped table-bordered">
<tr>
<th>Tool ID</th>
<td id="tid"></td>
</tr>
<tr>
<th>Tool Type</th>
<td id="tool_type"></td>
</tr>
<tr>
<th>Short Description</th>
<td id="short_desc"></td>
</tr>
<tr>
<th>Full Description</th>
<td id="fullDesc"></td>
</tr>
<tr>
<th>Rental Price</th>
<td id="rental_price"></td>
</tr>
<tr>
<th>Deposit Price</th>
<td id="deposit_price"></td>
</tr>
<tr>
<th>Accessories</th>
<td id="accesories"></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

Retrieve multiple data from 2 tables with php and jquery

This code is retrieving multiple data from 1 table only.
How can I retrieve multiple data from 2 tables and save them to another table?
Result that I want to achieve:
Here are the tables
request:
prNo branch
pr03 odessa
pr04 kiev
detail_request:
prNo productCode productName qty
pr03 111 soap 1200
pr03 112 tooth paste 1000
I want to save data on detail_request table to purchase table, but only the data that have check mark on the checkbox, and I'm adding price column to be fill manually.
purchase:
prNo productCode productName qty price
- - - - -
Here is the code:
<html>
<head>
<title>Lookup Modal Bootstrap 3</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css"/>
<link rel="stylesheet" href="datatables/dataTables.bootstrap.css"/>
<style>
body{
margin: 15px;
}
.pick:hover{
cursor: pointer;
}
</style>
</head>
<body>
<div class="row">
<div class="col-md-5">
<h2> </h2>
</div>
</div>
<form action="action" onsubmit="dummy();
return false">
<div class="form-group">
<label for="varchar">Request Number</label>
<div class="row">
<div class="col-md-4">
<input type="text" class="form-control" name="prNo" id="prNo" placeholder="Request Number" readonly />
<strong>Branch Name</strong><br>
<input type="text" class="form-control" name="branch" id="branch" placeholder="branch " readonly />
</div>
<div class="col-md-2">
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">. . .</button>
</div>
</div>
</div>
<table width="446" border="1">
<tr>
<th scope="row"> </th>
<th scope="row">Request Number</th>
<td><strong>Product Code</strong></td>
<td><strong>Product Name</strong></td>
<td><strong>QTY</strong></td>
<td><strong>Price</strong></td>
</tr>
<tr>
<th scope="row"><input type="checkbox" name="prNo" id="prNo"></th>
<th scope="row"><label for="Request Number"></label>
<input type="text" name="prNo" id="prNo"></th>
<td><label for="productCode"></label>
<input type="text" name="productCode" id="productCode"></td>
<td><label for="productName"></label>
<input type="text" name="productName" id="productName"></td>
<td><label for="qty"></label>
<input type="text" name="qty" id="qty"></td>
<td><input type="text" name="price" id="price"></td>
</tr>
<tr>
<th scope="row"><input type="checkbox" name="prNo4" id="prNo4"></th>
<th scope="row"><input type="text" name="prNo2" id="prNo2"></th>
<td><input type="text" name="productCode2" id="productCode2"></td>
<td><input type="text" name="productName2" id="productName2"></td>
<td><input type="text" name="qty2" id="qty2"></td>
<td><input type="text" name="price2" id="price2"></td>
</tr>
</table>
<input type="submit" value="Save" class="btn btn-primary" />
</form>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" style="width:800px">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Lookup </h4>
</div>
<div class="modal-body">
<table id="lookup" class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Request Number</th>
<th>Branch Name</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect('localhost', 'root', '', 'purchase');
$sql = mysqli_query($con,'select * from request ');
while ($r = mysqli_fetch_array($sql)) {
?>
<tr class="pick" no="<?php echo $r['prNo']; ?>", branch="<?php echo $r['branch'] ?>", code="<?php echo $r['productCode'] ?>">
<td><?php echo $r['prNo']; ?></td>
<td><?php echo $r['branch']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="js/jquery-1.11.2.min.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>
<script src="datatables/jquery.dataTables.js"></script>
<script src="datatables/dataTables.bootstrap.js"></script>
<script type="text/javascript">
$(document).on('click', '.pick', function (e) {
document.getElementById("prNo").value = $(this).attr('no');
document.getElementById("branch").value = $(this).attr('branch');
$('#myModal').modal('hide');
});
$(function () {
$("#lookup").dataTable();
});
</script>
</body>
</html>
<?php
if(isset($_POST['product_submit']))
{
$check=$_POST['check'];
foreach($check as $i)
{
$prno=$_POST['prNo'.$i];
$prcode=$_POST['productCode'.$i];
$prname=$_POST['productName'.$i];
$qty=$_POST['qty'.$i];
$price=$_POST['price'.$i];
$query = mysqli_query($con,"insert into purchase (prNo,productCode,productName,qty,price) value ('$prno', '$prcode', '$prname', '$qty', '$price')");
}
if($query)
{
?>
<script>
alert("success");
</script>
<?php
}
}
?>
<html>
<head>
<title>Lookup Modal Bootstrap 3</title>
</head>
<body>
<div class="row">
<div class="col-md-5">
<h2> </h2>
</div>
</div>
<form method="post" id="my_form">
<div class="form-group">
<label for="varchar">Request Number</label>
<div class="row">
<div class="col-md-4">
<input type="text" class="form-control" name="prNo" id="prNo" placeholder="Request Number" readonly />
<strong>Branch Name</strong><br>
<input type="text" class="form-control" name="branch" id="branch" placeholder="branch " readonly />
</div>
<div class="col-md-2">
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">. . .</button>
<button type="button" class="btn btn-default" onclick="show_fun()">View</button>
</div>
</div>
</div>
<table width="446" border="1">
<thead>
<tr>
<th scope="row"> </th>
<th scope="row">Request Number</th>
<td><strong>Product Code</strong></td>
<td><strong>Product Name</strong></td>
<td><strong>QTY</strong></td>
<td><strong>Price</strong></td>
<td><strong>Total</strong></td>
</tr>
</thead>
<tbody id="product_table">
</tbody>
</table>
<input type="submit" value="Save" name="product_submit" class="btn btn-primary" />
</form>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" style="width:800px">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Lookup </h4>
</div>
<div class="modal-body">
<table id="lookup" class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Request Number</th>
<th>Branch Name</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect('localhost', 'root', '', 'purchase');
$sql = mysqli_query($con,'select * from request ');
while ($r = mysqli_fetch_array($sql)) {
?>
<tr class="pick" no="<?php echo $r['prNo']; ?>", branch="<?php echo $r['branch'] ?>", code="<?php echo $r['productCode'] ?>">
<td><?php echo $r['prNo']; ?></td>
<td><?php echo $r['branch']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="js/jquery-1.11.2.min.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>
<script src="datatables/jquery.dataTables.js"></script>
<script src="datatables/dataTables.bootstrap.js"></script>
<script type="text/javascript">
$(document).on('click', '.pick', function (e) {
document.getElementById("prNo").value = $(this).attr('no');
document.getElementById("branch").value = $(this).attr('branch');
var no=$(this).attr('no');
$.ajax({
type:"post",
data:"&product_id=1&prno="+no,
url:"ajax.php",
success:function(result)
{
$("#product_table").html(result);
}
});
$('#myModal').modal('hide');
});
$(function () {
$("#lookup").dataTable();
});
function calc_fun(i){
var qty=$("#qty"+i).val();
var price=$("#price"+i).val();
var total=Number(qty)*Number(price);
$("#total"+i).val(total);
}
</script>
</body>
</html>
ajax.php
<?php
if(isset($_POST['product_id']))
{
$prno=$_POST['prno'];
$i=1;
$sql = mysqli_query($con,"select * from detail_request where prNo='$prno'");
while ($r = mysqli_fetch_array($sql)) {
echo '<tr>
<th scope="row"><input type="checkbox" name="check[]" id="check'.$i.'" value="'.$i.'"></th>
<th scope="row"><label for="Request Number"></label>
<input type="text" name="prNo'.$i.'" id="prNo'.$i.'" readonly value="'.$r["prNo"].'"></th>
<td><label for="productCode"></label>
<input type="text" name="productCode'.$i.'" id="productCode'.$i.'" readonly value="'.$r["productCode"].'"></td>
<td><label for="productName"></label>
<input type="text" name="productName'.$i.'" id="productName'.$i.'" readonly value="'.$r["productName"].'"></td>
<td><label for="qty"></label>
<input type="text" name="qty'.$i.'" id="qty'.$i.'" onchange="calc_fun('.$i.')" readonly value="'.$r["qty"].'"></td>
<td><input type="text" name="price'.$i.'" onchange="calc_fun('.$i.')" id="price'.$i.'"></td>
<td><input type="text" name="total'.$i.'" id="total'.$i.'"></td>
</tr>';
$i++;
}
}
?>

Categories