1

I have the following code that outputs terms & conditions data (agb) for a specific vendor using php echo in a pop-up window. This works fine as long as there is only one vendor. When additional vendors are listed, the displayed data in the new window is the same as for the first vendor.

As far as I understand javascript is executed outside of the PHP and therefore the value of the string output is not updated (because ofmissing target?) and refers always to the first vendor, i.e. the JavaScript code will always fill the new window with the same content.

I think I need to pass a vendor ID or similar to the javascript function. But how can I achieve this?

PHP

<?php if ($_v->getId()): ?>  
<p>Seller: <?php echo $_v->getVendorName() ?> </p>  
<a href="" class="new-window">terms & conditions</a> 

<div style="display:none;">
    <div id="agb-text"><?php echo $_v->getData('agb')?></div>
</div>

JS:

<script type='text/javascript'>    
    jQuery(function($) {
        $('a.new-window').click(function(){

        var recipe =  window.open('','PrintWindow','width=600,height=600');
        var html = '<html><head><title>AGB</title></head><body><div id="my-id">' + $('<div />').append($('#agb-text').clone()).html() + '</div></body></html>';
        recipe.document.open();
        recipe.document.write(html);
        recipe.document.close();

    return false;
    });
});
</script>
3
  • Ok, it looks like you are pre-building the popup content, and then using javascript to simply shuttle that pre-built sub-html into the window. Since you are using window.open (and not a modal)... you could instead target a php url for the content, instead of shuttling. Commented Jun 29, 2018 at 13:23
  • The other method, is to use a modal, and ajax, and hit php for new html content with a passed id. A little more involved, but its more 'today savy' in presentation. Commented Jun 29, 2018 at 13:24
  • @IncredibleHat, thanks for those alternatives! Commented Jun 29, 2018 at 14:46

2 Answers 2

1

The reason you are always getting the same text in the pop up is because your div which holds the text has the same ID of agb-text, and there can only be 1 ID set with that name, others will be ignored. So you would be better of using classes and data attributes.

You can do that like this:

<?php if ($_v->getId()): ?>  
<p>Seller: <?php echo $_v->getVendorName() ?> </p>  
<a href="" class="new-window" data-vendor="<?php echo $_v->getVendorID();?>">terms & conditions</a> 

<div style="display:none;">
    <div id="agb-text-<?php echo $_v->getVendorID();?>"><?php echo $_v->getData('agb')?></div>
</div>

<script type='text/javascript'>    

    jQuery(function($) {
        $('a.new-window').click(function(){
            var vendorID = $(this).data("vendor");
            var recipe =  window.open('','PrintWindow','width=600,height=600');
            var html = '<html><head><title>AGB</title></head><body><div id="my-id">' + $('<div />').append($('#agb-text-' + vendorID).clone()).html() + '</div></body></html>';
            recipe.document.open();
            recipe.document.write(html);
            recipe.document.close();

            return false;

        });
    });

</script>

I used the $_v->getVendorID() function as I'm assuming you have it, but if you don't, create one or use something that suits you better to generate a unique value for each vendor.

Sretno :)

Sign up to request clarification or add additional context in comments.

5 Comments

That was exactly the solution! Thank you so much, it helps a lot.
My only concern with this method, is there will be generated html for every vender in your system hidden on the page. If each block has sensitive information per vender, then anyone who 'view source' could see it ;) But this works if that info isn't all that specific.
@IncredibleHat I agree, and I would personally do this with ajax, especially if there is going to be more than a few vendors on a single page, so it doesn't increase the load time of the page having to render a ton of agreements.
Good point. I will need to consider it during development. As far as I can judge it, the information will not be sensitive.
@Igor Ilic, another good point taken, there will be only a a few vendors on each occasion, so it seems a workable solution.
0

try to using ajax request to get your data, and pass your id using onclick=<?php echo $_v->getId(); ?>.

but, you need to make controller to get ID.

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.