0

I have a loop in php that for each item, a link that opens a lightbox is generated. How can I pass data related to the current iteration (such as the userid) in such a way that I can use it in the lightbox and the form submission?

<?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>
1
  • What I would do is remove the .click from the javascript, add a parameter such as function(id) and then when echoing the link, add onclick=\"function(" . $myid . "\" Commented Feb 26, 2016 at 0:18

5 Answers 5

1

You can add any data into html

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

and pull it later with js

$(this).data('userid')
Sign up to request clarification or add additional context in comments.

Comments

1

You can use data attributes.

Add them to your php ..

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

.. then you can easily access them in javascript ..

this.dataset.userid

Comments

1
<?php
    foreach ($results as $viewUser)
        {
                if ($viewUser)
                {
                    echo '<a data-user="<?=$viewUser->userId?>" class="change-email" href="#">Change Email</a>';
                }
        }
    }
    ?>
<script>
$(function(){
    $(".change-email").click(function(){
            var user = $(this).data("user");

            $.fancybox({
                'href'              : '#change-email-dialog',
                'width'             : '200px',
                'height'            : 'auto',
                'autoScale'         : true,
                'transitionIn'      : 'fade',
                'transitionOut'     : 'fade',
                'type'              : 'inline',
                'modal'             : false,
                'afterShow'         : function () {
                     $("#user_input").val(user);
                }
            });
        });
});   
</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(); ?>">
        <input id="user_input" type="hidden" name="user" value="" />
        <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>  

Comments

1

Your JavaScript Query has a chance to return multiple results

change to this

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

                }
            });
        });
    });  
});

Comments

0

Expanding on my comment, you can remove the .click event from the javascript and just leave the function. Then you would call the function on the onclick event in HTML, while also passing your variable(s). It would look like this

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

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

    }
}
</script>

Where myid in javascript would contain whatever $myid contained in PHP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.