使用ajax将值从一个页面传递到另一个页面
问题描述:
我希望通过ajax从其他页面调用数据到我现有的页面。为此,我有以下代码使用ajax将值从一个页面传递到另一个页面
<script>
$(document).ready(function()
{
$(".link").on("click", function(e)
{
e.preventDefault();
var id = $(this).data("id");
console.log (id); // i am getting value in this id
$.ajax({
type : "post",
url : "register.php",
data : id,
cache : false,
success : function(html)
{
$('#msg').html(html);
}});
});
});
</script>
<a class='link' data-id="$idd" >TEXT</a>
直到执行console.log(ID)代码工作,我得到的内部ID值,但我不能够运行register.php页面。我希望携带id到register.php页面,在那里运行一些代码并在#msg下打印它的结果,谁能告诉我如何纠正我的ajax代码
答
您应该以key/value
格式传递数据。现在,您可以通过打印POST
数组来检查register.php
中的数据。
您可以传递纯数据串中的数据,就像我在示例中所示的那样,您也可以通过JSON
对象。
- “键1 =值&键2 =值......”
-
{K1:V1,K2:V2,......}
<script> $(document).ready(function() { $(".link").on("click", function(e) { e.preventDefault(); var id = $(this).data("id"); console.log (id); // i am getting value in this id $.ajax({ type : "post", url : "register.php", data : "id="+id, //you can pass value like this. cache : false, success : function(html) { $('#msg').html(html); } }); }); }); </script>
答
你需要发送数据,如数据:{id:id}
<script>
$(document).ready(function()
{
$(".link").on("click", function(e)
{
e.preventDefault();
var id = $(this).data("id");
console.log (id); // i am getting value in this id
$.ajax({
type : "post",
url : "register.php",
data : {id: id},
cache : false,
success : function(html)
{ alert(html);
$('#msg').html(html);
}});
});
});
</script>
希望这将解决您的问题。
答
检查的值传递方法
<script>
$(document).ready(function()
{
$(".link").on("click", function(e)
{
e.preventDefault();
var id = $(this).data("id");
//console.log (id);
$.ajax({
type : "post",
url : "register.php",
//data : id,
data:{'id':id},//values to be passed similarly
cache : false,
success : function(html)
{
$('#msg').html(html);
}});
});
});
</script>
答
更改您$.ajax()
功能是这样的:
$.ajax({
type: 'POST',
url: 'register.php',
data: {
id: id
},
success: function(response)
{
$('#msg').html(response);
}
});
我希望它可以帮助你......
答
试试这个;
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
$.ajax({
type:'GET',
url:'data.php',
data: {
"id": 123,
"name": "abc",
"email": "[email protected]"
},
success: function(ccc){
alert(ccc);
$("#result").html(ccc);
}
});
答
data.php
echo $id = $_GET['id'];
echo $name = $_GET['name'];
echo $email = $_GET['email'];
希望这将解决您的问题。
什么是您的成功回调中的console.log(html)? – Ludo
@卢多我无法检查console.log(html),因为我没有得到任何结果 – pp1989
你不能这样做。通过ajax将值传递给其他页面。你怎么能在其他页面获得它? –