A
A
Alexander Novikov2015-11-11 09:36:57
PHP
Alexander Novikov, 2015-11-11 09:36:57

How to change php variable on the fly?

People, tell me, there is a Select on the page, the choice of the city. When choosing one city, the Slogan, phone number, mail changes, without refreshing the page.

$.post(
      ajax.url,
      {
        'action' : 'contact_city',
        'page_id': ajax.page_id,
        'city_id': city_id
      },
      function(data){
        //console.log( data );
        data = JSON.parse( data );
        if ( data.tel ) {
                  console.log(data.tel);
          $('.phone-side-phone').html( data.tel );
          $('.phone-side-email').html( data.email );
          $('.slogan').html( data.slogan );
          $('#rab').html( data.rabochie );
          // в этой переменной коефициент для калькулятора
          if ( data.koef )
            ajax.koef = data.koef;
        }
      }
    );


The page also has a Block with the output of pictures in the carousel
<div class="workers-slider-wrap">
    <?php global $query_string;
      $remwork = query_posts($query_string.'&page=remont-kvartir'); ?>
      
      
    <?php $remworkers=get_field('ремонт_рабочие_питер'); ?>
      
      
      <ul class="workers-slider">
      <? foreach($remworkers as $remworker){?>
        <li>
          <img src="<?php echo $remworker['фото_рабочего'];?>" height="188" width="188" alt="">
          <p class="name"><?php echo $remworker['имя_рабочего'];?></p>
          <p class="dolj"><?php echo $remworker['должность_рабочего'];?></p>
          <div class="line"></div>
          <?php echo $remworker['опыт_рабочего'];?>
        </li>
      <? }?>
      </ul><!-- workers-slider -->
      <?php wp_reset_query();?>
    </div>


Tell me how to change
<?php $remworkers=get_field('ремонт_рабочие_питер'); ?>

on the
<?php $remworkers=get_field('ремонт_рабочие_Другой Город'); ?>


When choosing Select. That is, as a Phone, Mail, etc. No page reload.
Thank you .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Svirsky, 2015-11-11
@REI555

Use code wrapping to make what you write readable:
$.ajax({some: 'params'});

I
Ivan, 2015-11-11
@LiguidCool

To be honest, your code is not readable and I'm too lazy to read it, but I'll work with Vanga ...
I think you don't understand how PHP works - "it was born to die", i.e. by the time the user receives the page, the code has already been executed and the PHP process has exited. You will not be able to change any variable, because there is nothing to change - the program is unloaded.
Your task is solved through AJAX. For example, a page is loaded, in it, upon completion of loading or changing the necessary fields, a request is made to another file, where the city id is passed, and it returns html, which is inserted into the desired block, or xml, followed by parsing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question