Title: When PHP variable&#8217;s value change.
Last modified: September 10, 2021

---

# When PHP variable’s value change.

 *  [Roelf Keulder](https://wordpress.org/support/users/roelfk7/)
 * (@roelfk7)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/when-php-variables-value-change/)
 * Good day,
 * Sorry I’m a newb to WordPress development,
 * I know you can use the onchange function for text, option and select fields.
   
   But I want to perform an action when a PHP Variable changes.
 *     ```
       // SHOW WINNING LOOSING MESSAGE
   
       $user_bid = $_POST['wdm-bidder-bidval'];
       <?php
       if($user_bid === $curr_price){ ?>
       <div class="wdm_reserved_note wdm-mark-green wdm-align-left">
        <em><?php _e('WINNING!', 'wdm-ultimate-auction'); ?></em>
        </div>
       <?php } 
   
       else {?>
       <div class="wdm_reserved_note wdm-mark-red wdm-align-left">
       <em><?php _e('LOOSING!', 'wdm-ultimate-auction'); ?></em>
        <?php
            } ?>
   
       // END WINNING LOOSING MESSAGE
       ```
   
 * What I want to do if the PHP variable $curr_price changes it must check if the
   $curr_price is equal to the value the user entered in a text box, If it is equal
   it will display a message winning, but if it is diffrent it will show a message
   loosing.
 * any advise/help please
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fwhen-php-variables-value-change%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/when-php-variables-value-change/#post-14861868)
 * A PHP variable will not change in the current session unless there is code assigning
   a value to it. It’s not anything like a form value. What you can do is verify
   whether a submitted form field value matches a stored value or not. It appears
   this is what your code is doing, but it’s incomplete.
 * Where is the value of $curr_price coming from? It’s typically from a DB query
   of some sort like `get_post_meta()`. It’s not going to persist between requests
   unless it is saved somewhere persistent. There’d then be a separate mechanism
   where users of proper capability can periodically update this value as needed
   via a separate form.
 *  Thread Starter [Roelf Keulder](https://wordpress.org/support/users/roelfk7/)
 * (@roelfk7)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/when-php-variables-value-change/#post-14883774)
 * Hi [@bcworkz](https://wordpress.org/support/users/bcworkz/), thank you, If I 
   understand it correctly even if I have called the code previously in my PHP code
   from the database and used it on the current page I should call it again to use
   it in a java script. Please see my question and answers in [WordPress Stack exchange](https://wordpress.stackexchange.com/questions/395510/how-to-check-a-variable-changes?noredirect=1#comment576675_395510)
   
   Is that correct
 *  [mark l chaves](https://wordpress.org/support/users/mlchaves/)
 * (@mlchaves)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/when-php-variables-value-change/#post-14884327)
 * Hi [@roelfk7](https://wordpress.org/support/users/roelfk7/),
 * Just to throw out a couple of live examples.
 * 1) You can use an API to fetch your current price. E.g., [https://codepen.io/marklchaves/pen/wveyxoa](https://codepen.io/marklchaves/pen/wveyxoa)
 * 2) You can use AJAX to hit your DB for the current price. E.g., [https://streetphotography.blog/ajax-random-number/](https://streetphotography.blog/ajax-random-number/)
 * Example snippet for the AJAX option.
 *     ```
       setInterval(function () {
         // Just run this on the AJAX Random Number post.
         if (!document.URL.includes("ajax-random-number")) return;
         $.ajax({
           url: ajaxurl,
           cache: false,
           data: {
             action: "random_number_request", // This is your PHP function that can hit the DB.
           },
           success: function (data) {
             const bid = 1.05; // Stub as float.
             const status = data == bid ? "WINNING" : "LOSING"; // data is a string so use == to coerce
             $("#random").text(<code>${data} ${status}</code>);
           },
           error: function (errorThrown) {
             window.alert(errorThrown); // Change to console.log for prod.
           },
         });
       }, 2000); // setInterval()
       ```
   
 * Good luck! 🙂
 *  Thread Starter [Roelf Keulder](https://wordpress.org/support/users/roelfk7/)
 * (@roelfk7)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/when-php-variables-value-change/#post-14952489)
 * [@mlchaves](https://wordpress.org/support/users/mlchaves/) Thank you so much 
   it will definitely help me
 *  [mark l chaves](https://wordpress.org/support/users/mlchaves/)
 * (@mlchaves)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/when-php-variables-value-change/#post-14953433)
 * Hi [@roelfk7](https://wordpress.org/support/users/roelfk7/),
 * You’re welcome. Glad to help 🙂
 * Shout if you have any follow-up questions.
 * Have a great weekend!

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

The topic ‘When PHP variable’s value change.’ is closed to new replies.

## Tags

 * [java](https://wordpress.org/support/topic-tag/java/)
 * [jquery](https://wordpress.org/support/topic-tag/jquery/)
 * [php variable](https://wordpress.org/support/topic-tag/php-variable/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 3 participants
 * Last reply from: [mark l chaves](https://wordpress.org/support/users/mlchaves/)
 * Last activity: [4 years, 8 months ago](https://wordpress.org/support/topic/when-php-variables-value-change/#post-14953433)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
