[英]Use Ajax to POST and GET in the same PHP file (JSON)
I'm trying to reach for a result passing by two steps : 我试图通过两个步骤来达到一个结果:
this what I tried : 这是我尝试的:
<select class="pam" >
<option value=""></option>
<?php
$conn_string = "host=localhost port=5432 dbname=pame user=postgres password=";
$conn = pg_connect($conn_string);
$sql = pg_query($conn, "SELECT * FROM espece order by nom_com ASC ");
while($ligne_liste=pg_fetch_array($sql)) {
echo '<option value="'.$ligne_liste['id_espece'].'">'.$ligne_liste['nom_com']."</option>\n";
}
echo '</select>';
?>
The JS code I used : 我使用的JS代码:
jQuery(document).ready(function($) {
$('select.pam').change(function(){
$.post ('/charta.php',
{id_espece:$('select.pam').val()},
success: function(){
alert('success');
window.location = "http://localhost/charta.php";
}
);
});
});
My PHP file : 我的PHP文件:
<?php
$user_id = isset($_POST["id_espece"])?$_POST["id_espece"]:"";
$user=conn ($user_id) // conn is a function who return an array from database
$js = json_decode( $user, true );
?>
<?php
echo json_encode($js); ?>
the problem here is I get "NULL" for the value of $_POST ('id_espece') even though the value selected from the dropdown list; 问题是即使从下拉列表中选择的值,我也会得到$ _POST('id_espece')的值“ NULL”; but When I replace the success function with :
但是当我将成功函数替换为:
function(res){ $('#txt').html(res); }
It returnes the result i want (JSON output) in the <div id="txt"></div>
but not in the PHP file when it return an error that ` 当它返回错误“`”时,它在
<div id="txt"></div>
但不在PHP文件中返回我想要的结果(JSON输出)。
'undefined index : id_espece'`
'未定义的索引:id_espece'`
The seccond step I want to GET the output of the same PHP file to HTML (div): 我想将同一PHP文件的输出获取到HTML(div)的第二步:
If we supposed that PHP file works and sent a JSON form I tried to GET the result in JS file like this : 如果我们认为PHP文件可以工作并发送了JSON表单,则我尝试像这样在JS文件中获取结果:
$(document).ready(function(){ $.ajax({ url: "http://localhost/charta.php", method: "GET", success: function(data) { console.log(data); var region = []; var sup = []; for(var i in data) { region.push("region " + data[i].nom_reg_12); sup.push(data[i].aq); } var chartdata = { labels: region, datasets : [ { label: 'Superficie(m²)', backgroundColor: 'rgba(77, 214, 104, 0.75)', borderColor: 'rgba(77, 214, 104, 0.75)', hoverBackgroundColor: 'rgba(77, 214, 104, 1)', hoverBorderColor: 'rgba(77, 214, 104, 1)', data: sup } ] }; var ctx = $("#mycanvas"); var barGraph = new Chart(ctx, { type: 'bar', data: chartdata }); }, error: function(data) { console.log(data); } }); });
to be sure that the second step work fine , I replaced $_POST(['id_espece'])
with a default value and the PHP file return a JSON format not empty , so I think that the problem is in the first step , I don't know where is the problem exactly 为确保第二步工作正常,我将
$_POST(['id_espece'])
替换为默认值,并且PHP文件返回的JSON格式不为空,所以我认为问题出在第一步,我不知道不知道问题出在哪里
In order to get a form element in the $_POST
array it must have a name
attribute. 为了在
$_POST
数组中获取表单元素,它必须具有name
属性。 Change your select
element to have a name: 更改
select
元素以使其具有名称:
<select class="pam" name="id_espece">
In addition $.post
doesn't have a success
function as you have written it. 此外,
$.post
没有success
函数,就像您编写的那样。 The format should be this: 格式应为:
$.post ('/charta.php',
{id_espece:$('select.pam').val()},
function(data){
console.log(data);
window.location = "http://localhost/charta.php";
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.