donglei2288 2017-06-01 20:51
浏览 34
已采纳

来自foreach的javascript数据在php中。

Related to my previous question: How to use javascript load function twice? (load in load)

I've got everything working (load in load). But now I want to use a foreach which sends data to the second load.

When I use this foreach the data (POST ID) remains the same.

Example code:

<script >
    $(document).ready(function () {
        // Forum categories
        $("#main").on("click", "#message", function () {
            $("#getmessage").load("forum/getmessage.php", {'id': $("#id").val()});
        });
    });
</script >

<?php foreach($stickies as $sticky): ?>
                <form id="form_id" >
                    <a href="#" class="list-group-item small" id="message"
                       style="background-color:black; color:white;" >
                        <span class="glyphicon glyphicon-pushpin" aria-hidden="true" ></span >
                        | <?php echo $sticky['header']; ?>
                    </a >

                    <input type="hidden" id="id" name="id" value="<?php echo $sticky['id']; ?>" >

                </form >
            <?php endforeach; ?>

As you can see I'd like to push the value ID as POST data, so the loaded file can perform a query on this POST ID.

How can I send the correct ID for every foreach?

  • 写回答

2条回答 默认 最新

  • dtjwov4984 2017-06-01 21:09
    关注

    There are a lot of ways to achieve what you need. So it's just an example.

    First of all - value of id attribute must be unique. No exceptions. That's why id="message" for every a and id="id" for every input are invalid. For similar elements - use classes.

    Fixed code will be:

    <?php foreach($stickies as $sticky): ?>
        <form>
            <a href="#" class="list-group-item small js-message"
               style="background-color:black; color:white;" >
                <span class="glyphicon glyphicon-pushpin" aria-hidden="true" ></span >
                | <?php echo $sticky['header']; ?>
            </a >
            <input type="hidden" name="id" value="<?php echo $sticky['id']; ?>" >
        </form >
    <?php endforeach; ?>
    

    As you can see, I removed id attributes with same values. For inputs and forms you don't even need them, for as I added class js-message.

    Next is handler:

    $(document).ready(function () {
        // Forum categories
        $("#main").on("click", ".js-message", function () {
            // ways of getting value id can be different.
            $("#getmessage").load(
                "forum/getmessage.php", 
                 {'id': $(this).parent().find("input").val()
            });
    
            // optionally you can add
            return false;
            // to prevent default click action which in case
            // of link is scrolling to the top of a page
        });
    });
    

    Here, as one of the ways to get value I used

    $(this).parent().find("input").val()
    

    which is:

    $(this).parent()    // find a parent of a clicked a, it will be `form`
        .find("input")  // among `form` children find `input`
        .val()          // take value from found input
    

    Surely, if your markup will change and more inputs will appear on the form, this may stop working. But it's just an example, you can modify this as you want.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题