[英]php - pass an array from php to jquery ajax and then again pass the same array from jquery to php
[英]Get data from php using jquery then pass again to php
这是我的设置:
<div class="container">
<ul>
<li data-owner="93"><a href="javascript:void(0);"></a></li>
<li data-owner="94"><a href="javascript:void(0);"></a></li>
<li data-owner="95"><a href="javascript:void(0);"></a></li>
</ul>
<div class="below_list">
<div class="popup">
<a href="javascript:void(0);" class="popup-close">
<img src="<?php echo get_template_directory_uri(); ?>/images/close.jpg">
</a>
<div class="slider-wrapper">
<div class="slider-inner">
<img src="<?php echo get_template_directory_uri(); ?>/images/slider.png" />
<img src="<?php echo get_template_directory_uri(); ?>/images/slider2.png" />
<img src="<?php echo get_template_directory_uri(); ?>/images/slider3.png" />
</div>
</div>
<div class="description">
<h1 class="title"></h1>
<h3 class="subtitle"></h3>
<p class="description"></p>
</div>
</div>
</div>
</div>
每当我单击列表项时,都将使用以下数据获取数据所有者: var formId = $(this).attr('data-owner');
。 这将用作ID。
然后我将有要追加到空白h1
使用$('.title').append('<?php echo get_the_title(formId); ?>');
我了解PHP仅执行一次,但是我不确定如何提出解决方案。
请注意,所有内容都在一页中发生,而无需刷新。
您将需要为此使用ajax。 您的php最初是在页面加载时处理的。 但是之后的任何事情都不会被处理。 查看http://api.jquery.com/jQuery.post/上的指南。
您将需要将选定的ID传递到另一个php页面(在此示例中为backend.php),进行处理(以您的情况为准,获取标题),然后在backend.php中回显结果。
示例代码如下所示。
$.ajax({
type:"POST",
url: "backend.php",
data:"id="+formId , //add the variable you need to pass to the backend.php
success: function(html){
$("#response").html(data); //shows the output given in the backend.php in the response div.
}
});
两次调用ajax
$.ajax({
url: 'a.php',
data: {name: 'xxxxxx'},
dataType: 'jsonp',
success: function(data){
// do stuff
// call next ajax function
$.ajax({ ....call to b.php....});
}
});
如果您只需要更改h1
文本而无需更改数据库,则可以使用
$('.container ul li').on('click',function(){
var formId = $(this).attr('data-owner');
$.ajax({
type:"POST",
url: "saveData.php",
data:"formId="+formId ,
success: function(data){
$(".title").text(data);
}
});
});
在saveData.php
<?php
//您可以使用$_POST['formId'];
获取表单$_POST['formId'];
$formId = $_POST['formId'];
//编写query
以从database
获取适当的formId
//然后echo
显从query
结果中获得的标题,例如
echo $formTitle;
该数据将在ajax success
可用
希望这可以帮助你交配.. :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.