I'm trying to make two fields auto-fill after an item that generated from database been "selected". I'm not sure where did I made the mistake. I also used firebug but it doesn't show any error message. It just won't populate after I selected an item from dropdown menu. Please help me out and let me know where I did wrong.
Here is the script:
<script type="text/javascript" language="javascript">
$(function () {
$('#description').bind('input', function () {
$(this).val() // get value
$.ajax({
type: 'POST',
url: 'orderAuto.php',
data: {
url: $('#description').val()
},
dataType: 'json',
success: function (data) //on recieve of reply
{
var skuId = data[0];
var unitPrice = data[1];
$('#sku_1').val(skuId);
$('#uPrice_1').val(unitPrice);
}
});
});
});
</script>
Here is my form with fields and section from database:
<form name="form" method="get">
<table width="70%" border="5" align="center"><tr>
<th scope="row">Item Name</th>
<th scope="row">Item SKU</th>
<th scope="row">Quantity</th>
<th scope="row">Special Note</th>
<th scope="row">Unit Price</th>
<th scope="row">Total Price</th>
</tr>
<tr>
<th scope="row">
<?php
include('connect.php');
$result = mysqli_query("SELECT description FROM products")
or die(mysqli_error());
print '<select name="description" id="description" value="description">';
print '<option value="" disabled selected>Please Select A Product</option>';
while ($info = mysqli_fetch_array($result))
{
$p = $info["description"];
$p = htmlspecialchars($p);
printf('<option value="%s">%s</option>', $p, $p);
}
print '</select>';
?>
</th>
<th scope="row"><input name="sku_1" id="sku_1" readonly /></th>
<th scope="row"><input name="qty_1" /></th>
<th scope="row"><input name="note_1" /></th>
<th scope="row"><input name="uPrice_1" id="uPrice_1" readonly /></th>
<th scope="row"><input name="tPrice_1" readonly /></th>
</tr>
</table>
<input type="submit"/>
</form>
And here is orderAuto.php:
<?php
include('connect.php');
$p = $_POST['description'];
$result = mysqli_query("SELECT sku_id, unit_price FROM products WHERE description= '".$p."'");
$array = mysqli_fetch_array($result);
echo json_encode($array);
?>
Updates
<script type="text/javascript" language="javascript">
$(function () {
$('#description').change(function () {
$.ajax({
type: 'POST',
url: 'orderAuto.php',
data: {
description: $(this).val()
},
dataType: 'json',
success: function (data) //on recieve of reply
{
var skuId = data[0];
var unitPrice = data[1];
$('#sku_1').val(skuId);
$('#uPrice_1').val(unitPrice);
}
});
});
});
</script>
and
<?php
include('connect.php');
$p = mysqli_real_escape_string($_POST['description']); // should be doing this
$result = mysqli_query("SELECT sku_id, unit_price FROM products WHERE description= '".$p."'");
$array = mysqli_fetch_array($result);
echo json_encode($array);
?>
Related
I created a viewing module where users could view values from the database and I added an edit button, when you click the button, the modal should pop up with values based on the id.
Currently, this is what I'm getting when I click the edit button:
Now I'm still lacking one thing and it's the JavaScript which I already created:
<script>
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('whatever') // Extract info from data-* attributes
var modal = $(this);
var dataString = 'id=' + recipient;
$.ajax({
type: "GET",
url: "editdata.php",
data: dataString,
cache: false,
success: function (data) {
console.log(data);
modal.find('.dash').html(data);
},
error: function(err) {
console.log(err);
}
});
})
</script>
My fetch.php is purely PHP and I'm not sure how I would add the JS into it. Here's my fetch.php:
<?php
$connect = mysqli_connect("localhost", "root", "", "seatrequest");
$output = '';
$colors = array();
$colors["Ongoing"] = "red";
$colors["Closed"] = "#00FF00";
if(isset($_POST["query"]))
{
$search = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "
SELECT * FROM request
WHERE req_date LIKE '%".$search."%'
OR reqname LIKE '%".$search."%'
OR natureofreq LIKE '%".$search."%'
OR postitle LIKE '%".$search."%'
OR critlevel LIKE '%".$search."%'
OR deadline LIKE '%".$search."%'
OR account LIKE '%".$search."%'
OR newaccname LIKE '%".$search."%'
OR lob LIKE '%".$search."%'
OR site LIKE '%".$search."%'
OR status LIKE '%".$search."%'
";
}
else
{
$query = "
SELECT * FROM request ORDER BY reqnumber";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th style="background-color: #e6ecff;">Date Requested</th>
<th style="background-color: #e6ecff;">Requested By</th>
<th style="background-color: #e6ecff;">Nature of Request</th>
<th style="background-color: #e6ecff;">Position Title</th>
<th style="background-color: #e6ecff;">Critical Level</th>
<th style="background-color: #e6ecff;">Deadline</th>
<th style="background-color: #e6ecff;">Account</th>
<th style="background-color: #e6ecff;">Name of Account (For New Seat)</th>
<th style="background-color: #e6ecff;">LOB</th>
<th style="background-color: #e6ecff;">Site</th>
<th style="background-color: #e6ecff;">Status</th>
<th style="background-color: #e6ecff;">Action</th>
<th style="background-color: #e6ecff;">Edit</th>
</tr>';
while($row = mysqli_fetch_array($result))
{
$output .= '<tr>
<td>'.$row["req_date"].'</td>
<td>'.$row["reqname"].'</td>
<td>'.$row["natureofreq"].'</td>
<td>'.$row["postitle"].'</td>
<td>'.$row["critlevel"].'</td>
<td>'.$row["deadline"].'</td>
<td>'.$row["account"].'</td>
<td>'.$row["newaccname"].'</td>
<td>'.$row["lob"].'</td>
<td>'.$row["site"].'</td>
<td style="color:' . $colors[$row["status"]] . ';">' .$row["status"] . '</td>
<td>
<form method="post" action="update-work-status.php">
<input type="hidden" name="reqnumber" value="'.$row['reqnumber'].'" />
<button class="fa fa-check" style="color: green" type="submit" name="approve" value=""></button><button class="fa fa-close" style="color: red" type="submit" name="decline" value=""></button>
</form>
</td>
<td><a class="btn btn-small btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="'.$row['reqnumber'].' ">Edit</a></td>
</tr>
';
}
echo $output;
}
else
{
echo 'Data Not Found';
}
?>
I guess my question is how would I incorporate that JS inside fetch.php? I'm not really sure if it's gonna work after adding the JS but I'll find out.
Edit Modal
To pass data to your fetch.php from JS I'd create a data array and use the POST method like the following:
var dataString = {
id: recipient,
other: 'string'
}
$.ajax({
type: "POST",
url: "fetch.php",
data: dataString,
cache: false,
success: function (data) {
console.log(data);
modal.find('.dash').html(data);
},
error: function(err) {
console.log(err);
}
});
and in your php:
$id = $_POST['id'];
$other = $_POST['other'];
//Do something with your data
Let me know if that helps.
i am new in php programming i have problem in this code, i want to show data in text box when i press select button which is in grid, but i am stuck on it because data not shown in text box and my select button also not working, what i do friends for this,
this is my code
FIRST NAME
MIDDLE NAME
LAST NAME
**--//php code--//**
$qrydatabind='SELECT ecode, first_name, middle_name, last_name, father_name, mother_name,
number_of_dependents, dob, gender, identification_mark, marital_status, spouse_name, mobile_number,
email_id, adhar_id, pan_number, passport_number, tin_number, dl_number FROM USER_MASTER ORDER BY user_id DESC
LIMIT 1';
$results= mysql_query($qrydatabind) or die(mysql_error());
while( $row = mysql_fetch_array( $results ) ) {
echo
"
<div class='table-responsive'>
<table border='1' style= 'background-color: #84ed86; color: #761a9b; ' >
<thead>
<tr>
<th></th>
<th>Entity Code</th>
<th>User Id</th> <th>User Name</th> <th>Father Name</th> <th>Mother Name</th> <th>No.Of Dependents</th>
<th>D.O.B</th> <th>GENDER</th> <th>Id Mark</th> <th>MARITAL STATUS</th> <th>SPOUSE NAME</th>
<th>Mob. Number</th> <th>E-Id</th> <th>ADHAR-ID</th> <th>PAN-No.</th> <th>PASSPORT-No.</th>
<th>TIN-NO.</th> <th>DL-No.</th>
</tr>
</thead>
<tr >
<td><button type='submit' class='btn btn-primary' name='select_button'>Select</button> </td>
<td>{$row['ecode']}</td> <td> echo $row[0];</td>
<td>{$row['first_name']} {$row['middle_name']} {$row['last_name']}</td>
<td>{$row['father_name']}</td> <td>{$row['mother_name']}</td>
<td>{$row['number_of_dependents']}</td> <td>{$row['dob']}</td>
<td>{$row['gender']}</td> <td>{$row['identification_mark']}</td>
<td>{$row['marital_status']}</td> <td>{$row['spouse_name']}</td>
<td>{$row['mobile_number']}</td> <td>{$row['email_id']}</td>
<td>{$row['adhar_id']}</td> <td>{$row['pan_number']}</td>
<td>{$row['passport_number']}</td> <td>{$row['tin_number']}</td>
<td>{$row['dl_number']}</td>
</tr> </table>
</div>";
}}
if(isset($_POST['select_button']))
{
$qrydatabind1='SELECT ecode, first_name, middle_name, last_name, father_name, mother_name,
number_of_dependents, dob, gender, identification_mark, marital_status, spouse_name, mobile_number,
email_id, adhar_id, pan_number, passport_number, tin_number, dl_number FROM USER_MASTER';
$results1= mysql_query($qrydatabind1) or die(mysql_error());
while( $row = mysql_fetch_array( $results1 ) ) {
echo'
<tr >
<td> </td>
<td><input type="text" value="{$row["first_name"]}" ></td>
</tr>';
}
}
It is very simple. as you are putting data in .
Your code
<td> echo $row[0];</td>
Text box code:
<input type="text" value="<?php echo $row[0];?>">
PHP Variables will only be evaluated in strings that are quoted in double quotes "
You should either concatinate a string:
echo '<td><input type="text" value="' . $row["first_name"] . '" ></td>';
Or, use <?php and ?> to switch between script and markup
while( $row = mysql_fetch_array( $results1 ) ) {
?><td><input type="text" value="<?php echo $row["first_name"]; ?>" ></td><?php
}
You can also use this as
echo'<tr><td><input type="text" value="'.$row["first_name"].'" ></td></tr>';
I have created a category module and switch button on then link show and switch button off then link hide. I am using codeigniter framemwork.
controller:
class Category extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('category_model');
$this->load->library('form_validation');
}
public function index() {
if (!empty($_SESSION['admin'])) {
$data = array(
'page_title' => 'Category',
'page_name' => 'category/index',
'result' => $this->category_model->list_all()
);
$this->load->view('template', $data);
} else {
redirect('login');
}
}
public function update_cat_status() {
$data = $this->category_model->update_cat_status($_GET);
echo json_encode($data);
}
VIew:
<table id="example" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
<th>Category Name</th>
<th>Status</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $value): ?>
<tr>
<td><?= $value['name']; ?></td>
<td> <div class="onoffswitch">
<input type="hidden" value="<?= $value["id"]; ?>"/>
<input type="checkbox" class="js-switch"
<?php
if ($value['status'] == 1) {
echo "checked";
}
?>>
</div>
</td>
<td>Edit</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Jquery
$(document).ready(function () {
//category switch
$('.onoffswitch').change(function () {
var cat_id = $(this).children(':hidden').val();
if ($(this).children(':checked').length === 0)
{
var status = 0;
}
else
{
var status = 1;
}
$.ajax({
type: 'GET',
url: base_url + "category/update_cat_status",
data: {status: status, cat_id: cat_id},
success: function (response) {
//console.log(response);
}
});
});
});
Screenshot
MY Question:
switch button on then edit button hide and switch button off then edit buttton show.
so please view above screenshot
add id attribute to your button in view:
from Edit
to: <a id="btnEdit<?= $value['id']?>" href="<?= base_url("category/edit/{$value['id']}"); ?>" class="btn btn-success">Edit</a>
on your jquery:
if ($(this).children(':checked'))
{
var status = 1;
$("#btnEdit"+cat_id).show();
}
else
{
var status = 0;
$("#btnEdit"+cat_id).hide();
}
This is a php and javascript. I have a row consist of classcode, courseNumber,courseDescription,units,time,days,room the problem is I could not arrange it. It displays all data in the column of courseDescription. Below is the picture what it looks like now and how I want it to be.
[https://plus.google.com/u/0/112172241812600096315/posts/Du8f6rHsEtY?pid=6147544934724622242&oid=112172241812600096315][1]
javascript
$(document).ready(function() {
$("#faq_search_input").watermark("Begin Typing to Search");
$("#faq_search_input").keyup(function()
{
var faq_search_input = $(this).val();
var dataString = 'keyword='+ faq_search_input;
if(faq_search_input.length>3)
{
$.ajax({
type: "GET",
url: "search.php",
data: dataString,
beforeSend: function() {
$('input#faq_search_input').addClass('loading');
},
success: function(server_response)
{
$('#searchresultdata').html(server_response).show();
$('span#faq_category_title').html(faq_search_input);
if ($('input#faq_search_input').hasClass("loading")) {
$("input#faq_search_input").removeClass("loading");
}
}
});
}return false;
});
});
home.php
<div id="SubjectOffering" class = "listTable" >
<p><h3> Subject Offering </h3> </p>
<p>
<div class = "searchBar">
<form id="searchbox" action="#" onsubmit="return false;">
<!-- The Searchbox Starts Here -->
<input name="query" type="text" id="faq_search_input" />
<!-- The Searchbox Ends Here -->
</form>
</div>
</p>
<p>
<table>
<tr>
<td>Class Code</td>
<td>Course Number</td>
<td>Course Description</td>
<td>Time</td>
<td>Days</td>
<td>Room</td>
</tr>
<tr>
<td></div></td>
<td></td>
<td><div id="searchresultdata" class="faq-articles"> </td>
<td> </td>
<td> </td>
<td></td>
<td> </td>
</tr>
</table>
search.php
<?php
include_once ('connections.php');
if(isset($_GET['keyword'])){
$keyword = trim($_GET['keyword']) ;
$keyword = mysqli_real_escape_string($dbc, $keyword);
$query = "select courseCode,classcode,courseDescription,time,day,room from class where classcode like '%$keyword%' or courseDescription like '%$keyword%' or courseCode like '%$keyword%' or time like '%$keyword%'
or day like '%$keyword%' or room like '%$keyword%'";
//echo $query;
$result = mysqli_query($dbc,$query);
if($result){
if(mysqli_affected_rows($dbc)!=0){
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<p> <b>'.$row['classcode'].'</b> '.$row['courseCode']. '</b>'.$row['courseDescription'].'</b> '.$row['time'].'</b> '.$row['day'].'</b> '.$row['room'].'</p>';
}
}else {
echo 'No Results for :"'.$_GET['keyword'].'"';
}
}
}else {
echo 'Parameter Missing';
}
?>
Your code has few html tag errors like unused tags are there. I just revised your code. Please verify if you found any errors still just let me know.
Your JAVASCRIPT / JQUERY code should be as the following:
/**
* #description: Update result when user searches.
* #author Vivek Keviv
* #params none
* #return none
*/
$ (document).ready(function() {
$("#faq_search_input").watermark("Begin Typing to Search");
$("#faq_search_input").keyup(function() {
var faq_search_input = $(this).val();
var dataString = 'keyword='+ faq_search_input;
if (faq_search_input.length>3) {
$.ajax({
type: "GET",
url: "search.php",
data: dataString,
beforeSend: function() {
$('input#faq_search_input').addClass('loading');
},
success: function(server_response) {
$('#searchresultdata').html(server_response).show();
$('span#faq_category_title').html(faq_search_input);
if ($('input#faq_search_input').hasClass("loading")) {
$("input#faq_search_input").removeClass("loading");
}
}
});
}
return false;
});
});
Your HTML code should be as the following:
<div id="SubjectOffering" class="listTable">
<p>
<h3>Subject Offering</h3>
</p>
<div class="searchBar">
<form id="searchbox" action="#" onsubmit="return false;">
<!-- The Searchbox Starts Here -->
<input name="query" type="text" id="faq_search_input" />
<!-- The Searchbox Ends Here -->
</form>
</div>
<div id="searchresultdata" class="faq-articles">
<table>
<tr>
<th>Class Code</th>
<th>Course Number</th>
<th>Course Description</th>
<th>Time</th>
<th>Days</th>
<th>Room</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
</div>
Your PHP code should be as the following:
<?php
include_once ('connections.php');
if (isset($_GET['keyword'])) {
$keyword = trim($_GET['keyword']) ;
$keyword = mysqli_real_escape_string($dbc, $keyword);
$query = "select courseCode,classcode,courseDescription,time,day,room from class where classcode like '%$keyword%' or courseDescription like '%$keyword%' or courseCode like '%$keyword%' or time like '%$keyword%'
or day like '%$keyword%' or room like '%$keyword%'";
//echo $query;
$result = mysqli_query($dbc,$query);
if ($result) {
if (mysqli_affected_rows($dbc)!=0) { ?>
<table>
<tr>
<th>Class Code</th>
<th>Course Number</th>
<th>Course Description</th>
<th>Time</th>
<th>Days</th>
<th>Room</th>
</tr>
<?php while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) { ?>
<tr>
<td><?php echo $row['classcode']; ?></td>
<td><?php echo $row['courseCode']; ?></td>
<td><?php echo $row['courseDescription']; ?></td>
<td><?php echo $row['time']; ?></td>
<td><?php echo $row['day']; ?></td>
<td><?php echo $row['room']; ?></td>
</tr>
<?php } ?>
</table>
<?php } else {
echo 'No Results for :"'.$_GET['keyword'].'"';
}
}
} else {
echo 'Parameter Missing';
}
?>
Thanks & Regards,
Vivek
How to retain with the same selection in dropdown when refresh or after I submit or delete?
I have this code below that select value from database. I want to back with the same value in select dropdown after I refresh or I submit the form or delete records.
bid.php
<html>
<head>
<script>
function showUser(str) {
var $txtHint = $('#txtHint');
if (str == "") {
$txtHint.html('');
return;
}
$txtHint.load('bid_list.php?q=' + str)
}
</script>
</head>
<body onload=showUser(str="ALL")>
<?php
$mysqli = new mysqli("localhost", "root", "", "app");
$result = $mysqli->query("SELECT bid FROM procurement WHERE bid LIKE '13-___' OR bid LIKE '1_-___' OR bid LIKE '2_-___' GROUP BY bid ORDER BY bid");
$option = '';
while($row = $result->fetch_assoc())
{
$option .= '<option value = "'.$row['bid'].'">'.$row['bid'].'</option>';
}
?>
<select name="users" onchange="showUser(this.value)" style="overflow:scroll;width:100px;">
<option value="ALL" selected='ALL'>ALL</option>
<?php echo $option; ?>
</select>
<div id="txtHint">
</div>
</body>
</html>
bid_list.php
<?php
$mysqli = new mysqli("localhost", "root", "", "app");
$q = $_GET["q"];
$where = '';
if ( $q != 'ALL' ) {
$where = " WHERE bid='$q' ";
}
$result1 = $mysqli->query("
SELECT bid, item_name, item_description, unit, unit_cost, quantity, supplier, po_number, po_date, counter, SUM(unit_cost*quantity) AS total_amount
FROM procurement
$where
GROUP BY counter ORDER BY bid
");
echo'<table id="tfhover" cellspacing="0" class="tablesorter" style="text-transform:uppercase;">
<thead>
<tr>
<th colspan="3" id="none" style="cursor: default;"></th>
<th title="Item Name">Item Name</th>
<th title="Item Description">Description</th>
<th title="Example : Pc, Pcs, Box and Etc.">Unit</th>
<th title="Item Price">Unit Cost</th>
<th title="Total Item Quantity">QTY</th>
<th title="Total Price">Total Amount</th>
<th title="Name of Supplier">Supplier</th>
<th title="Purchase Order Date">PO Date</th>
<th title="Purchase Order #">PO #</th>
<th id="none" style="cursor: default;"></th>
</tr>
</thead>';
echo'<tbody>';
while($row = $result1->fetch_assoc()){
if($row['bid'] != '')
{
echo'<tr>
<td align="center"><img src="images/del.png" border="0" width="10" height="10" title="Delete"></td>
<td align="center"><a class="fancybox" href="edit.php?pn='.$row["counter"].'"><img src="images/edit.png" border="0" width="10" height="10" title="Edit"></a></td>
<td align="center"><a class="fancybox" href="comments.php?pn='.$row["counter"].'"><img src="images/remarks.png" border="0" width="10" height="10" title="Remarks and Notes"></a></td>
<td>'.$row['item_name'].'</td>
<td>'.$row['item_description'].'</td>
<td>'.$row['unit'].'</td>
<td>'.number_format($row['unit_cost'], 2, '.', ',').'</td>
<td>'.$row['quantity'].'</td>
<td>'.number_format($row['total_amount'], 2, '.', ',').'</td>
<td>'.$row['supplier'].'</td>
<td>'.$row['po_date'].'</td>
<td>'.$row['po_number'].'</td>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="'.$row['counter'].'"></td>
</tr>';
}
}
echo "</tbody></table>";
use a jquery ajax call, and update the DOM when your ajax call succesfully returns. see http://api.jquery.com/jQuery.ajax/
AJAX can be used for interactive communication with a database, please check the web site here : http://www.w3schools.com/php/php_ajax_database.asp
i believe that is what you need.