将数据从一个PHP页面传递到另一个PHP页面
我有一个我正在制作的程序,它从数据库中获取前5行,并在点击我制作的网站上的按钮时将其呈现给用户。我能够在单击按钮的页面上打印数据库数据,但我希望它打印到我创建的另一个页面上。以下是我的主页代码,其中包含按钮和获取数据库信息的PHP代码(文件名为file1.PHP):将数据从一个PHP页面传递到另一个PHP页面,php,jquery,html,mysql,Php,Jquery,Html,Mysql,我有一个我正在制作的程序,它从数据库中获取前5行,并在点击我制作的网站上的按钮时将其呈现给用户。我能够在单击按钮的页面上打印数据库数据,但我希望它打印到我创建的另一个页面上。以下是我的主页代码,其中包含按钮和获取数据库信息的PHP代码(文件名为file1.PHP): 测试sqldb 函数myAjax(){ $.ajax({type:'POST', 数据:{}, url:'query.php',// 最后,我希望传输数据的文件在file2.php中给出了一个错误,称为“Undefined in
测试sqldb
函数myAjax(){
$.ajax({type:'POST',
数据:{},
url:'query.php',//
最后,我希望传输数据的文件在file2.php中给出了一个错误,称为“Undefined index:dbNames”(这是第二个文件中给出错误的那一行)
没有任何帖子能够帮助我,因此对此的任何帮助都将不胜感激。首先,您不应该将ajax请求嵌入到函数中。它剥夺了所谓“事件”的大部分灵活性 您希望在用户单击按钮时请求数据:
<script type = "text/javascript">
$(function() {
$('#normalnames').click(function(){
$.ajax( {
type : 'POST',
data : { type: 'normalnames' },
url : 'query.php',
success: function ( data ) {
$('#dbResult').html(data);
},
error: function ( xhr ) {
alert( "error" );
}
});
});
$('#specialnames').click(function(){
//etc
});
});
</script>
<button id="normalnames" type="button" class="btn btn-default">Normal names</button>
<button id="specialnames" type="button" class="btn btn-default">Normal names</button>
$(函数(){
$('#normalnames')。单击(函数(){
$.ajax({
键入:“POST”,
数据:{type:'normalnames'},
url:'query.php',
成功:功能(数据){
$('#dbResult').html(数据);
},
错误:函数(xhr){
警报(“错误”);
}
});
});
$(“#特殊名称”)。单击(函数(){
//等
});
});
普通名称
普通名称
在PHP方面:
<?php
if(isset($_POST['type'])){
if($_POST['type'] == 'normalnames'){
// make normal names query
} elseif($_POST['type'] == 'specialnames'){
// make special names query
} else {
die('incorrect request');
}
} else {
die('unknown request');
}
?>
现在,您希望在dbresponse div中登记数据:
while($row = mysqli_fetch_assoc($result)){
$data[] = "<li>".$row['Name']."</li>";
}
echo '<ul>' . implode($data) . '</ul>';
while($row=mysqli\u fetch\u assoc($result)){
$data[]=“”$row['Name']。“ ”;
}
回显“”。内爆($data)。“
”;
这就是它的要点,也许最好使用json(echo json_encode($data)
,不带html标记)返回,因为javascript可以遍历它并将其附加到列表中
要在另一个页面上使用这些数据,请使用。我已经看过了这些帖子,但没有任何效果。如果我没有用我的问题查看流行的帖子,我就不会发布这个问题。你没有通过Ajax传递任何数据。这是我不明白如何在谷歌搜索Ajax时将变量发送到php脚本的部分。你应该ld find something如果您没有将数据附加到post请求中,
数据:{}
为空,而它应该是数据:{'dbNames':'My data I want attach'}
。
<script type = "text/javascript">
$(function() {
$('#normalnames').click(function(){
$.ajax( {
type : 'POST',
data : { type: 'normalnames' },
url : 'query.php',
success: function ( data ) {
$('#dbResult').html(data);
},
error: function ( xhr ) {
alert( "error" );
}
});
});
$('#specialnames').click(function(){
//etc
});
});
</script>
<button id="normalnames" type="button" class="btn btn-default">Normal names</button>
<button id="specialnames" type="button" class="btn btn-default">Normal names</button>
<?php
if(isset($_POST['type'])){
if($_POST['type'] == 'normalnames'){
// make normal names query
} elseif($_POST['type'] == 'specialnames'){
// make special names query
} else {
die('incorrect request');
}
} else {
die('unknown request');
}
?>
while($row = mysqli_fetch_assoc($result)){
$data[] = "<li>".$row['Name']."</li>";
}
echo '<ul>' . implode($data) . '</ul>';