I'm trying post data to PHP file but i can't receive any data from PHP file. Let me add codes. This is my jQuery function:
我正在尝试将数据发布到PHP文件,但我无法从PHP文件接收任何数据。我来添加代码。这是我的jQuery函数:
$(document).ready(function () {
$(function () {
$('a[class="some-class"]').click(function(){
var somedata = $(this).attr("id");
$.ajax({
url: "foo.php",
type: "POST",
data: "id=" + somedata,
success: function(){
$("#someid").html();
},
error:function(){
alert("AJAX request was a failure");
}
});
});
});
});
This is my PHP file:
这是我的PHP文件:
<?php
$data = $_POST['id'];
$con = mysqli_connect('localhost','root','','somedatabase');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"database");
$sql="SELECT * FROM sometable WHERE id = '".$data."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo $row['info'];
}
mysqli_close($con);
?>
This what i have in HTML file:
这就是我在HTML文件中的内容:
<p id="someid"></p>
<a href="#page2" class="some-class" id="1">Data1</a>
<a href="#page2" class="some-class" id="2">Data2</a>
Note: This website is horizontal scrolling and shouldn't be refreshed. When i'm clicking links (like Data1) it's going to another page without getting data from PHP file
注意:此网站是水平滚动,不应刷新。当我点击链接(如Data1)时,它将转到另一个页面而不从PHP文件中获取数据
5 个解决方案
#1
2
You have a few problems:
你有一些问题:
- You are not using the data as mentioned in the other answers:
success: function(data){ $("#someid").html(data); },
- You are not cancelling the default click action so your link will be followed:
$('a[class="some-class"]').click(function(e){ e.preventDefault(); ...
; - As the id's are integers, you can use
data: "id=" + somedata,
although sending an object is safer in casesomedata
contains characters that need to be escaped:data: {"id": somedata},
; - You have an sql injection problem. You should cast the variable to an integer or use a prepared statement:
$data = (int) $_POST['id'];
; - As also mentioned in another answer, you have two
$(document).ready()
functions, one wrapping the other. You only need one.
您没有使用其他答案中提到的数据:success:function(data){$(“#someid”)。html(data); },
您没有取消默认点击操作,因此您的链接将被遵循:$('a [class =“some-class”]')。click(function(e){e.preventDefault(); ...;
由于id是整数,你可以使用数据:“id =”+ somedata,尽管如果somedata包含需要转义的字符,发送对象会更安全:data:{“id”:somedata} ,;
你有一个SQL注入问题。您应该将变量强制转换为整数或使用预准备语句:$ data =(int)$ _POST ['id'] ;;
另外在另一个答案中提到,你有两个$(document).ready()函数,一个包装另一个。你只需要一个。
#2
1
success: function(){
$("#someid").html();
},
should be:
success: function(data){
$("#someid").html(data);
},
#3
1
You should add parameter in success
您应该成功添加参数
success: function(data){ //Added data parameter
console.log(data);
$("#someid").html(data);
},
The data get the values what you echo in PHP end.
数据获取您在PHP结束时回显的值。
#4
1
This:
success: function(data){
$("#someid").html(data);
},
and you have two document ready, so get rid of:
你准备好了两个文件,所以摆脱:
$(document).ready(function () { ...
});
#5
0
data: "id=" + somedata,
Change it to:
将其更改为:
data: { id : somedata }
#1
2
You have a few problems:
你有一些问题:
- You are not using the data as mentioned in the other answers:
success: function(data){ $("#someid").html(data); },
- You are not cancelling the default click action so your link will be followed:
$('a[class="some-class"]').click(function(e){ e.preventDefault(); ...
; - As the id's are integers, you can use
data: "id=" + somedata,
although sending an object is safer in casesomedata
contains characters that need to be escaped:data: {"id": somedata},
; - You have an sql injection problem. You should cast the variable to an integer or use a prepared statement:
$data = (int) $_POST['id'];
; - As also mentioned in another answer, you have two
$(document).ready()
functions, one wrapping the other. You only need one.
您没有使用其他答案中提到的数据:success:function(data){$(“#someid”)。html(data); },
您没有取消默认点击操作,因此您的链接将被遵循:$('a [class =“some-class”]')。click(function(e){e.preventDefault(); ...;
由于id是整数,你可以使用数据:“id =”+ somedata,尽管如果somedata包含需要转义的字符,发送对象会更安全:data:{“id”:somedata} ,;
你有一个SQL注入问题。您应该将变量强制转换为整数或使用预准备语句:$ data =(int)$ _POST ['id'] ;;
另外在另一个答案中提到,你有两个$(document).ready()函数,一个包装另一个。你只需要一个。
#2
1
success: function(){
$("#someid").html();
},
should be:
success: function(data){
$("#someid").html(data);
},
#3
1
You should add parameter in success
您应该成功添加参数
success: function(data){ //Added data parameter
console.log(data);
$("#someid").html(data);
},
The data get the values what you echo in PHP end.
数据获取您在PHP结束时回显的值。
#4
1
This:
success: function(data){
$("#someid").html(data);
},
and you have two document ready, so get rid of:
你准备好了两个文件,所以摆脱:
$(document).ready(function () { ...
});
#5
0
data: "id=" + somedata,
Change it to:
将其更改为:
data: { id : somedata }