如何在不刷新页面的情况下使用POST Ajax更新PHP字符串?
我正在尝试将字符串如何在不刷新页面的情况下使用POST Ajax更新PHP字符串?,php,ajax,foreach,Php,Ajax,Foreach,我正在尝试将字符串$user\u foreach\u limit中的数字从1更改为2,从2更改为3。。。(在每个按钮上单击+1)。这个字符串是foreach 当前代码:Ajax可以工作,但我必须刷新页面才能看到更改。我如何在没有页面刷新的情况下做到这一点?是否有其他选项可以让它在不将整个foreach添加到表单show more.php的情况下工作 HTML form-show-more.php $user_foreach_limit = 2; 这相当简单,您可以直接在原始循环所在的位置执行h
$user\u foreach\u limit
中的数字从1更改为2,从2更改为3。。。(在每个按钮上单击+1)。这个字符串是foreach
当前代码:Ajax可以工作,但我必须刷新页面才能看到更改。我如何在没有页面刷新的情况下做到这一点?是否有其他选项可以让它在不将整个foreach添加到表单show more.php
的情况下工作
HTML
form-show-more.php
$user_foreach_limit = 2;
这相当简单,您可以直接在原始循环所在的位置执行html,您需要一个包装器。jQuery允许您在需要时直接输入html 将其创建为视图函数,以便在需要时重复使用。该函数不能按原样工作,您需要注入
$tmp
或其他任何需要的内容,但概念要求将其作为一个函数
/functions/myfunction.php
<?php
function myfunction($user_foreach_limit = 1)
{
# I would check if the value is a number first
if(!is_numeric($user_foreach_limit))
$user_foreach_limit = 1;
# Do your loop stuffs
foreach ($modules_for_from as $m_foreach_as) {
if ($tmp++ < $user_foreach_limit) {
// foreach content
}
}
}
<form method='POST' id='form_show_more' action="form_show_more.php">
<input type="hidden" name="action" value="get_more_mods" />
<input type="hidden" name="increment" value="2" />
<input type='submit' value='Show more'/>
</form>
<!-- create a wrapper so you can use it to drop the updated html into -->
<div id="table-list">
<?php
# Add the view function
include_once(__DIR__.'/functions/myfunction.php');
# Run the function right on load for incremental 1, ajax will replace this content
myfunction(1);
?>
</div>
<script>
$('#form_show_more').on('submit', function(event){
event.preventDefault();
$.ajax({
// You can get the destination from the form
url: $('#form_show_more').attr("action"),
type: 'POST',
data: $('#form_show_more').serialize(),
success: function(response) {
$('#table-list').html(response);
}
});
});
</script>
<?php
# Stop if action not sent
if(empty($_POST['action']))
die("Invalid request.");
# Stop if the action is not correct
elseif($_POST['action'] != 'form_show_more')
die("Invalid action.");
# Add the view function
include_once(__DIR__.'/functions/myfunction.php');
myfunction($_POST['increment']);
exit;
?>
$('form#show_more')。关于('submit',函数(事件){
event.preventDefault();
$.ajax({
//您可以从表单中获取目的地
url:$('#form_show_more').attr(“action”),
键入:“POST”,
数据:$('#form_show_more')。序列化(),
成功:功能(响应){
$('#表格列表').html(回复);
}
});
});
如果发送了错误的请求,请不要执行任何操作
/form\u show\u more.php
<?php
function myfunction($user_foreach_limit = 1)
{
# I would check if the value is a number first
if(!is_numeric($user_foreach_limit))
$user_foreach_limit = 1;
# Do your loop stuffs
foreach ($modules_for_from as $m_foreach_as) {
if ($tmp++ < $user_foreach_limit) {
// foreach content
}
}
}
<form method='POST' id='form_show_more' action="form_show_more.php">
<input type="hidden" name="action" value="get_more_mods" />
<input type="hidden" name="increment" value="2" />
<input type='submit' value='Show more'/>
</form>
<!-- create a wrapper so you can use it to drop the updated html into -->
<div id="table-list">
<?php
# Add the view function
include_once(__DIR__.'/functions/myfunction.php');
# Run the function right on load for incremental 1, ajax will replace this content
myfunction(1);
?>
</div>
<script>
$('#form_show_more').on('submit', function(event){
event.preventDefault();
$.ajax({
// You can get the destination from the form
url: $('#form_show_more').attr("action"),
type: 'POST',
data: $('#form_show_more').serialize(),
success: function(response) {
$('#table-list').html(response);
}
});
});
</script>
<?php
# Stop if action not sent
if(empty($_POST['action']))
die("Invalid request.");
# Stop if the action is not correct
elseif($_POST['action'] != 'form_show_more')
die("Invalid action.");
# Add the view function
include_once(__DIR__.'/functions/myfunction.php');
myfunction($_POST['increment']);
exit;
?>
php
是服务器端脚本,当您的页面已经在浏览器上呈现时,您与php
无关。如果要在不刷新的情况下更改页面内容,请使用javascript
。您需要使用更新foreach
中的内容。完成