duanbi1983 2019-03-03 12:22
浏览 147
已采纳

AJAX POST和PHP POST在同一页面

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>

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

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

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>

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

Inspect

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.

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.

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

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

  • 写回答

2条回答 默认 最新

  • dongpu2694 2019-03-06 08:15
    关注

    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>
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能