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条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R