PHP:标签的帖子名称,然后在链接页面上使用

PHP:<a>标签的帖子名称,然后在链接页面上使用

问题描述:

so basically lets say I have a page called page-with-links.php:


<ul>
    <li><a href="page-with-checkboxes.php" name="blue">Blue</a></li>
    <li><a href="page-with-checkboxes.php" name="red">Red</a></li>
    <li><a href="page-with-checkboxes.php" name="green">Green</a></li>
    <li><a href="page-with-checkboxes.php" name="yellow">Yellow</a></li>
    <li><a href="page-with-checkboxes.php" name="orange">Orange</a></li>
</ul>


and a page called page-with-checkboxes.php:

<h3>You selected:</h3>

<form>
    <input type="checkbox" name="blue" />
    <label for="blue"> Blue</label><br>

    input type="checkbox" name="red" />
    <label for="red"> Red</label><br>

    input type="checkbox" name="green" />
    <label for="green"> Green</label><br>

    <input type="checkbox" name="yellow" />
    <label for="yellow"> Yellow</label><br>

    <input type="checkbox" name="orange" />
    <label for="orange"> Orange</label><br>
</form>



I would like to be able to use the name value of the <a> tag to check the related box on the next page.

For example, if the user clicks 'green' on page-with-links.php, I would like the checkbox with name="green" to be checked when page-with-checkboxes.php loads.

I hope I've been clear enough. Thanks in advance to anyone who can help!

write line of code on your page page-with-links.php

<ul> <li> <a href="page-with-checkboxes.php?value=blue" name="blue">Blue</a> </li> <li> <a href="page-with-checkboxes.php?value=red" name="red">Red</a></li> <li> <a href="page-with-checkboxes.php?value=green" name="green">Green</a></li> <li> <a href="page-with-checkboxes.php?value=yellow" name="yellow">Yellow</a></li> <li> <a href="page-with-checkboxes.php?value=orange" name="orange">Orange</a></li> </ul>

and write follow line of code on page called page-with-checkboxes.php:

<input type="checkbox" name="blue" <?php echo $_GET['value']=='blue'?'checked':''; /> <label for="blue"> Blue</label><br> <input type="checkbox" name="red" <?php echo $_GET['value']=='red'?'checked':''; /> <label for="red"> Red</label><br> <input type="checkbox" name="green" <?php echo $_GET['value']=='green'?'checked':''; /> <label for="green"> Green</label><br> <input type="checkbox" name="yellow" <?php echo $_GET['value']=='yellow'?'checked':''; /> <label for="yellow"> Yellow</label><br> <input type="checkbox" name="orange" <?php echo $_GET['value']=='orange'?'checked':''; /> <label for="orange"> Orange</label>

Can you try this,

In page-with-links.php:

  <li><a href="page-with-checkboxes.php?type=blue" name="blue">Blue</a></li>

In page-with-checkboxes.php:

 <input type="checkbox" name="blue" <?php echo $_GET['type']=='blue'?'checked':''; />
 <label for="blue"> Blue</label><br>

The a tag attributes does not affect on the request.

You must follow this :

<ul>
    <li><a href="page-with-checkboxes.php?name=blue" name="blue">Blue</a></li>
    <li><a href="page-with-checkboxes.php?name=red" name="red">Red</a></li>
    <li><a href="page-with-checkboxes.php?name=green" name="green">Green</a></li>
    <li><a href="page-with-checkboxes.php?name=yellow" name="yellow">Yellow</a></li>
    <li><a href="page-with-checkboxes.php?name=orange" name="orange">Orange</a></li>
</ul>

And in your page-with-checkboxes.php get this values :

<?php
    if(isset($_GET['name']) {
        $color = $_GET['name'];
    } else {
        $color = null;
    }
?>

<form>
    <input type="checkbox" name="blue" <?php print $color=="blue" ? "checked" : "" ?> />
    <label for="blue"> Blue</label><br>

    input type="checkbox" name="red" <?php print $color=="red" ? "checked" : "" ?> />
    <label for="red"> Red</label><br>

    input type="checkbox" name="green" <?php print $color=="green" ? "checked" : "" ?> />
    <label for="green"> Green</label><br>

    <input type="checkbox" name="yellow" <?php print $color=="yellow" ? "checked" : "" ?> />
    <label for="yellow"> Yellow</label><br>

    <input type="checkbox" name="orange" <?php print $color=="orange" ? "checked" : "" ?> />
    <label for="orange"> Orange</label><br>
</form>