I have been trying to make a search engine where I have two inputfields, both are searching in the same database and table but different rows/columns. My problem is they both search in both rows/columns. I'm adding screenshots at the bottom so you can understand me better.
I'm suspecting there's something wrong the functions in document 2, but I can't understand what.
Here's the code in two documents.
Document 1
mysql_connect ("localhost","xxxx","xxxx") or die ("Tilkoblingsfeil");
mysql_select_db ("xxxx") or die("Finner ikke database");
$output = '';
if(isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
$searchq = preg_replace ("#^0-9#"," ",$searchq);
$query = mysql_query("SELECT * FROM ds_OrderItem WHERE idProduct LIKE '%$searchq%' LIMIT 100") or die("Klarte ikke søke!");
$count = mysql_num_rows ($query);
if($count == 0){
$output = 'Ingen produktID med disse verdiene';
}else{
$output .='<table border="1" cellpadding="5" cellspacing="1" width="50%" bordercolor="grey"><tr><th>ProduktID</th><th>Ordrenummer</th><th>Beskrivelse</th><th>Antall</th>';
while($row = mysql_fetch_array($query)) {
$output .= '<tr><td>'.$row['idProduct'].'</td><td>'.$row['idOrder'].'</td><td>'.$row['title'].'</td><td>'.$row['qty'].'</td></tr>';
}
$output .='</table>';
if($_POST['searchVal'] == NULL) {
$output = "";
}
}
}
if(isset($_POST['searchVal'])) {
$searchw = $_POST['searchVal'];
$searchw = preg_replace ("#^0-9a-z#i"," ",$searchw);
$query = mysql_query("SELECT * FROM ds_OrderItem WHERE title LIKE '%$searchw%' LIMIT 100") or die("Klarte ikke søke!");
$count = mysql_num_rows ($query);
if($count == 0){
$output = 'Ingen TECDOC artikler med disse verdiene';
}else{
$output .='<table border="1" cellpadding="5" cellspacing="1" width="50%" bordercolor="grey"><tr><th>ProduktID</th><th>Ordrenummer</th><th>Beskrivelse</th><th>Antall</th>';
while($row = mysql_fetch_array($query)) {
$output .= '<tr><td>'.$row['idProduct'].'</td><td>'.$row['idOrder'].'</td><td>'.$row['title'].'</td><td>'.$row['qty'].'</td></tr>';
}
$output .='</table>';
if($_POST['searchVal'] == NULL) {
$output = "";
}
}
}
echo ($output);
?>
Document 2
<?php
error_reporting(1);
include('system_admin.inc.php');
make_header($title,array('enable_ajax' => true));
?>
<html>
<head>
<title>TecdocORProduct</title>
<script type=text/javascript " src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type=text/javascript ">
function searchq() {
var searchTxt = $("input[name='search1']").val();
$.post("testsearch.php", {searchVal: searchTxt}, function(output) {
$("#output") .html(output);
});
}
function searchw() {
var searchTxt = $("input[name='search2']").val();
$.post("testsearch.php", {searchVal: searchTxt}, function(output) {
$("#output") .html(output);
});
}
</script>
</head>
<body>
<h1>Søk i produktID eller tecdocnr</h1>
<form class="odd" action="william.properties.php" method="post">
<input type="text" name="search1" placeholder="Søk etter produktID" onkeyup="searchq();" />
<form class="odd" action="william.properties.php" method="post">
<input type="text" name="search2" placeholder="Søk etter tecdocnr" onkeyup="searchw();" />
<div id="output">
</div>
</form>
</body>
</html>
Thank you for your time and any answers I might be given.
You should use unique keys for your both search request's {searchVal: searchTxt} or {searchVal2: searchTxt} and use in php search conditions accordingly
Try changing your js and php code like below:
JS:
function searchq() {
var searchTxt = $("input[name='search1']").val();
$.post("testsearch.php", {searchVal: searchTxt}, function(output) {
$("#output") .html(output);
});
}
function searchw() {
var searchTxt = $("input[name='search2']").val();
$.post("testsearch.php", {searchVal2: searchTxt}, function(output) {
$("#output") .html(output);
});
}
Php:
mysql_connect ("localhost","xxxx","xxxx") or die ("Tilkoblingsfeil");
mysql_select_db ("xxxx") or die("Finner ikke database");
$output = '';
if(isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
$searchq = preg_replace ("#^0-9#"," ",$searchq);
$query = mysql_query("SELECT * FROM ds_OrderItem WHERE idProduct LIKE '%$searchq%' LIMIT 100") or die("Klarte ikke søke!");
$count = mysql_num_rows ($query);
if($count == 0){
$output = 'Ingen produktID med disse verdiene';
}else{
$output .='<table border="1" cellpadding="5" cellspacing="1" width="50%" bordercolor="grey"><tr><th>ProduktID</th><th>Ordrenummer</th><th>Beskrivelse</th><th>Antall</th>';
while($row = mysql_fetch_array($query)) {
$output .= '<tr><td>'.$row['idProduct'].'</td><td>'.$row['idOrder'].'</td><td>'.$row['title'].'</td><td>'.$row['qty'].'</td></tr>';
}
$output .='</table>';
if($_POST['searchVal'] == NULL) {
$output = "";
}
}
exit($output);
}
if(isset($_POST['searchVal2'])) {
$searchw = $_POST['searchVal2'];
$searchw = preg_replace ("#^0-9a-z#i"," ",$searchw);
$query = mysql_query("SELECT * FROM ds_OrderItem WHERE title LIKE '%$searchw%' LIMIT 100") or die("Klarte ikke søke!");
$count = mysql_num_rows ($query);
if($count == 0){
$output = 'Ingen TECDOC artikler med disse verdiene';
}else{
$output .='<table border="1" cellpadding="5" cellspacing="1" width="50%" bordercolor="grey"><tr><th>ProduktID</th><th>Ordrenummer</th><th>Beskrivelse</th><th>Antall</th>';
while($row = mysql_fetch_array($query)) {
$output .= '<tr><td>'.$row['idProduct'].'</td><td>'.$row['idOrder'].'</td><td>'.$row['title'].'</td><td>'.$row['qty'].'</td></tr>';
}
$output .='</table>';
if($_POST['searchVal'] == NULL) {
$output = "";
}
}
exit($output);
}
?>
You need to differentiate the post data name { idproduct : searchTxt} and { title:searchtxt }
JS :
function searchq() {
var searchTxt = $("input[name='search1']").val();
$.post("testsearch.php", {idproduct: searchTxt}, function(output) {
$("#output") .html(output);
});
}
function searchw() {
var searchTxt = $("input[name='search2']").val();
$.post("testsearch.php", {title: searchTxt}, function(output) {
$("#output") .html(output);
});
}
PHP :
if(isset($_POST['idproduct'])) { .... }
if(isset($_POST['title'])) { .... }
Related
i have problem with my code. Lets say i have PHP site in background and this site do something. Lets say in this site i have $status = "1"; and in my ajax code i have:
$.ajax({
url: 'src/single.php',
type: 'POST',
data: {single:1, id:id},
success: function(data){
$('#live_data').val('');
$('#live_data').html(data);
}
});
and i want to know if it si possible to trigger $status from my php page. Something like
success: function(data){
if (data.status == '1') {
$('#live_data').val('');
$('#live_data').html(data);
} if (data.status == '2') {
$('#search_result').val('');
$('#search_result').html(data);
}
Thank you for help if you can :)
EDIT:
So here is my code:
first is for fetch data from db,
and second is for search data from db,
And third is for fetch single data from db.
It work good but problem is that when i click on row it display results 2times first from show and second from search
But in search it is correct.
This $('#live_data') is for show.php data and $('#search_result') is for search data. And when i click on row from show.php it will display result 2times. First from show.php and second from search.php but when i click on row from search.php it display correctly (only one result). I know it is cuz i have
$('#live_data').val('');
$('#live_data').html(data);
but when i want to hide duplicated result from show.php i do $('#search_result').val(''); but then i hide my single data from search.php
This is why i want to controll it with if statement
I attach images below
ajax for single data fetch:
//jeden zaznam
$(document).on('click', '.clickable-row', function(){
var id = $(this).data("id2");
$.ajax({
url: 'src/single.php',
type: 'POST',
data: {single:1, id:id},
success: function(data){
$('#live_data').val('');
$('#live_data').html(data);
$('#search_result').html(data);
}
});
});
First:
<?php
include("db.php");
$output = "";
$sql = "SELECT * FROM otk";
$result = mysqli_query($conn, $sql);
$output .= "<table class='table table-hover'>
<thead>
<tr>
<th>Číslo zákazky</th>
<th>Pozícia</th>
<th>Stav</th>
<th>Dátum</th>
<th>Operátor</th>
</tr>
</thead>";
while ($row = mysqli_fetch_array($result))
{
$output .= "<tr class = 'clickable-row' data-id2 ='".$row['id_otk']."'>
<td>".$row['kod_otk']."</td>
<td>".$row['poz_otk']."</td>
<td>".$row['stav_otk']."</td>
<td>".$row['datum_otk']."</td>
<td>".$row['op_otk']."</td>
</tr>";
}
$output .= "</table>";
echo $output;
?>
Second:
<?php
if (isset($_POST['search']))
{
include("db.php");
$search_text = mysqli_real_escape_string($conn, $_POST['search_text']);
$search = htmlspecialchars($search_text);
$output = "";
$output .= "
<table class='table table-hover'>
<thead>
<tr>
<th>Číslo zákazky</th>
<th>Pozícia</th>
<th>Stav</th>
<th>Dátum</th>
<th>Operátor</th>
</tr>
</thead>";
$sql = "SELECT * FROM otk WHERE kod_otk LIKE '%".$search."%'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= "
<tr class = 'clickable-row' data-id2 ='".$row['id_otk']."'>
<td>".$row['kod_otk']."</td>
<td>".$row['poz_otk']."</td>
<td>".$row['stav_otk']."</td>
<td>".$row['datum_otk']."</td>
<td>".$row['op_otk']."</td>
</tr>";
}
$output .= "</table>
<div class='d-flex justify-content-center'>
<button class='btn btn-primary' onclick='history.go();'>Späť</button>
</div>";
echo $output;
} else {
echo "Žiadny záznam";
}
}
?>
Third:
<?php
if (isset($_POST['single']))
{
include("db.php");
$id = mysqli_real_escape_string($conn, $_POST['id']);
echo "id je: ".$id;
$sql = "SELECT * FROM otk WHERE id_otk = '".$id."'";
$result = mysqli_query($conn, $sql);
$output = "<table class='table table-hovrt'>
<thead>
<tr>
<th>Číslo zákazky</th>
<th>Pozícia</th>
<th>Stav</th>
<th>Poradové číslo</th>
<th>Technológia</th>
<th>Dokument</th>
<th>Zariadenie</th>
<th>Operátor</th>
<th>Dátum</th>
</tr>
</thead>";
if (mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_array($result))
{
$output .= "
<tr>
<td>".$row['kod_otk']."</td>
<td>".$row['poz_otk']."</td>
<td>".$row['stav_otk']."</td>
<td>".$row['cislo_otk']."</td>
<td>".$row['tech_otk']."</td>
<td>".$row['dok_otk']."</td>
<td>".$row['zar_otk']."</td>
<td>".$row['op_otk']."</td>
<td>".$row['datum_otk']."</td>
</tr>";
}
$output .= "</table>
<div class='d-flex justify-content-center'>
<button class='btn btn-primary' onclick='history.go(-2);'>Späť</button>
</div>";
echo $output;
} else {
echo "Error: " . mysqli_error($sql);
}
}
?>
Image01:
Image02:
You must get the single and id vars with:
<?php
$single = $_POST['single'];
$id = $_POST['id'];
// do your logic and set it's status
echo json_encode(['status' => $status]);
Then you'll be able to retrieve the status param in your success data.
Your best bet would be to use json_encode()
example:
$result = array();
$result = array('status' => '1');
echo json_encode(result);
With the above, you'll be able to access the status key via jquery.
Imagine that I have 1 2 3 4pages in my table, then if I click the page 1 the number 1 will be background: black Then the problem is the whole pages will be black, when I executing. Please help me out of this problem. I want to change the background color of the page if what page I click. Tyia
Here's the code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="table-responsive" id="pagination_data"></div>
<script>
$(document).ready(function(){
load_data();
function load_data(page)
{
$.ajax({
url:"pagination.php",
method:"POST",
data:{page:page},
success:function(data){
$('#pagination_data').html(data);
$(".pagination_link").css({"background":, "black"});
}
})
}
$(document).on('click', '.pagination_link', function(){
var page = $(this).attr("id");
load_data(page);
$(".pagination_link").css({"background":, "black"});
});
});
</script>
PHP code:
<?php
//pagination.php
$connect = mysqli_connect("localhost", "root", "", "psbr");
$record_per_page = 6;
$page = '';
$output = '';
if(isset($_POST["page"]))
{
$page = $_POST["page"];
}
else
{
$page = 1;
}
$start_from = ($page - 1)*$record_per_page;
$query = "SELECT * FROM accounts ORDER BY id DESC LIMIT $start_from, $record_per_page";
$result = mysqli_query($connect, $query);
$output .= "
<div class='grid-container'>
";
while($row = mysqli_fetch_array($result))
{
$output .= '
<div class="grid-content">
<img src="../accounts/table/upload/'.$row['image'].'" class="grid-image">
</div>
';
}
$output .= '</div> <br /><div align="right">';
$page_query = "SELECT * FROM accounts ORDER BY id DESC";
$page_result = mysqli_query($connect, $page_query);
$total_records = mysqli_num_rows($page_result);
$total_pages = ceil($total_records/$record_per_page);
for($i=1; $i<=$total_pages; $i++)
{
$output .= "<span class='pagination_link' id='".$i."'>".$i."</span>";
}
$output .= '</div><br /><br />';
echo $output;
?>
So the issue i'm having is that when I click the post button nothing happens and a comment doesn't display or go to my database. I have checked for spelling mistakes and believe I have got them all.
This is my HTML form called community.php
<div class="page-container">
<?php
get_total();
require_once 'check_com.php';
?>
<form action="" method="post" class="main">
<label>Enter a Brief Comment</label>
<textarea class="form-text" name="comment" id="comment"></textarea>
<br />
<input type="submit" class="form-submit" name="new_comment" value="Post">
</form>
<?php get_comments(); ?>
</div>
This is my js script called global.js
$(document). ready(function() {
$(".child-comments").hide();
$("a#children").click(function() {
var section = $(this).attr("name");
$("#C-" + section).toggle();
});
$(".form-submit").click(function() {
var commentBox= $("#comment");
var commentCheck= commentBox.val();
if(commentCheck == '' || commentCheck == NULL) {
commentBox.addClass("form-text-error");
return false;
}
});
$(".form-reply").click(function() {
var replyBox= $("#new-reply);
var replyCheck= replyBox.val();
if(replyCheck == '' || replyCheck == NULL) {
replyBox.addClass("form-text-error");
return false;
}
});
$("a#reply").one("click", function() {
var comCode = $(this).attr("name");
var parent = $(this).parent();
parent.append("<br / ><form actions='' method='post'><textarea class='form-text' name='new-reply' id='new-reply' required='required'></textarea><input type='hidden' name='code' value='"+comCode"' /><input type='submit' class='form-submit' id='form-reply' name='new_reply' value='Reply'/></form>")
});
})
Check_com.php file
<?php
// new comment fucntion
if(isset($_POST['new_comment'])) {
$new_com_name = $_SESSION['user'];
$new_com_text = $_POST['comment'];
$new_com_date = date('Y-m-d H:i:s');
$new_com_code = generateRandomString();
if(isset($new_com_text)) {
mysqli_query($conn, "INSERT INTO `parents` (`user`, `text`, `date`, `code`) VALUES ('$new_com_name', '$new_com_text', '$new_com_date', '$new_com_code')");
}
header ("Location: ");
}
// new reply
if(isset($_POST['new_reply'])) {
$new_reply_name = $_SESSION['user'];
$new_reply_text = $_POST['new-reply'];
$new_reply_date = date('Y-m-d H:i:s');
$new_reply_code = $_POST('code');
if(isset($new_reply_text)) {
mysqli_query($conn, "INSERT INTO `children` (`user`, `text`, `date`, `par_code`) VALUES ('$new_reply_name', '$new_reply_text', '$new_reply_date', '$new_reply_code')");
}
header ("Location: ");
}
?>
Functions.php File
<?php
session_start();
$_SESSION['user'] = 'Admin';
function get_total() {
require 'includes/dbh.inc.php';
$result = mysqli_query($conn, "SELECT * FROM `parents` ORDER BY `date` DESC");
$row_cnt = mysqli_num_rows($result);
echo '<h1>All Comments ('.$row_cnt.')</h1';
}
function get_comments() {
require 'includes/dbh.inc.php';
$result = mysqli_query($conn, "SELECT * FROM `parents` ORDER BY `date` DESC");
$row_cnt = mysqli_num_rows($result);
foreach($result as $item) {
$date = new dateTime($item['date']);
$date = date_format($date, 'M j, Y | H:i:s');
$user = $item['user'];
$comment = $item['text'];
$par_code = $item['code'];
echo '<div class="comment" id="'.$par_code.'">'
.'<p class="user">'.$user.'</p> '
.'<p class="time">'.$date.'</p>'
.'<p class="comment-text">'.$comment.'</p>'
.'<a class="link-reply" id="reply" name="'.$par_code.'">Reply</a>';
$chi_result = mysqli_query($conn, "SELECT * FROM `children` WHERE `par_code`='$par_code' ORDER BY `date` DESC");
$chi_cnt = mysqli_num_rows($chi_result);
if($chi_cnt == 0){
}else {
echo '<a class="link-reply" id="children" name="'.$par_code.'"><span id="tog_text">replies</span> ('.$chi_cnt.')</a>'
.'<div class="child-comments" id="C-'.$par_code.'">';
foreach ($chi_result as $com) {
$chi_date = new dateTime($com['date']);
$chi_date = date_format($chi_date, 'M j, Y | H:i:s');
$chi_user = $com['user'];
$chi_com = $com['text'];
$chi_par = $com['par_code'];
echo '<div class="child" id="'.$par_code.'-C">'
.'<p class="user">'.$chi_user.'</p> '
.'<p class="time">'.$chi_date.'</p>'
.'<p class="comment-text">'.$chi_com.'</p>'
.'</div>';
}
echo '</div>';
}
echo '</div>';
}
}
function generateRandomString($length = 6) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$characterLength = strlen($characters);
$randomString = '';
for($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $characterLenght - 1)];
}
return $randomString;
}
?>
Either remove action="" or change it to action="community.php"
I have a table with the following.
Table parts_stock
*--------------------*
| id | sku | stock |
| 1 | 101 | 2 |
| 2 | 102 | 3 |
*--------------------*
This is my code so far, i'm sure there are many ways to achieve this but ideally I want the qty value to change based on which button is clicked on without the page being refreshed (AJAX probably).
<tbody>
<?php
$query = 'SELECT stock_id, sku, in_stock ';
$query .= 'FROM parts_stock';
confirmQuery($query);
$select_skus = mysqli_query($connection, $query);
$num = mysqli_num_rows($select_skus);
if($num>0) {
while($row = mysqli_fetch_assoc($select_skus)) {
$id = $row['stock_id'];
$sku = $row['sku'];
$qty = $row['in_stock'];
echo "<tr>";
echo "<td>".$sku."</td>";
echo "<td>".$qty."</td>";
echo "<td>
<a href='' onclick='rem_qty()' id='minus' name='minus' class='btn btn-warning'><span class='glyphicon glyphicon-minus'></span></a>
<a href='' onclick='add_qty()' id='plus' name='plus' class='btn btn-success'><span class='glyphicon glyphicon-plus'></span></a>
</td>";
</td>";
}
}?>
</tbody>
ajax_search.js
<script>
function rem_qty(){
$.ajax({
type: "POST",
url: "update_qty.php",
data: {id_m: stock_id}
});
}
function add_qty(){
$.ajax({
type: "POST",
url: "update_qty.php",
data: 'id_p: stock_id'
});
}
</script>
update_qty.php file
<?php
if (isset($_POST['id_m'])) {
$r = $_POST['id_m'];
echo $r;
$cur_inv = "SELECT in_stock FROM parts_stock WHERE stock_id = '".$r."'";
$cur_query = mysqli_query($connection, $cur_inv);
while ($row = mysqli_fetch_assoc($cur_query)) {
$rem_stock = $row['in_stock'];
$rem_stock -= 1;
}
$inv_update = "UPDATE parts_stock SET in_stock = '".$rem_stock."' WHERE stock_id = '".$value."'";
$inv_query = mysqli_query($connection, $inv_update);
}
if (isset($_POST['id_p'])) {
$a = $_POST['id_p'];
echo $a;
$cur_inv = "SELECT in_stock FROM parts_stock WHERE stock_id = '".$a."'";
$cur_query = mysqli_query($connection, $cur_inv);
while ($row = mysqli_fetch_assoc($cur_query)) {
$add_stock = $row['in_stock'];
$add_stock -= 1;
}
$inv_update = "UPDATE parts_stock SET in_stock = '".$add_stock."' WHERE stock_id = '".$value."'";
}
?>
A simple and complete solution: just change mysqli_connect config in both page
index.php
<?php
$connection = mysqli_connect("localhost", "root", "", "dbname"); //change dbname
$query = 'SELECT id, sku, stock FROM parts_stock';
//confirmQuery($query);
$select_skus = mysqli_query($connection, $query);
$num = mysqli_num_rows($select_skus);
?>
<table>
<tr>
<th>Sku</th>
<th>Stock</th>
<th>Action</th>
</tr>
<?php if($num>0){
while($row = mysqli_fetch_assoc($select_skus)) {
$id = $row['id'];
$sku = $row['sku'];
$qty = $row['stock'];
echo "<tr>";
echo "<td>".$sku."</td>";
echo "<td class='stock-{$id}'>".$qty."</td>";
echo "<td>
<a class='btn btn-warning' onclick='add_qty({$id})' href='#'><span class='glyphicon glyphicon-minus'>Value Add</span></a>
<a class='btn btn-success' onclick='rem_qty({$id})' href='#'><span class='glyphicon glyphicon-plus'>Value Deduct</span></a>
</td>";
echo "</tr>" ;
}
}?>
</table>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script type="text/javascript">
function add_qty(id){
$.ajax({
type: "POST",
url: 'update_qty.php', //Relative or absolute path to response.php file
data: {id:id, type:'add'},
dataType: "json",
success: function (data) {
console.log(data);
if(data.success){
//successfully added
$(".stock-"+id).html(data.data.stock);
alert(data.msg);
}
}
});
}
function rem_qty(id){
$.ajax({
type: "POST",
url: 'update_qty.php', //Relative or absolute path to response.php file
data: {id:id, type:'rem'},
dataType: "json",
success: function (data) {
console.log(data);
if(data.success){
//successfully added
$(".stock-"+id).html(data.data.stock);
alert(data.msg);
}
}
});
}
</script>
update_qty.php
<?php
$connection = mysqli_connect("localhost", "root", "", "dbname"); ////change dbname
header('Content-Type: application/json');
$success = false; $msg ="";
if (isset($_POST['id'])) {
$id = $_POST['id'];
$cur_inv = "SELECT * FROM parts_stock WHERE id ={$id}";
$cur_query = mysqli_query($connection, $cur_inv);
if(mysqli_num_rows($cur_query)>0){ //if id is exist in database
if($_POST['type']=="add"){
$inv_update = "UPDATE parts_stock SET stock = (stock+1) WHERE id = {$id}"; //increase your stock dynamically
}elseif($_POST['type']=="rem"){
$inv_update = "UPDATE parts_stock SET stock = (stock-1) WHERE id = {$id}"; //increase your stock dynamically
}
$inv_query = mysqli_query($connection, $inv_update);
if($inv_query){ //If sucess
$msg = "Successfully Updated";
$success = true;
}else{ //if failed
$msg = "Failed to Update";
}
}else{
$msg="Id is not found.";
}
$last_inv = "SELECT * FROM parts_stock WHERE id ={$id}";
$last_query = mysqli_query($connection, $last_inv);
$row = mysqli_fetch_assoc($last_query);
echo json_encode(array('success'=>$success, 'msg'=>$msg, 'data'=>$row));
}
?>
No need extra js file just index.php and update_qty.php
Working example
demo.php
<?php
$query = 'SELECT id, sku, stock ';
$query .= 'FROM parts_stock';
confirmQuery($query);
$select_skus = mysqli_query($connection, $query);
$num = mysqli_num_rows($select_skus);
if($num>0) {
while($row = mysqli_fetch_assoc($select_skus)) {
$id = $row['id'];
$sku = $row['sku'];
$qty = $row['stock'];
$data = "";
$data .= "<tr>";
$data .= "<td>{$sku}</td>";
$data .= "<td>{$qty}</td>";
$data .= "<td>
<a class='btn btn-warning' href='inventory.php?source=edit_inventory&id={$id}'><span class='glyphicon glyphicon-minus'></span></a>
<a class='btn btn-success' href='inventory.php?source=edit_inventory&id={$id}'><span class='glyphicon glyphicon-plus'></span></a>
</td>";
echo $a;
exit;
}
}?>
AJAX:
<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<table >
<tr>
<th>Id</th>
<th>Sku</th>
<th>Qty</th>
</tr>
<tbody id="mytable">
</tbody>
</table>
<button id="clickme">Click</button>
<script>
$(document).ready(function(){
$("#clickme").click(function() {
$.ajax({
url:"demo.php",
type:"GET",
beforeSend:function() {
$("#mytable").empty();
},
success:function(response){
$("#mytable").append(response);
}, error:function(err) {
console.log(err);
}
})
});
});
In your HTML Button to have a onClick event like this onclick="buttonSubtract1('<?php if(isset($val['itm_code'])){echo $val['itm_code'];}?>')"(You can fetch the itm_code from your db).Then Write your AJAX for Request and Response. And You need to Pass the itm_code through var x. e.g for like this xmlhttp.open("POST", "ajax/get_items.php?val=" +x, true);
In AJAX file
$item_cat = $_SESSION['item_cat']; // get a category from session
$iname=$_GET['val']; //get the value from main php file
if(!key_exists($item_cat)
{
$_SESSION['main'][$iname] = "1";
}
else
{
$_SESSION['main'][$item_cat][$iname]++;
}
echo "<pre>";
print_r($_SESSION['main']);
echo "</pre>";
EDIT 1
function buttonSubtract1(x)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.location.assign('items.php');
}
}
xmlhttp.open("POST", "ajax/get_items.php?val=" +x, true);
xmlhttp.send();
}
Good afternoon Stackoverflow,
Today I got f'd by IE as usual when using the jQuery library with Ajax.
So whats my problem, well take a look at my live demo: http://194.247.30.66/~keizer/iemakesmesad/
Try ordening the items in FF / Chrome, then test it in IE and we'll all facepalm because of Bill Gates.
Possible explanation: IE grabs the whole ul instead of the ul inside the ul.. any solution?
Code of index.php
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
// When the document is ready set up our sortable with it's inherant function(s)
$(document).ready(function() {
<?php
include_once("../ond/inc/php/connect.php");
$i = 0;
$result = mysql_query("SELECT * FROM paginas WHERE type='0' ORDER BY id ASC");
while($row = mysql_fetch_array($result))
{
$i++;
$result2 = mysql_query("SELECT * FROM paginas WHERE type=".$row{"id"}." ORDER BY id ASC LIMIT 1");
while($row2 = mysql_fetch_array($result2))
{
echo ' $("#submenu_list'.$i.'").sortable({
handle : \'.handle\',
update : function () {
var order = $(\'#submenu_list'.$i.'\').sortable(\'serialize\');
$("#info").load("process-sortable.php?"+order);
}
});
';
}
}
?>
$("#menu_list").sortable({
handle : '.handle',
update : function () {
var order = $('#menu_list').sortable('serialize');
$("#info").load("process-sortable.php?"+order);
}
});
});
</script>
</head>
<body>
<?php
$result = mysql_query("SELECT * FROM paginas_test WHERE type='0' ORDER BY position ASC");
echo '<table cellspacing="2" cellpadding="2">';
echo ' <tr>';
echo ' <th colspan="2" scope="col"><img src="../ond/inc/afb/report.png" />Volgordebepaling</th>';
echo ' </tr>';
echo '</table>';
echo '<ul id="menu_list">';
$i = 0;
while ($row = mysql_fetch_array($result))
{
$i++;
echo '<li style="list-style:none;" id="listItem_'.$row{"id"}.'">';
echo ' <img src="../testajax/arrow.png" alt="move" width="16" height="16" class="handle" /> '.$row{"titel"}.'<br />';
#mysql_query("SELECT * FROM paginas WHERE type='0' ORDER BY id ASC");
$result2 = mysql_query("SELECT * FROM paginas_test WHERE type=".$row{"id"}." ORDER BY position ASC");
echo '<ul id="submenu_list'.$i.'">';
while($row2 = mysql_fetch_array($result2))
{
echo '<li style="list-style:none;margin-left:15px;" id="sublistItem_'.$row2{"id"}.'">';
echo '<img src="../testajax/arrow.png" alt="move" width="16" height="16" class="handle" /> '.$row2{"titel"}.'<br />';
echo '</li>';
}
echo '</ul></li>';
}
echo '</ul>';
echo '<div id="info"></div>';
?>
</body>
</html>
Code of process-sortable.php:
<?php
include("../ond/inc/php/connect.php");
if(isset($_GET['listItem']))
{
foreach ($_GET['listItem'] as $position => $item)
{
$sql = "UPDATE `paginas_test` SET `position` = $position WHERE `id` = $item";
mysql_query($sql);
}
}
if(isset($_GET['sublistItem']))
{
foreach ($_GET['sublistItem'] as $position2 => $item2)
{
$sql = "UPDATE `paginas_test` SET `position` = $position2 WHERE `id` = $item2";
mysql_query($sql);
}
}
print_r ($sql);
?>
check this one: jQuery unexpected sortable behaviour