是否可以获取元素id
into a PHP
多变的?
假设我有许多带有 ID 的元素:
<span id="1" class="myElement"></span>
<span id="2" class="myElement"></span>
我怎样才能把它变成PHP
变量以便提交查询。我想我必须重新提交页面,这没关系。我想使用 POST。我可以做类似的事情吗:
<script language="JavaScript">
$(document).ready(function(){
$(".myElement").click(function() {
$.post("'.$_SERVER['REQUEST_URI'].'", { id: $(this).attr("id") });
});
});
</script>
我需要通过$(this).attr('id')
into $newID
为了运行
SELECT * from t1 WHERE id = $newID
jQuery
是一个非常强大的工具,我想找到一种方法将其功能与服务器端代码结合起来。
Thanks.
这就像你的问题:使用 jQuery 进行 ajax 发布
如果您希望将所有内容都保存在一个文件中(发布到活动文件),那么您通常需要以下内容:
<?php
// Place this at the top of your file
if (isset($_POST['id'])) {
$newID = $_POST['id']; // You need to sanitize this before using in a query
// Perform some db queries, etc here
// Format a desired response (text, html, etc)
$response = 'Format a response here';
// This will return your formatted response to the $.post() call in jQuery
return print_r($response);
}
?>
<script type='text/javascript'>
$(document).ready(function() {
$('.myElement').click(function() {
$.post(location.href, { id: $(this).attr('id') }, function(response) {
// Inserts your chosen response into the page in 'response-content' DIV
$('#response-content').html(response); // Can also use .text(), .append(), etc
});
});
});
</script>
<span id="1" class="myElement"></span>
<span id="2" class="myElement"></span>
<div id='response-content'></div>
从这里您可以自定义查询和响应以及您想对响应执行的操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)