dth34653 2016-07-01 05:32
浏览 131


I know similar questions to this have been asked before but I don't see an answer that suits my question. I have a div that triggers a jquery script which stores an id. I can get the id into a variable in the jquery but I want to then assign that id to a PHP variable that exists on the same page.

So far this is what I've got. Here is my div that exists in index.php:

echo '<div id="'.$unit_id.'" class="dropdown unit '.$unit_size.' unit-'.$unit_number.' '.$unit_status.' data-unit-id-'.$unit_id.'">';

And here is my jquery that I call in from functions.php:

        var unit_id = $(this).attr('id');
            type: 'post',
            data: {id: unit_id}, //Pass the id

I want to pass the unit_id into a variable on index.php called $getID but I can't seem to find a way of getting in there. I've tried using post but there really isn't a form being submitted. It's just a dive being clicked on.

  • 写回答

4条回答 默认 最新

  • duanjia3187 2016-07-01 05:43

    You must change your id in your <div> and set it to fetch the variable sent by jQuery. Other than that, your jQuery script looks fine.

    Here's how I would do it:

    # Fetch the variable if it's set.
    <?php $unit_id = (isset($_POST["id"])) ? $_POST["id"] : null; ?>
    # Echo it as the id of the div.
    <div id = "<?= $unit_id; ?>" class = "dropdown unit"></div>


    The PHP code above is equal to:

    # Fetch the variable if it's set.
        if (isset($_POST["id"])):
            $unit_id = $_POST["id"];
            $unit_id = null;
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥30 求一下解题思路,完全不懂。
  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导