Pass JavaScript value to PHP

How to step JavaScript value to PHP?
The value (passValue)
Or the value you have on the "on " or "off " button

for here $testup = (passValue);

Sorry bad English

  <button id="toggle" name="test11" onclick="myFunction20()" value="on">on</button>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script>
    
    function myFunction20() {
    var change = document.getElementById("toggle");
    
    var passValue  = change.value;
    if (change.innerHTML == "on")
    {
    change.innerHTML = "off";
    change.value= "off";
    }
    else {
    change.innerHTML = "on";
    change.value= "on";
    }
    <?php $plugins_url = plugins_url();?>
    $.ajax({
    type: 'POST',
      url: "<?=$plugins_url.'/enhanced-text-widget/updatebutton.php';?>",
      cache: false,
      data: { 
         
            <?php 
            $testup = (passValue);
            update_post_meta('20', 'tracks-info', $testup ); ?>
        },
      success: function(html){
        
    	alert(html)
    	return false;
      }
    });
    
    alert(passValue);
    }
    
    </script>

PHP creates the web page.
The web browser runs the web page.
JavaScript on the web page is then executed.

The only useful way to get information to PHP is to make an ajax request to the a php page, giving it the appropriate information.

In this case, it looks like you need to do the request to update_post_meta first, and only on receiving a suitable response will you then post to the updatebutton page.

1 Like

I should follow up with the type of code structure that you can use to achieve that.

$.when(
    $.ajax("/first/call"),
    $.ajax("/second/call"),
    $.ajax("/third/call")
).done(function(first_call, second_call, third_call) {
    // do something
}).fail(function() {
    // handle errors
});

In your case you would only use one ajax in the when section, to the data, and use the done section to do the updatebutton ajax post.