Home > Back-end >  button which open array links on same page in php
button which open array links on same page in php

Time:08-28

I want to open webpage through array link data while pressing the button on remaining same page, I've tried many time houres and houres work but my code did not work. I try to detect where is fault but not understand , how to solve it. Please help.

This is code of open webpage by array link data while pressing button or pagination.

             <!DOCTYPE html>
<html>
<body>


<?php


$data=array("www.google.com","www.google.com/1","www.google.com/2");


foreach ($data[0] as $url) {

$ch=curl_init();

curl_setopt($ch, CURLOPT_URL , $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$code=curl_exec($ch);


function openpage() {

global $code;
echo $code;

}

curl_close($ch);

}

?>

<button onclick= openpage();>open NEXT array PAGE</button>


</body>
</html>

CodePudding user response:

PHP is executed on the server and the javascript is executed on the browser.

It looks like you are trying to call a PHP function from javascript. You can only call a javascript function in the onclick event. And you have to populate the function call with a parameter being the page you want to go to.

CodePudding user response:

You mentioned ( did not opening link when click on it.). Buttons do not open links. Anchor tags do. If you really need a button, you can use the btn class to make it look like one.What you are doing is you are trying to print the response you get from the curl. That is not opening a page. Curls do not open new tabs. it returns a response from that particular link.

<?php

$data=array("www.google.com","www.google.com/1","www.google.com/2");
foreach ($data[0] as $url) { 

?>
 <a href='$url' target="_blank" >Click to Open</a>

<?php } ?>
  • Related