javascript - 将php数据发送到javascript点击事件

标签 javascript php

我在 php 中有一个循环,对于每个项目,都会生成一个打开灯箱的链接。如何传递与当前迭代相关的数据(例如用户 ID),以便我可以在灯箱和表单提交中使用它?

<?php
foreach ($results as $viewUser)
    {
            if ($viewUser)
            {
                echo '<a class="change-email" href="#">Change Email</a>';
            }
    }
}
?>

<script>
$(function(){
    $(".change-email").click(function(){

            $.fancybox({
                'href'              : '#change-email-dialog',
                'width'             : '200px',
                'height'            : 'auto',
                'autoScale'         : true,
                'transitionIn'      : 'fade',
                'transitionOut'     : 'fade',
                'type'              : 'inline',
                'modal'             : false,
                'afterShow'         : function () {

                }
            });
        });
});   
</script>

<div id="change-email-dialog">
    <form id="change-email-form" class="table" method="POST" action="<?php echo site_url('customerservice/changeEmail').'user='.$viewUser->getBusID(); ?>">
        <ul class="table-row">
            <li class="table-cell">New Email</li>
            <li class="table-cell"><input type="text" name="newEmail" /></li>
        </ul>
        <ul class="table-row">
            <li class="table-cell">Notify Contract Entry?</li>
            <li><input type="checkbox" checked="checked" name="notify" /></li>
        </ul>

        <ul class="table-row">
            <li><input name="userSearchSubmit" type="submit" value="Save" /></li>
        </ul>

    </form>  
</div>

最佳答案

您可以将任何数据添加到html中

echo '<a class="change-email" href="#" data-userid="$viewUser.id">Change Email</a>';

稍后用js拉取

$(this).data('userid')

关于javascript - 将php数据发送到javascript点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35640954/

相关文章:

javascript - 将 'Select All' 添加到 Bootstrap 多选表单/列表

javascript - AdminLte 删除准备出售您的主题

没有框架的javascript ajax请求

php - 使用 PHP 代码更改 MySQL 中的行

javascript - 在javascript中使用索引数组对数组进行索引

Javascript .find() 无法在所有 IE 浏览器下工作

php - Laravel laratrust 在注册时为新用户附加一个角色

php - pg_connect 使我的 php 脚本崩溃

php - 无法删除用 PHP 创建的目录或文件(又名权限 hell )

php - sql中如何获取子查询结果列的一列的总和