admin管理员组文章数量:1332394
I wish to call data from another page to my existing page through ajax. For this i have the following code
<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>
Till console.log (id) code is working, i am getting value inside id but i am not able to run the register.php page. I wish to carry the id to register.php page, run some code there and print its result under #msg, can anyone please tell how i can correct my ajax code
I wish to call data from another page to my existing page through ajax. For this i have the following code
<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>
Till console.log (id) code is working, i am getting value inside id but i am not able to run the register.php page. I wish to carry the id to register.php page, run some code there and print its result under #msg, can anyone please tell how i can correct my ajax code
Share Improve this question asked Dec 22, 2015 at 4:42 user5404107user5404107 7- what is console.log(html) in your success callback ? – Ludovic C Commented Dec 22, 2015 at 4:43
- @Ludo i am not able to check console.log(html) as i am not getting any result – user5404107 Commented Dec 22, 2015 at 4:45
- You can't do this like this. Passing value to Other page by ajax. how can you get it in other page? – Parth Trivedi Commented Dec 22, 2015 at 4:45
-
5
you should do
data : {id:id}
. then get id onregister.php
the variable$_POST['id']
– roullie Commented Dec 22, 2015 at 4:45 - 1 @roullie thanks your advice has helped in solving the issue, now i am getting the result – user5404107 Commented Dec 22, 2015 at 4:49
6 Answers
Reset to default 2You need to send data like data : {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>
Hope this will solve your issue.
You should pass data in key/value
format. Now, you can check your data in register.php
by printing POST
array.
You can pass data in plain string like I shown in example also you can pass JSON
object.
- "key1=value1&key2=value2...."
{ 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>
Check the values passed method
<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>
Change you $.ajax()
function like this:
$.ajax({
type: 'POST',
url: 'register.php',
data: {
id: id
},
success: function(response)
{
$('#msg').html(response);
}
});
I hope it helps you...
data.php
echo $id = $_GET['id'];
echo $name = $_GET['name'];
echo $email = $_GET['email'];
Hope this will solve your issue.
Try this;
<script src="//ajax.googleapis./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);
}
});
本文标签: javascriptpass value from one page to another using ajaxStack Overflow
版权声明:本文标题:javascript - pass value from one page to another using ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742282563a2446361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论