简体   繁体   中英

Store a button id into a variable in php

I have this example button <button id="red1"></button> Is there a way that i could access/store this button into a variable in php? I tried to searched same questions but they are related to jquery or javascripts.

As Vladimir said, PHP is a server-side language and therefore you cannot access the DOM and manipulate it while it's has been sent to the client. We use Javascript, a client-side language, to achieve that.

You have asked How to store a button id into a variable in PHP? . Not sure if that's really what you're asking for but here's how.

PHP file:

<?

$myId = 'elementId';

// this will echo the button with the id elementId
echo '<button id="'.$myId.'"></button>';

Using AJAX

But if you have multiple buttons with different ids and you want to know server-side which button's id the user has clicked then you should use AJAX like Masivuye proposed.

AJAX stands for "Asynchronous JavaScript and XML". Although the name includes XML, JSON is more often used due to it's simpler formatting and lower redundancy. AJAX allows the user to communicate with external resources without reloading the webpage.

HTML:

<button id="buttonA" class="buttonAction">A</button>
<button id="buttonB" class="buttonAction">B</button>
<button id="buttonC" class="buttonAction">C</button>
<button id="buttonD" class="buttonAction">D</button>

JS:

$(document).ready(function() {

  $('.buttonAction').click(function() {

    var id = $(this).attr('id');

    $.ajax({

       url: "myphpfile.txt",

       data: id,

       success: function(response){

        // if you have echoed something in your PHP file 
        // then you can access that with var `response`

        }

     });


  });


});

PHP:

<?

if(isset($_REQUEST['id'])) {

    $data = json_decode($_REQUEST['id'])

    echo json_encode('true');

} else {

    echo json_encode('false');

}

To get the id of a button you could use jquery .prop() method / function which is used to Get the value of a property for the first element in the set of matched elements or set one or more properties for every matched element. Then you could use ajax to send the value of that id to the server side.

here's how :

<script src="https://code.jquery.com/jquery-3.2.1.min.js"  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>

<button id="red1">Button text</button>

<script type="text/javascript">

    $('#red1').on('click',function(){


        var btnId = $(this).prop('id');

        $.ajax({


            type :"POST",
            data : {buttonID : btnId},
            url  :"action.php",
            dataType : "json",
            encode : true,
            success : function(data){

                alert(data);
            }
        })


    });
</script>

then server side action.php

<?php

    $btnID = $_POST['buttonID'];

    //do what ever you want to do with this ID. $btnID

    echo json_encode($btnID); //send back the id to the front end as json format

?>

PHP is a server-side language and can't access and change already generated document, but you can get document content and parse it. In your example, you can get the HTML content of this link http://example.local/form like this:

$doc = file_get_contents('http://example.local/form');
// Now parse $doc

but for client-side changes you need to use Javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM