[英]How to pass PHP variable to AJAX URL
Hello guys I am new to javascript I am trying to send php variable to AJAX url file but i was unable to do it.大家好,我是 javascript 新手,我正在尝试将 php 变量发送到 AJAX url 文件,但我无法做到。 I don't know where the problem actually arise.
我不知道问题实际上出在哪里。 your help will be highly appreciated
您的帮助将不胜感激
Here i want to send the below PHP Variable "$cheque" to the AJAX URL Page cheque_select.php在这里,我想将以下 PHP 变量“$cheque”发送到 AJAX URL 页面 cheque_select.php
<php? $cheque = '78964Y' ?>
$(document).ready(function(){
function fetch_data()
{
$.ajax({
url:"cheque_select.php",
method:"POST",
dataType:"json",
success:function(data)
{
var html = '';
for(var count = 0; count < data.length; count++)
{
html += '<tr>';
html += '<td><input type="checkbox" id="'+data[count].id+'" data-cheque_no="'+data[count].cheque_no+'" data-id="'+data[count].id+'" data-name="'+data[count].name+'" data-sum="'+data[count].sum+'" data-account="'+data[count].account+'" class="check_box" /></td>';
html += '<td>'+data[count].cheque_no+'</td>';
html += '<td>'+data[count].id+'</td>';
html += '<td>'+data[count].name+'</td>';
html += '<td>'+data[count].sum+'</td>';
html += '<td>'+data[count].account+'</td></tr>';
}
$('tbody').html(html);
}
});
}
fetch_data();
This is my cheque_select.php i want to fetch data from mysql by the above variable这是我的 cheque_select.php 我想通过上述变量从 mysql 获取数据
<?php
include('connection.php');
$query = "select * FROM entry where entry.bank= $cheque";
$statement = $connect->prepare($query);
if($statement->execute())
{
while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
$data[] = $row;
}
echo json_encode($data);
}
?>
Simply add a "data" value in the ajax/jquery request you are firing.只需在您触发的 ajax/jquery 请求中添加一个“数据”值。 This will send data in the form of a POST value to the page that is receiving your ajax request.
这将以 POST 值的形式将数据发送到接收您的 ajax 请求的页面。 This is the revised "fetch_data" function after adding the data you wish to send:
这是添加要发送的数据后修改后的“fetch_data”函数:
function fetch_data()
{
$.ajax({
url:"cheque_select.php",
method:"POST",
dataType:"json",
data:{"cheque":"<?= $cheque ?>"},
success:function(data)
{
var html = '';
for(var count = 0; count < data.length; count++)
{
html += '<tr>';
html += '<td><input type="checkbox" id="'+data[count].id+'" data-cheque_no="'+data[count].cheque_no+'" data-id="'+data[count].id+'" data-name="'+data[count].name+'" data-sum="'+data[count].sum+'" data-account="'+data[count].account+'" class="check_box" /></td>';
html += '<td>'+data[count].cheque_no+'</td>';
html += '<td>'+data[count].id+'</td>';
html += '<td>'+data[count].name+'</td>';
html += '<td>'+data[count].sum+'</td>';
html += '<td>'+data[count].account+'</td></tr>';
}
$('tbody').html(html);
}
});
}
Then on the page that is receiving the request, you would get the cheque value by saying php $_POST['cheque']
.然后在接收请求的页面上,您可以通过说
php $_POST['cheque']
来获取检查值。 Also one mistake I noticed is you have mistyped your opening PHP tag.我注意到的另一个错误是您输入了错误的 PHP 开头标签。 You have wrote
<php?
你写过
<php?
. . The correct way is
<?php
.正确的方法是
<?php
。
One more thing - you haven't prepared your statement correctly in the PHP page.还有一件事 - 您没有在 PHP 页面中正确地准备好您的语句。 This is how you should prepare it:
这是你应该如何准备它:
$pre_stmt = "SELECT * FROM entry WHERE entry.bank=?";
$stmt = $conn->prepare($pre_stmt);
$stmt->bind_param(
"i", # if cheque is a numerical value keep it "i" otherwise change it to "s" if it is a string
$cheque
);
$stmt->execute();
#do whatever success condition
Preparing statements like this prevents SQL injection准备这样的语句可以防止 SQL 注入
Add data
in your AJAX request like -在 AJAX 请求中添加
data
,例如 -
<?php $cheque = '78964Y' ?>
$(document).ready(function(){
function fetch_data()
{
$.ajax({
url:"cheque_select.php",
method:"POST",
dataType:"json",
data:{'cheque': "<?php echo $cheque; ?>"},
success:function(data)
{
var html = '';
for(var count = 0; count < data.length; count++)
{
html += '<tr>';
html += '<td><input type="checkbox" id="'+data[count].id+'" data-cheque_no="'+data[count].cheque_no+'" data-id="'+data[count].id+'" data-name="'+data[count].name+'" data-sum="'+data[count].sum+'" data-account="'+data[count].account+'" class="check_box" /></td>';
html += '<td>'+data[count].cheque_no+'</td>';
html += '<td>'+data[count].id+'</td>';
html += '<td>'+data[count].name+'</td>';
html += '<td>'+data[count].sum+'</td>';
html += '<td>'+data[count].account+'</td></tr>';
}
$('tbody').html(html);
}
});
}
fetch_data();
It will send the cheque value to the server end.它将校验值发送到服务器端。
And get it at the PHP end just before the query by adding -并在查询之前通过添加 - 在 PHP 端获取它 -
<?php
include 'connection.php';
$cheque = $_POST['cheque'];
$query = "SELECT * FROM entry WHERE entry.bank = ?";
$statement = $connect->prepare($query);
$statement->execute([$cheque]);
$data = $statement->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.