vmichnowicz / vwm_polls

Free ExpressionEngine Poll Module and Fieldtype
Apache License 2.0
16 stars 8 forks source link

Polls display correctly on some pages, but not others #31

Closed imonroe closed 10 years ago

imonroe commented 10 years ago

Ok, so I've used the VWM polls sample code to create a widget, which I want to be able to include on story pages. Here's the code:

<div class="col-xs-6 poll_display" style="float:left;">

I'm the poll include.  I got an entry of "{embed:poll}".

{exp:channel:entries channel="polls" status="open|closed" limit="1" entry_id="{embed:poll}"}

    <h3>Poll: {title}</h3>

    {poll_details}

    {exp:vwm_polls:poll entry_id="{entry_id}" field_id="{poll_question:field_id}" redirect="index" limit="1"}
        {if can_vote && segment_3 != "results"}
            <fieldset>
                {if already_voted}
                    You already voted in this poll, however, I will allow you to vote again &mdash; cuz I am a nice guy.
                {/if}

                <ul>
                    {options}
                        <li {if user_vote}class="user_vote"{/if}>
                            <label for="option_{id}">
                                <input type="{input_type}" name="{input_name}" id="option_{id}" value="{id}" />
                                <span>{text}</span>
                                {if type == "other"}
                                    <input type="text" name="{other_name}" />
                                {/if}
                            </label>
                        </li>
                    {/options}
                </ul>
                <input type="submit" name="submit" value="Vote" />
                <a href="{path="polls/{url_title}/results}">View Results</a>
            </fieldset>
        {if:else}
            <ul>
                {options_results}
                    <li id="option_{id}" {if user_vote}class="user_vote"{/if}>
                        <div style="width: {percent}%">{percent}%</div>
                        <span>{text}</span>
                    </li>
                {/options_results}
            </ul>
            <img src="{chart}" alt="" />
        {/if}

    {/exp:vwm_polls:poll}

    <hr/>

{/exp:channel:entries}

<script type="text/javascript">
$(document).ready(function() {

$('form[id*="vwm_polls_poll"]').submit(function(e) {
    e.preventDefault();
    var form = $(this);
    var action = $(this).attr('action');
    var id = $(this).attr('id') + '_img';

    $.ajax({
        type: 'POST',
        url: action,
        data: $(this).serialize(),
        success: function(data) {
            var chart = data.chart;
            var img = $('<img />').attr('src', chart).attr('id', id).attr('alt', '');
            $(form).find('fieldset').fadeOut('slow', function() {
                $(form).append(img).hide().fadeIn('slow');
            });
        },
        error: function(data) {
            var data = $.parseJSON(data.responseText);
            var xid = data.xid;
            $(form).find('input[name="XID"]').val(xid);
            $.each(data.errors, function(index, value) {
                alert(value);
            });
        },
        dataType: 'json'
    });
});

});
</script>

</div>

this is called like so:

<? if (!empty($poll_id)) { ?>
    {embed="widgets/widget.poll_display" poll="<?=$poll_id; ?>"}
<? } ?>

So this works great on the front page of my site (where no $poll_id variable is supplied), and it works on a page all by itself, but it doesn't work on my story pages. It's like the {exp:channel:entries} loop never fires at all.

What am I doing wrong?

vmichnowicz commented 10 years ago

The example code assumes you intend to display the polls dynamically. If, for example, you want to display a particular poll on multiple pages of your site regardless of the current URL you must use dynamic="no" in the {exp:channel:entries} tag.

imonroe commented 10 years ago

AHA! Brilliant!

I knew it was just some stupid thing that I had forgotten about.

Works great now. Thanks very much for the quick response!