Javascript 在php中从ajax获取数据
我是ajax新手,遇到了一个问题。我想将结果列表中单击的元素的id发布到另一个页面(在localhost中)。当我想从ajax数据中获取发布的id时,我会得到未定义的索引id(即$id=$\u POST['id'])。我怎样才能解决这个问题 我感谢任何帮助或建议 index.phpJavascript 在php中从ajax获取数据,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我是ajax新手,遇到了一个问题。我想将结果列表中单击的元素的id发布到另一个页面(在localhost中)。当我想从ajax数据中获取发布的id时,我会得到未定义的索引id(即$id=$\u POST['id'])。我怎样才能解决这个问题 我感谢任何帮助或建议 index.php <section id= "searchbar"> <form method="get" action="index.php" name="myForm" id="myForm"&g
<section id= "searchbar">
<form method="get" action="index.php" name="myForm" id="myForm">
<h3>Search</h3>
<table>
<tr>
<td><input id="wilaya" type="text" name="wilaya" class="resizedTextbox" placeholder="Choose your Wilaya"></td>
<td><input id="surface" type="text" name="surface" class="resizedTextbox" placeholder="Choose the surface"></td>
<td><input id="search" type="submit" class="resizedTextbox" value="Search"></td>
</tr>
</table>
</form>
</section>
<section id="resultlist">
<table width="100%" height="90%" id="table1">
<thead>
<tr>
</tr>
</thead>
<?php
$array[] = null;
$i = 1;
while ($results = mysql_fetch_assoc($result)) {
?>
<tr>
<div class="round-button">
<!--echo '<img name= "house" class= "imagehouse" src="images/' . $results['photo'] . '" id=' . $results['id'] . ''-->
<?php echo "<img src= images/" . $results['photo'] . " width='50' id=" . $results['id'] . " />"; ?>
<td id="nametable" width="10%"> <?php echo $results['name']; ?></td>
<td id="wilayatable"width="10%"><?php echo $results['wilaya']; ?></td>
<td width="10%"><?php echo $results['surface'] . " m²"; ?></td>
</tr>
<?php
$i++;
}
}
?>
</table>
</section>
<?php
//extract data from the post
//set POST variables
$url = 'http://localhost/testcurl.php';
$fields = array(
'wilaya' => urlencode($_POST['wilaya']),
'id' => urlencode($_POST['id']),
);
//url-ify the data for the POST
foreach ($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
<?php
var_dump($_POST['id']);
?>
testcurl.php在这里,我向服务器发送了一个post-the-id
<?php
$url = "http://localhost/testcurlserver.php";// where you want to post data
$wilaya = isset($_POST['wilaya']);
$id = isset($_POST['id']);
$fields = array(
'wilaya' => urlencode($_POST['wilaya']),
'id' => urlencode($_POST['id']),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
var_dump($output); // show output
?>
请在代码顶部键入
if (! is_array($_POST)) {
$_POST = json_decode(file_get_contents('php://input'), true);
}
在尝试之后,我现在找到了一个解决方案,这就是为什么我想在这里分享它。我使用类似($.post)的ajax帖子发布id,并在id为'content'的div中获取响应
<div id="content" style ="border: 1px solid red;">
</div>
<script type="text/javascript">
$('img').click(function () {
alert(this.id);
var id = this.id;
$.post('testcurlserver.php', {'id': id}, function (data) {
$('#content').html(data);
});
});
$('img')。单击(函数(){
警报(this.id);
var id=this.id;
$.post('testcurlserver.php',{'id':id},函数(数据){
$('#content').html(数据);
});
});
@Xatenev在那之后不是3行!我在index.php中得到了未定义的索引,在这里我制作了curl post,我将添加通知。@Jeff你说的在那之后三行是什么意思?“不是在那之后三行!”是对删除的评论的回答。这里'id'=>urlencode($\u POST['id'])
您不检查$\u POST['id']
isset@Jeff是的,你说得对!与此同时,我找到了一个解决方案,并将其发布在这里。谢谢:)在我的curl请求中,我没有将数据类型定义为json,或者是默认值?
Notice: Undefined index: id in C:\xampp\htdocs\index.php on line 122
Notice: Undefined variable: fields_string in C:\xampp\htdocs\index.php on line 127
Notice: Undefined variable: fields_string in
C:\xampp\htdocs\testcurl.php on line 14 string(0) "" it worked Notice:
Undefined variable: output in C:\xampp\htdocs\testcurl.php on line 31
NULL
Notice: Undefined index: id in C:\xampp\htdocs\index.php on line
147 NULL
if (! is_array($_POST)) {
$_POST = json_decode(file_get_contents('php://input'), true);
}
<div id="content" style ="border: 1px solid red;">
</div>
<script type="text/javascript">
$('img').click(function () {
alert(this.id);
var id = this.id;
$.post('testcurlserver.php', {'id': id}, function (data) {
$('#content').html(data);
});
});