简体   繁体   中英

Get value of href in php

So I have this code:

<a style="color:white" id="randomNo">Hello</a>;

I am using jQuery to get the value of it by using:

$('#randomNo').text();

Is there a way to get the value ("Hello") in PHP?

@FewFlyBy here look (Note: this is just a example of using js var in php).

<div id="foo">Click Me</div>    
    $("#foo").on('click', function(event) {

        event.preventDefault();

        var randomNo= $('#randomNo').text();
        // Ajax.
        $.ajax({
            url: "SAMPLE.php",
            type: "post",
            data: {
                   randomNo: randomNo
            },
            success: function(){
                alert("success");
            },
            error:function(){
                alert("failure");
            }
        });
    });

on SAMPLE.php get the value from $_POST['randomNo'];

This is just a sample. if want want you can send your value to php page like above.

Hi You have to extract the anchor tag then textContent will help you to fetch the text of anchor tag

Here is the sample stuff

$link = "<a href='test.php'>tester</a>";
$dom = new domdocument;
$dom->loadHTML($link);
foreach ($dom->getElementsByTagName("a") as $a) {
    echo $a->textContent, "\n";
    echo $a->getAttribute("href"), "\n";
}

Hope it helps.

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