How to open href link by array one by one

please tell how can we open href link by array one by one … please see source code file and correct the code.

source code file

and check where is problem .

when i click the button , one element load and one link will open through array , and when i click again on countiue button, then 2nd element of array will load and 2nd link will open.

any one reply please.

<?php

$nextpage = array("https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL3+8QE&distance=20
", "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NG&distance=20
", "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NS&distance=20
");

foreach ($nextpage as $x) {

?>
<a href="<?php print $x; ?>"><button>Continue</button></a>
    
<?php

}

?>

Try this - I have tested the code and tree buttons will display, when clicked they work fine.
Good luck

please note: what i want?

I want to link wiil be open as when i click “1st time” on button then 1st element of array will load through “continue” button… similarly when again i click on “continue” button then 2nd element of the array will load in “continue” button… similarly when again 3rd time i click on “continue” button then 3rd element of the array will be load in “continue” button and link will open.

You have an example… update it to meet your needs.

You then increment through the array each button click.

I’m beginner in coding, so that please make it yourself, i"ve try many time, but incorrect code by me… so please help from your side.

Are there are any sites you know of that do what you want? I would like to see an example of what you are asking for.

i’ve stated my question above in this post with full explain , kindly see my question with source file which i share here and more explanation also i shared on my later replies.

This should get you started.

<!DOCTYPE HTML>

<html>

<head>
    <title>Untitled</title>

<script>
let link = new Array()
link[0] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL3+8QE&distance=20"
link[1] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NG&distance=20"
link[2] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NS&distance=20"

let intlinkIndex = 0;

function writeLink() {
    if ( intlinkIndex == link.length ) intlinkIndex = 0;
    document.getElementById('mylink').innerHTML = '<a href="' + link[intlinkIndex] + '">' + link[intlinkIndex] + '</a>';
    intlinkIndex++;
}
</script>
</head>

<body>

<button onclick="writeLink();">Click Me</button>
<div id="mylink"></div>

</body>
</html>

Slightly different function but does the same thing without the if.

function writeLink() {
let print = document.getElementById('mylink');
    print.innerHTML = '<a href="' + link[intlinkIndex] + '">' + link[intlinkIndex] + '</a>';;
    intlinkIndex = (intlinkIndex + 1) % (link.length);
}

@benanamen YOUR above code is nice and work but small issue is still coming,… the array has three element… and it should be work only for three element and after that it should be stop… but when we click four time on button , array will start again from start… i want just one thing, array should not start again from start when press it four time…
in my mind it will be better, button should be hide or block when we want to press it four time so that array will not repeat again from start again and again…
I hope you understand this issue and again will help me in this regards,
your thankful…

Give this a try.

<!DOCTYPE HTML>

<html>

<head>
    <title>Untitled</title>

<script>
let link = new Array()
link[0] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL3+8QE&distance=20"
link[1] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NG&distance=20"
link[2] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NS&distance=20"

let intlinkIndex = 0;

function writeLink() {
  if (intlinkIndex >= link.length) {
    let btn = document.getElementById('btn');
    btn.style.display = 'none';
    mylink.style.display = 'none';
  }
    document.getElementById('mylink').innerHTML = '<a href="' + link[intlinkIndex] + '">' + link[intlinkIndex] + '</a>';
    intlinkIndex++;
}
</script>
</head>

<body>

<button id="btn" onclick="writeLink();">Click Me</button>
<div id="mylink"></div>

</body>
</html>

@benanamen , wao, yes… it works great… you are really a great coder… unbelievable result.
thanks a lot.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service