简体   繁体   English

将按钮ID存储到php中的变量中

[英]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? 我有这个示例按钮<button id="red1"></button>有没有一种方法可以将这个按钮访问/存储到php中的变量中? I tried to searched same questions but they are related to jquery or javascripts. 我试图搜索相同的问题,但它们与jquery或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. 正如弗拉基米尔(Vladimir)所说,PHP是一种服务器端语言,因此在将DOM发送到客户端后,您将无法访问和操纵DOM。 We use Javascript, a client-side language, to achieve that. 我们使用客户端语言Javascript来实现这一目标。

You have asked How to store a button id into a variable in PHP? 您已经问过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: PHP文件:

<?

$myId = 'elementId';

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

Using AJAX 使用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. 但是,如果您有多个具有不同ID的按钮,并且想知道服务器端用户单击了哪个按钮的ID,则应使用Masivuye提出的AJAX。

AJAX stands for "Asynchronous JavaScript and XML". AJAX代表“异步JavaScript和XML”。 Although the name includes XML, JSON is more often used due to it's simpler formatting and lower redundancy. 尽管名称包括XML,但由于JSON格式简单且冗余度较低,因此更常用。 AJAX allows the user to communicate with external resources without reloading the webpage. AJAX允许用户与外部资源进行通信,而无需重新加载网页。

HTML: 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: 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: 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. 要获取按钮的ID,可以使用jquery .prop()方法/函数,该方法/函数用于获取匹配元素集中第一个元素的属性值,或者为每个匹配元素设置一个或多个属性。 Then you could use ajax to send the value of that id to the server side. 然后,您可以使用ajax将该id的值发送到服务器端。

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 然后是服务器端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. PHP是一种服务器端语言,无法访问和更改已生成的文档,但是您可以获取文档内容并进行解析。 In your example, you can get the HTML content of this link http://example.local/form like this: 在您的示例中,您可以像以下方式获取此链接http://example.local/form的HTML内容:

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

but for client-side changes you need to use Javascript. 但是对于客户端更改,您需要使用Javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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