Recover ID passed by DIV on next page

Asked

Viewed 122 times

1

I have the following CSS class along with foreach():

    <section id="cidades">
        <div class="listacidade">ESCOLHA UMA OPÇÃO</div>
        <ul id="ListandoCidades">
        <? foreach($cidades as $valor){ ?>
            <a href="<? echo base_url('inicial'); ?>">
                <li class="listacidade" id="<? echo $valor->idParametro; ?>">
                    <img src="<? echo base_url(); ?>site/modules/entrada/images/<?=url_title($valor->parametro);?>.jpg" width="250" height="120" alt=""/>
                </li>
            </a>
        <? } ?>
        </ul>
    </section>

I’m running the ID through div. How do I retrieve it on a next page?

  • Which ID? Which page?

  • I need to pass this parameter: id="<? echo $value->idParametro; ? >", that the next page that will be /site/index... will receive this id passed.

  • The next page is this href="<? echo base_url('inicial'); ?>?

  • That’s right Kadu

  • Can’t get through there, like href="<? echo base_url('inicial').'?idParam='.$valor->idParametro; ?>?

  • It would not be my intention... It would hide the ID

  • click on the link, create a cookie, redirect, and on the other page read the cookie

Show 2 more comments

1 answer

0


As comments, to save the information use cookies or Local Storage:

$(document).on('click', '#ListandoCidades a', function(event){
   event.preventDefault(); // Evita que o navegador navegue para o link

   // Pega o ID
   var id = $(this).find('.listacidade').attr('id');

   // Salva o cookie
   document.cookie = 'idParametro='+id;

   // Ou use Local Storage do HTML5
   localStorage.setItem("idParametro", id);

   // Segue o link
   window.location.href = $(this).attr('href');

});

To read do the following:

// Cookie
var cookies = document.cookie; 
// O retorno é uma string com todos os cookies, será necessário 
// tratá-la para pegar o cookie desejado

// Storage
localStorage.getItem('idParametro');

Obs: Your markup is wrong, the tags li must be direct daughters of ul, and the tag a should be within the tags li. If you fix this, you will need to change the selector of the $(this).find('.listacidade') for $(this).parent().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.