AJAX POST& PHP POST在同一页面 [英] AJAX POST & PHP POST In Same Page

查看:111
本文介绍了AJAX POST& PHP POST在同一页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个分别为drpcategorydrpitem的下拉列表,如下所示;

I have 2 dropdowns called drpcategory and drpitem as below;

<div class="form-group">
<label>Category</label>
<select class="form-control bg-dark btn-dark text-white" id="drpcategory" name="drpcategory" required>
<?php
$category = ''.$dir.'/template/post/category.txt';
$category = file($category, FILE_IGNORE_NEW_LINES);

foreach($category as $category)
{
echo "<option value='".$category."'>$category</option>";
}
?>
</select>
</div> 

<div class="form-group">
<label>Item</label>
<select class="form-control bg-dark btn-dark text-white" id="drpitem" name="drpitem">
<?php
$category = $_POST["drpcategory"] ?? 'Electronics';
$item = ''.$dir.'/template/post/'.$category.'/item.txt';
$item = file($item, FILE_IGNORE_NEW_LINES);

foreach($item as $item)
{
echo "<option value='".$item."'>$item</option>";
}
?>
</select>
</div>

如您所见,根据选定的drpcategory值,应动态填充drpitem.

As you can see, depending on the selected value of drpcategory, the drpitem should be dynamically populated.

如果您想知道,如果我没有进行任何post检查的情况下手动设置$category,则两个下拉列表都将经过PHP循环并填充.

In case you were wondering, both dropdowns will go through the PHP loop and populate if I set $category manually without any post checks.

现在,我正在使用AJAX postdrpcategory中的更改发布到下面的同一页面中;

Now, I'm using AJAX post to post the changes in drpcategory into the same page as below;

<script>
  $(function(){

    $('#drpcategory').on('change',function()
    {

      $.ajax({
        method: 'post',
        data: $(this).serialize(),
        success: function(data)
        {
          alert(data);
        }
      });

    });    

  });
</script>

以下是上述AJAX post Chrome 浏览器> 检查> 网络选项卡输出;

Here is the Chrome Browser > Inspect > Network tab output for the above AJAX post;

是的,我正在post将此AJAX放入同一页面,并且url是:http://example.com/?page=post,与此网络"选项卡显示的相同.

And yes, I'm posting this AJAX into the same page and the url is: http://example.com/?page=post which is the same as this Network tab shows.

我已经从AJAX函数中删除了url字段,因为浏览器会自动选择当前页面,因此更好,而且不行,在其中手动编写任何url并不会改变我要更改的内容在下面问.

I have removed the url field from AJAX function because the browser automatically picks up the current page so it's better, and no, manually writing any url in there didn't change anything about what I'm about to ask below.

问题是,我怎样才能使drpitem提取AJAX post ed drpcategory值并开始动态填充?

Question is, how can I make that drpitem to pickup the AJAX posted drpcategory value and start populating dynamically?

我想AJAX post到同一页面,并且所有这些都应该在不重新加载页面的情况下发生.

I want to AJAX post to the same page and all of this should happen without a page reload.

推荐答案

最后,在等待7天以上以解决此问题后,我无法AJAX post到同一页面,可能是因为<html>元素已经在同一页面中.但是,这里使用外部post.php文件.除了上面我最初的问题中提到的以外,我仅需要进行这些更改即可完成此工作.

Finally after waiting more than 7 days trying to figure this out, I couldn't AJAX post to the same page probably because there are too much of <html> elements already in the same page. But here goes it using an external post.php file. Only these changes were required for me to get this working other than mentioned in my original question above.

<script>
$(function()
{
$('#drpcategory').change(function()
{
$.ajax({
method: 'post',
url: 'post.php',
data: $(this).serialize(),
success: function(data)
{
$('#drpitem').html(data);
}
});
});
});
</script>

然后,我必须将此post.php保存在index.php所在的根网站目录中.根据需要更改目录,并且还记得更改上述AJAX函数中的url字段.

Then I had to save this post.php in the root website directory where index.php is located. Change your directory as necessary and also remember to change the url field in the above AJAX function.

<?php include 'config.php';?> //this is where the $dir variable is derived in my website. i had to include this since post.php is an external file that doesn't know about the variable.

<?php
$category = $_POST["drpcategory"];
$item = ''.$dir.'/template/post/'.$category.'/item.txt';
$item = file($item, FILE_IGNORE_NEW_LINES);

foreach($item as $item)
{
echo "<option value='".$item."'>$item</option>";
}
?>

感谢你们中的每个人都引导我完成此任务.如果给出直接答案,我会标记您中的某个人为答案,但是如果有人已经给出了答案,对不起您未能正确理解您.但是我认为最终目标是让某人以此作为帮助.

Thank you for each and everyone of you for walking me through this. I would have marked someone of you as the answer if a direct answer was given but if someone already given this, I'm sorry for not understanding you properly. But I think the end goal is for someone to use this as help.

这篇关于AJAX POST&amp; PHP POST在同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆