Title: php called by ajax code
Last modified: September 1, 2016

---

# php called by ajax code

 *  [gugu85](https://wordpress.org/support/users/gugu85/)
 * (@gugu85)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/php-called-by-ajax-code/)
 * Hi, i have a problem.
 * I created a div that is showed when a user click on a link otherwise is hidden..
   the div is activated by javascript…
 * Here is the code:
 * 1) HTML/PHP:
 *     ```
       <div id="testo"><a href="#" onclick="visualizza('recapiti'); return false"><strong>Visualizza Recapiti Telefonici</strong></a></div>
       <div id="recapiti" style="display:none;">
       <?php if(get_post_meta( get_the_ID(), 'fiemme_telefono', true ) != '') { ?>
       	<p><strong>Telefono</strong>: <?php echo get_post_meta( get_the_ID(), 'fiemme_telefono', true );?></p>
       <?php } ?>
       <?php if(get_post_meta( get_the_ID(), 'fiemme_laboratorio', true ) != '') { ?>
       	<p><strong>Laboratorio</strong>: <?php echo get_post_meta( get_the_ID(), 'fiemme_laboratorio', true );?></p>
       <?php } ?>
       <?php if(get_post_meta( get_the_ID(), 'fiemme_cellulare', true ) != '') { ?>
       	<p><strong>Cellulare</strong>: <?php echo get_post_meta( get_the_ID(), 'fiemme_cellulare', true );?></p>
       <?php } ?>
       <?php if(get_post_meta( get_the_ID(), 'fiemme_fax', true ) != '') { ?>
       	<p><strong>Fax</strong>: <?php echo get_post_meta( get_the_ID(), 'fiemme_fax', true );?></p>
       <?php } ?>
       </div>
       ```
   
 * 2) Javascript:
 *     ```
       <script type="text/javascript" language="javascript">
       function visualizza(id){
         if (document.getElementById){
           if(document.getElementById(id).style.display == 'none'){
             document.getElementById(id).style.display = 'block';
           }else{
             document.getElementById(id).style.display = 'none';
           }
         }
       }
       </script>
       ```
   
 * Now i want to insert a function that counts how many click users make on this
   link..I want to create a php file placed on my wordpress root folder and call
   it by jquery….is it possible? how can i do it?
 * Obviously i want to count one click for user session, so as not to increase the
   counter at every click of the same user in the same session.
 * I tried with this code:
 *     ```
       <script type="text/javascript" language="javascript">
       function click_counter() {
             jQuery.ajax({
                  type: 'POST',
                  url: 'click_counter.php',
                  data:{action:'call_this'},
                  success:function(html) {
                    alert(html);
                  }
             });
        }
       </script>
       ```
   
 * I’ve placed click_counter.php in public_html folder of wordpress, but it seems
   not attainable..
 * Now I have two requests:
    1) What parameter i have to set on “url:” in the latest
   code posted to make accessible the file click_counter.php
 * 2) how can I build the code that runs the counter as I described at the beginning
   of post (click_counter.php code)
 * Thanks you all and sorry for my bad english 😉

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Josh](https://wordpress.org/support/users/josh401/)
 * (@josh401)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/php-called-by-ajax-code/#post-7714821)
 * > 1) What parameter i have to set on “url:” in the latest code posted to make
   > accessible the file click_counter.php
 * In this case; you would need to use the full url to the `click_counter.php` file.(
   [https://mysite.com/click_counter.php](https://mysite.com/click_counter.php) 
   for example)
 * > 2) how can I build the code that runs the counter as I described at the beginning
   > of post (click_counter.php code)
 * This is where I believe your approach may be inadequate. What I would do, is 
   add the javascript so when a user clicks the button (in addition to showing the
   div), it also performs an AJAX rquest.
 * Then, in your functions.php file, you add the handler for processing the server
   side request. Here is what I would do:
 * 1) Set javascript click function to show div.
    2) Add AJAX call. 3) In functions.
   php, set an option you can use to keep the count. 4) In the AJAX handler in your
   functions.php, you will a) get the option, b) increment by one, c) update the
   option with the new count.
 * This way, every time the button is clicked, it executes the ajax (calling the
   php handler) and updates the count.
 * Then, in the page where you want to use the count, simply call it from the option
   you created.
 * Hope this helps!
 *  Thread Starter [gugu85](https://wordpress.org/support/users/gugu85/)
 * (@gugu85)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/php-called-by-ajax-code/#post-7714837)
 * > 1) In this case; you would need to use the full url to the click_counter.php
   > file. ([https://mysite.com/click_counter.php](https://mysite.com/click_counter.php)
   > for example)
 * I tried but from consolle I can see a 500 (Internal Server Error), instead of
   404 of before.
 * > 2) This is where I believe your approach may be inadequate. What I would do,
   > is add the javascript so when a user clicks the button (in addition to showing
   > the div), it also performs an AJAX rquest.
   > Then, in your functions.php file, you add the handler for processing the server
   > side request. Here is what I would do:
   > 1) Set javascript click function to show div.
   >  2) Add AJAX call. 3) In functions.
   > php, set an option you can use to keep the count. 4) In the AJAX handler in
   > your functions.php, you will a) get the option, b) increment by one, c) update
   > the option with the new count.
   > This way, every time the button is clicked, it executes the ajax (calling the
   > php handler) and updates the count.
   > Then, in the page where you want to use the count, simply call it from the 
   > option you created.
 * unfortunately i’m at the beginning. if you want to help me with the code would
   you do me a big favor, otherwise do not worry, I understand..
 * for now thank 😉

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘php called by ajax code’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [gugu85](https://wordpress.org/support/users/gugu85/)
 * Last activity: [9 years, 11 months ago](https://wordpress.org/support/topic/php-called-by-ajax-code/#post-7714837)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
