Clear Event ID and call a new one without refreshing the page

Viewed 36

I'm parsing array of events from mysql and list them into a landing page. The user can click a button to send a message to the event organizer. The user should login before sending the email so I sent the event ID with the http request and return it back when session is sent than call via ajax to populate the modal and send the message. Everything is going fine until now however the user should reload the page to clear the last event id and call a new one all over again.

Below is the code where I set data-jid="<?= $jid; ?>" where it set the event id attached to the button and send it via http request once redirected to the login page

                    <?php if ($this->session->userdata('is_user_login')) : ?>
                        <button type="button" class="btn btn-secondary btn-block rounded contactBiz" data-toggle="modal" data-jid="<?= $jid; ?>"  data-target="#modalApply"><?=trans('apply')?></button>
                    <?php  else:
                        $last_request_page = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

                        $this->session->set_userdata('last_request_page', $last_request_page); 
                    ?>
                        <a href="<?= base_url(); ?>auth/login?id=<?= $jid; ?>" data-jid="<?= $jid; ?>" class="btn btn-secondary btn-block rounded preapply"><?=trans('apply')?></a>
                    <?php endif; ?>

In the login page I've set the session of the event id as following:

<?php
    if(isset($_GET['id'])) {
        $this->session->set_userdata('event_id', $_GET['id']);
    }
?>

Now after the user redirects back the last clicked button will be populated to send the message using below javascript call.

<?php if (!empty($this->session->userdata('event_id'))) {
    $sID = $this->session->userdata('event_id'); ?>

    $(document).ready(function(){
        $(".contactBiz").trigger("click");
    });

<?php } ?>
<?php if ($this->session->userdata('is_user_login')) : ?>
        $('.contactBiz').click(function(e){
            e.preventDefault();
            var id;
            var csrfName = '<?php echo $this->security->get_csrf_token_name();?>';
            var csrfHash = '<?php echo $this->security->get_csrf_hash();?>';

            var data = {
            <?php if (!empty($this->session->userdata('event_id'))) { ?>
                id: <?= $sID; ?>,
            <?php } else { ?>
                id: $(this).attr('data-jid'),
            <?php } ?>
                uid: <?= $this->session->userdata('user_id'); ?>,
            };
            data[csrfName] = csrfHash;

            $.ajax({
                cache: false,
                url: base_url + 'job-data',
                dataType: 'json',
                type: 'POST',
                data: data,
                error: function(jqXHR, textStatus, errorThrown) {
                    alert('Something is wrong');
                },
                success: function(data) {
                    console.log(data);
                    $("#subject").val(data[0].title);
                    $(".messageTitle").empty().append( " <?= trans('message'); ?> " + data[0].name + " about " + data[0].title );
                    p1 = data[0].business_id;
                    p2 = data[0].email;
                    <?php $this->session->unset_userdata('event_id'); ?>
                } 
            }); 

        });
<?php endif; ?>

Now everything as said is fine, the last clicked button is populated using the event id data-jid="<?= $jid; ?>" but what happens next the user got stuck with the same event id repeated for all buttons.

The solution I used in the ajax call to define the id as below:

        <?php if (!empty($this->session->userdata('event_id'))) { ?>
            id: <?= $sID; ?>,
        <?php } else { ?>
            id: $(this).attr('data-jid'),
        <?php } ?>

And after the successful ajax response I'm clearing the session as set below:

            <?php $this->session->unset_userdata('event_id'); ?>

I think my mistake is here however I couldn't find any better solution, your advice is highly appreciated thank you!

0 Answers
Related