0

i am really new to stackoverflow and actually dont have any clue about php.

I have a json array which looks like

[{"betreff":"h"},{"betreff":"VS Code fehlt"}]

i´ve created (copied :D) a foreach loop to output the "h" and "VS Code fehlt"

function display_array_recursive($json_rec){

if($json_rec){
    foreach($json_rec as $key=> $value){
        if(is_array($value)){
            display_array_recursive($value);
        }else{
            echo '<a href="#"  onClick="test("'.$value.'")">';
        }
    }
}

As we can see now the "h" and "VS Code fehlt" parts are being outputted as links. I want to make it so that whenever I click on the link a new get requests should be sent out with the giving value.

function test($value){when pressed = send new file_get_contents request to localhost/api/ticket/{$value}}

i hope i could describe good enough what i want. Thanks in advance, please inform me if anything is unclear

durmaz
  • 1
  • 1

1 Answers1

0

You can't use PHP to make GET requests like that (unless you use a hidden IFRAME, as far as I know). You need to use JavaScript for that, using any of those APIs (if you're using vanilla JavaScript):

Example:

<script>
    function test(url)
    {
        fetch(url);
    }
</script>

<?php

function display_array_recursive($json_rec)
{
    if (!$json_rec) {
        return;
    }
    foreach($json_rec as $key=> $value){
        if (is_array($value)) {
            display_array_recursive($value);
            continue;
        }
        echo '<a href="#" onClick="test(\''
            . htmlspecialchars(
                $value,
                ENT_QUOTES|ENT_HTML5,
                'UTF-8'
            )
            . '\')">Text</a>';
    }
}

Notice the function display_array_recursive is escaping the HTML. It's also using ' (single quote) for the test arguments. It's already using " (double quote) for the element attribute values.

I suggest you to read:

Pedro Amaral Couto
  • 2,056
  • 1
  • 13
  • 15