如何使用 Ajax 通过超链接将数据传递到 php 页面
How to pass data through a hyperlink to a php page using Ajax
我正在尝试使用 Ajax
通过获取请求将数据从超链接传递到页面
最初我可以在没有 Ajax 的情况下直接执行此操作,如下所示
<a id="link" href="page.php?id=12&pid=12" >pass data</a>
我在下面的 php
<?php $_GET['id'] ?>
<?php $_GET['pid']?>
现在我想使用 Ajax 执行此操作,因此不需要加载页面
我正在尝试以下
<a class="choice" id="link" href="page.php?id=12&pid=12" >pass data</a>
$(document).ready(function() {
$(".choice").click(function(e) {
e.preventDefault();
$.ajax( {
<!--insert.php calls the PHP file-->
url: "votes.php",
method: "get",
dataType: 'html',
success: function(strMessage) {
$("#vote").html(strMessage);
}
});
});
});
但我无法让它工作。我需要有关使用 Ajax 向我的 php 文件
正确发送数据的帮助
首先,JavaScript 部分需要一个 <script>
标签。 Ajax URL 可以通过 $(this).attr('href')
从 <a>
标签 href
属性中读取。同样根据您的代码,需要一个带有 vote
id 的 div,因此添加了 <div id="vote"></div>
。
<a class="choice" id="link" href="page.php?id=12&pid=12" >pass data</a>
<div id="vote"></div>
<script>
$(document).ready(function() {
$(".choice").click(function(e) {
e.preventDefault();
$.ajax( {
url: $(this).attr('href'),
method: "get",
dataType: 'html',
success: function(strMessage) {
$("#vote").html(strMessage);
}
});
});
});
</script>
我正在尝试使用 Ajax
通过获取请求将数据从超链接传递到页面最初我可以在没有 Ajax 的情况下直接执行此操作,如下所示
<a id="link" href="page.php?id=12&pid=12" >pass data</a>
我在下面的 php
<?php $_GET['id'] ?>
<?php $_GET['pid']?>
现在我想使用 Ajax 执行此操作,因此不需要加载页面
我正在尝试以下
<a class="choice" id="link" href="page.php?id=12&pid=12" >pass data</a>
$(document).ready(function() {
$(".choice").click(function(e) {
e.preventDefault();
$.ajax( {
<!--insert.php calls the PHP file-->
url: "votes.php",
method: "get",
dataType: 'html',
success: function(strMessage) {
$("#vote").html(strMessage);
}
});
});
});
但我无法让它工作。我需要有关使用 Ajax 向我的 php 文件
正确发送数据的帮助首先,JavaScript 部分需要一个 <script>
标签。 Ajax URL 可以通过 $(this).attr('href')
从 <a>
标签 href
属性中读取。同样根据您的代码,需要一个带有 vote
id 的 div,因此添加了 <div id="vote"></div>
。
<a class="choice" id="link" href="page.php?id=12&pid=12" >pass data</a>
<div id="vote"></div>
<script>
$(document).ready(function() {
$(".choice").click(function(e) {
e.preventDefault();
$.ajax( {
url: $(this).attr('href'),
method: "get",
dataType: 'html',
success: function(strMessage) {
$("#vote").html(strMessage);
}
});
});
});
</script>