• 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 to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    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

    (@roelfk7)

    Hi @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
    Is that correct

    Hi @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

    2) You can use AJAX to hit your DB for the current price. E.g., 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

    (@roelfk7)

    @mlchaves Thank you so much it will definitely help me

    Hi @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.