Fetch dynamically selected data from database using ajax php

Multi tool use


Fetch dynamically selected data from database using ajax php
My view.php page comes from another page by getting a unique id which is serial. Now in my view.php page I want to show that specified serial no data from charts.php. I have done my code by myself. It is fetching the data. But not that selected serial no. How can I solve this
view.php
<?php
if(isset($_GET['serial'])){
$serial = $_GET['serial'];
?>
<html>
<div class="container" id="output"></div>
</html>
<script>
$(document).ready(function(){
function getData(){
$.ajax({
type: 'POST',
url: 'charts.php',
success: function(data){
$('#output').html(data);
}
});
}
getData();
setInterval(function () {
getData();
}, 1000); // it will refresh your data every 1 sec
});
</script>
charts.php
<?php
$sql = mysqli_query($con,"SELECT * FROM criminal WHERE rand = '$serial'");
while($row = mysqli_fetch_assoc($sql)){
?>
Please help.
$serial
how can I send $serial variable o the php script in ajax
– Mehedi Hasan Siam
yesterday
You need a
data:
property for the AJAX request.– Jay Blanchard
yesterday
data:
data: $("#whatever_your_form_id_is").serialize()
– IRGeekSauce
yesterday
DOH! I saw 'POST' and thought form. My bad. Didn't notice the query string at the top.
– IRGeekSauce
yesterday
2 Answers
2
You want to place your $serial variable within your url. This is known as a query string.
$(function() {
function getData(){
$.ajax({
type: 'POST',
dataType: 'JSON',
url: 'charts.php?serial=<?= $serial?>', //<-- RIGHT HERE
success: function(data){
$('#output').html(data);
}
});
}
})
Then you'll GET the data you just sent from ajax. It'll look like your first GET variable. You'll use that variable in your query.
In your php:
<?php
$your_variable = $_GET['serial'];
$sql = mysqli_query($con,"SELECT * FROM criminal WHERE rand = '$your_variable'");
while($row = mysqli_fetch_assoc($sql)){
$variable_to_send = $row['serial']; //<--- Whatever your column name is
}
echo json_encode($variable_to_send);
?>
PDO VERSION
As suggested by @JayBlanchard it's highly advisable you do some research on PDO. It is much safer.
I'll give you a PDO example:
$serial = $_GET['serial']; //The variable you're sending over from view.php
$hostname = 'your_hostname';
$username = 'your_username';
$password = 'your_passwd';
$dbname = 'your_db_name';
$pdo = new PDO("mysql:host=$hostname;$dbname=$dbname", $username, $password); //Create a new PDO object
$stmt = $pdo->prepare("SELECT * FROM criminal WHERE rand = :rand"); //prepare the query for execution
$stmt->bindValue(':rand', $serial); //bind your variable to your query
$stmt->execute(); //Run it
$result = $stmt->fetchColumn(); //Get a single column. No while loop.
echo json_encode($result); //Echo it back to your ajax function
I tested this against my own database and it displayed results on my screen with no errors (using my own values of course).
Little Bobby says your script is at risk for SQL Injection Attacks.. Even escaping the string is not safe! Learn about prepared statements for MySQLi and consider using PDO, it's really pretty easy.
– Jay Blanchard
yesterday
Yeah. We gotta remember Bobby Tables. He causes a lot of trouble. I should've written my answer in that context. I just wasn't thinking about it. Thanks for mentioning that.
– IRGeekSauce
yesterday
Edited answer to add PDO version.
– IRGeekSauce
yesterday
}
data: {serial: "echo with php the variable"},
$_POST['serial']
Like this: data: ({serial: "<?php echo $row['rand'] ?>"}),
– Mehedi Hasan Siam
yesterday
after url: 'charts.php', on the next line data: {serial: "<?php echo $serial; ?>"},
– oalexandru
yesterday
IRGeekSauce`s version, using get to send the serial is good too.
– oalexandru
yesterday
@Jay Blanchard there is nothing about a form in what i said.
– oalexandru
22 hours ago
No, but you said "send the serial". My bad, I misunderstood your meaning
– Jay Blanchard
22 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You never send
$serial
to the PHP script.– Jay Blanchard
yesterday