Passing value from HTML link to use in php if statement on next page

56

I currently have a menu with 4 links, and each link goes to its own php file/page like so:

<!--Page One-->
<a href="temp_one.php"><h5 >Template One</h5></a>
<a href="temp_two.php"><h5 >Template Two</h5></a>
<a href="temp_three.php"><h5 >Template Three</h5></a>
<a href="temp_four.php"><h5 >Template Four</h5></a>

I'm refactoring this so that I only use one single file that conditionally sets the HTML for the page depending on which link was selected, probably by using GET

I want to change it to get values for each link that direct to a new single page, template.php, and use the value for the page so that the new url would effectively look like this:

template.php?1
template.php?2
template.php?3
template.php?4

So essentially the same as doing this:

<a href="template.php?1"><h5 >Template One</h5></a>
<a href="template.php?2"><h5 >Template Two</h5></a>
<a href="template.php?3"><h5 >Template Three</h5></a>
<a href="template.php?4"><h5 >Template Four</h5></a>

The reason is that I want to be able to use the values in an if statement on template.php so that I know which HTML to use like so:

<!--Page Two, template.php-->
if(value = 1){
    //HTML for template one
}
elseif(value=2){
    //HTML for template two
}
elseif(value=3){
    //HTML for template three
}
else{
    //HTML for template four
}

How can I modify the html of the links to supply the proper value to use in the next page with an if statement?

739

Answer

Solution:

You can do like this:

Pass a variable indicating the template value through URL.

And in php filetemplate.php,$_GET that variable.

<a href="template.php?value=1"><h5 >Template One</h5></a>
<a href="template.php?value=2"><h5 >Template Two</h5></a>
<a href="template.php?value=3"><h5 >Template Three</h5></a>
<a href="template.php?value=4"><h5 >Template Four</h5></a>

And intemplate.php,

$value = isset($_GET['value']) ? $_GET['value'] : 1; // Where 1 is default value.

if($value = 1){
    //HTML for template one
}
elseif($value=2){
    //HTML for template two
}
elseif($value=3){
    //HTML for template three
}
else{
    //HTML for template four
}

People are also looking for solutions to the problem: php - Laravel eloquent cannot determine table

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.