How can you pass a sql query through ajax? - php

I have sql query I need pass through to an ajax file.
$qry = mysqli_query($this->con,"SELECT * FROM products");
I have contained the variable in html such as:
<input type="button" data-qry="'.$qry.'" id="button" value="click">
I use the ajax jquery code below to pass it through:
$('#button').click(function(){
array = $(this).attr('data-qry');
$.ajax({
type : 'POST',
dataType : 'json',
url : 'ajax.php',
data : 'qry='+qry,
success : function(data) {
$('#result').html(data);
}
});
});
I want to be able to open ajax.php and perform
$qry = $_POST['qry'];
$row = mysqli_fetch_assoc($qry);
Of course this is not working. How can I pass the query through, does it need to be done through JSON some how?

At the start of your button you assign the value to the variable array
array = $(this).attr('data-qry');
But you send qry with the ajax
data : 'qry='+qry,
The way you fix this is by sending the variable you assign the value to

Related

how would I pull the data out of the database and re-check the checkboxes that were originally checked and submitted?

I entered checkbox values into the database using the code below. When a user wants to view the checked boxes at a later time, how would I pull the data out of the database and re-check the checkboxes that were originally checked and submitted?
From the code below the data gets entered like this: DATA1|DATA2|DATA3|
var checkbox_value = "";
$(":checkbox").each(function () {
var ischecked = $(this).is(":checked");
if (ischecked) {
checkbox_value += $(this).val() + "|";
}
});
Now how to I take DATA1|DATA2|DATA3| and re-check the corresponding checkboxes?
Here is how I'm getting other data and re-displaying it from normal input text boxes:
$.ajax({
url: 'ajax/example/example.php',
data: "findval="+carrier+"&column="+column,
dataType: 'json',
success: function(data)
{
var auto_id = data['id'];
var auto_name = data['name'];
var auto_address = data['address'];
var auto_trailer_types = data['trailer_types'];
$('#output_autocomplete_forms').html("<form id='example' class='form-horizontal'><input type='hidden' name='auto_id' id='auto_id' class='form-control' value="+auto_id+">......<div class='form-group'>
<div class='checkbox'><label><input type='checkbox' name='trailer_kinds[]' value='DATA1'>DATA1</label></div>
<div class='checkbox'><label><input type='checkbox' name='trailer_kinds[]' value='DATA2'>DATA2</label></div>
<div class='checkbox'><label><input type='checkbox' name='trailer_kinds[]' value='DATA3'>DATA3</label></div>
var auto_trailer_types = data['trailer_types'];
Now how do I take DATA1|DATA2|DATA3| and re-check the corresponding checkboxes?
So, I am guessing that the string stored in the database is something like 0|0|1|0, and you need to restore the checkboxes to that state.
As has been said, you can use AJAX for that. First, you need a trigger to launch the AJAX routine -- a button click, usually:
<button id="mybutt">Update Checkboxes</button>
Your AJAX routine will look something like this:
var rowID = $(this).closest('tr').attr('id');
alert(rowID); //this datum must allow you to identify the row in the database
$.ajax({
type: 'post',
url: 'another_php_file.php',
data: 'id=' +rowID,
success: function(recd){
var arrCB = recd.split('|');
for (var n=0; n<arrCB.length; n++){
var z = n+1;
$('#cb'+ z).val( arrCB[n] );
}
}
});
another_php_file.php:
<?php
$id = $_POST['id'];
$pseudo_query = "SELECT `field_name` FROM `table_name` WHERE `id` = '$id' ";
echo $pseudo_query_result;
The pseudo_query_result that you echo should be your original 0|0|1|0. The PHP echo sends that datum back to the jQuery AJAX routine's success function. Important: the received data is not available outside that success function. You must do what you want with that data inside the success function, as shown in above example.
In above code, .split() was used to turn the 0|0|1|0 string into an array, and then we use a for loop (or even just manually code each checkbox update individually).
Use ajax to resolve this issue. But before try to encode your php query response in JSON
$.ajax({
url: "test.php" //Your request URI
}).done(function(data) {
//data (you can name it whatever you want) is the data that you get from your request.
//eg. if(data.checkbock){ //do something }
});
More informations here

passing a php array through jquery ajax

I want to pass an array through ajax, but not sure how I would do this.
<input type="button" data-array="'.$array.'" id="button" value="click">
I know I cant put arrays in html, but I couldnt think of how else to explain what I am trying to accomplish.
$('#button').click(function(){
array = $(this).attr('data-array');
$.ajax({
type : 'POST',
dataType : 'json',
url : 'ajax.php',
data : 'array='+array,
success : function(data) {
$('#result').html(data);
}
});
});
So I then want to pass that array variable, $array, through to ajax.php. Is this even possible, if so how is it done? I am guess something to do with json?
Instead of storing the array in html you could store it in a session variable and call it later when needed.
$_SESSION['dataArray'] = $array;

Modifying jquery $.ajax() to handle received information by $_POST in php file

Before asking my question, following is my html code(Edited):
<?php
$conn = //connected to db successfully.
$sql = "SELECT t1.column1 AS Column_1 FROM table1 t1";
$rs = mysqli_query($conn,$sql);
$rows= mysqli_fetch_assoc($rs);
do{
?>
<button name="my_button" data-id="<?php echo $rows['Column_1']; ?>" type="submit" onclick="my_function()">Click Me</button>
<?php }while($rows = mysqli_fetch_assoc($rs)); ?>
Following is the jquery ajax in my html header tag:
function my_function(){
$.ajax({
url : 'my_file.php',
type : 'POST',
data: {item_id : $('button[data-id]').attr('data-id')},
success : function(data){
alert(data);
}
});
}
Now I have a little general question:
Being very new to jquery and specially jquery ajax. What should I include in "data:" in jquery $.ajax() or how should I modify $.ajax() so I could both handle my button data-id tag value (which is 1 here) and my text input value/name by $_POST value in 'my_file.php'.
I receive 'undefined index item_id' alert error for php file using $_POST["item_id"] when I have { item_id : $(this).data("id")} in $.ajax data. I appreciate your guiding me with this issue. Thanks in advance.
Edit 1: I changed the 'data' but the problem again persists.
Edit 2: I have edited my html doc. In my real html doc, I fetch the items with buttons in my html file by mysqli_fetch_assoc from my mysql database. (Please check my edited html code). The problem is, no matter which button I click on, the data-id that is inserted into my mysql table2 using 'my_file.php' file, is always one specific value. What should I do?
You can use $('button[data-id]').attr('data-id') instead of $(this).data("id"). Try this
function my_function(){
$.ajax({
url : 'my_file.php',
type : 'POST',
data: {item_id : $('button[data-id]').attr('data-id'), field1 : $('#field1').val()},
success : function(data){
alert(data);
}
});
}
And in my_file.php get the POST values like this
$item_id = isset($_POST['item_id']) ? $_POST['item_id'] : '';
$field1 = isset($_POST['field1']) ? $_POST['field1'] : '';
echo $item_id;
you could catch the button click and do a return: false; then submit the data.
to send your textbox data you can go as below:
var field = $('#field1').val();
then
data: field,
you can also want to take a look at serialize function

How can i change an SQL Query using Ajax?

I'm noob in PHP, and JQuery, and i'm trying to do a pagination without changing the page.
I could submit a form, but i want to change the value from the limit without doing a submission.
My Query(i want to change the Limit):
$query = "SELECT * FROM events WHERE event_day = '".$list_day."' AND event_month = '".$month."' AND event_year = '".$year."' LIMIT 0,3";
I tried to do that -
First, i created a form with the limit value:
$limit = 0;
<form id="more" method="GET">
<button type="button" id="more_btn" value="<?= $limit ?>" class="c-button-1"><</button>
</form>
And then, i tried to get the value from the button, using Ajax
$(function(){
$(document).on("click","#more_btn",function() {
var value = $(this).attr('value');
$.ajax( {
type: 'get',
url: "modules/groups/calendar.php",
data: $('#more').serialize(),
success: function( response ) {
}
});
});
});
I was thinking about getting the value from the button, and change it in JQuery:
$query = "SELECT * FROM events WHERE event_day = '".$list_day."' AND event_month = '".$month."' AND event_year = '".$year."' LIMIT ".$limit.",3";
you don't even need the <form> there. when you are working with ajax you generally don't need any forms unless you want to do some hybrid coding for browsers with javascript turned off, or for serializing them (what you tried).
but in your case it doesn't really make any sense since your form just contains one button.
and you are already trying to get the value from the button but don't use it later on for some reason. you don't need to use .attr() there, since jQuery got the .val()-function for HTML elements with the value attribute.
so i'd do it like this:
$(function(){
$(document).on("click","#more_btn",function() {
var value = $(this).val();
$.ajax( {
type: 'get',
url: "modules/groups/calendar.php",
data: {
my_button_value : value
}
success: function( response ) {
}
});
});
});
$_GET['my_button_value'] then will receive the value in your PHP script.
in your question you also serialized the form which returns a URL-encoded string ("GET-style") which you can't just use in the data-attribute of the $.ajax function. you need an object there as i did in my code.

filter Query data with var from html value input

if i have this input on html code
<input value="0001" name="call" id="call">
how i call that value in php script without submit? like event onBlur or anything ??
the php file have query something like this :
select * from table where field = (input html value)
you need to use Ajax for that.
<script>
$(document).ready(function()
{
var value = $("#call").val();
$.ajax({
url: "you file path where your php code is written to fetch value from database",
type:"POST",
data:{"call":value},
success:function(result){alert(result);}
})
});
</script>
and on php file.
<?php
write code for sql connection ;
$feild_value = $_POST["call"];
// you can use this $feild_value variable in query;
select * from table where field = $feild_value ;
//echo query result here
echo $query_result;
die;
?>
If you want to send this value to your PHP script when event Blur is triggered, then try this
<input value="0001" name="call" id="call">
<script type="text/javascript">
$('#call').on('blur',function(){
var value = $(this).val();
$.ajax({
url: "http://website.com/index.php",
type: "POST",
data: {"call": value},
success: function(data){
console.log(data);
}
});
});
</script>
To set input value to PHP session you will need to run ajax request anyway, see this question Set Session variable using javascript in PHP

Categories