简体   繁体   中英

PHP radio buttons and submit()

I am using On/off radio button inside my wordpress to hide post from a particular category and I've done well so far,

I mean when i set the button to 'On', posts from category whose id is 495 disappears and when i turn if off the posts comes back,

here is the problem i am facing,

I set the button to 'Off' so the posts from category id 495 disappears then i click on 'older posts' link and goes to previous pages but then when i chose 'On' i automatically jumped back to page number 1 :S

why is this happening ? Why am i automatically jumping back to 1st page when click 'On' ? :S

here is the code,

This is the header code,

<?php
session_start();
if (isset($_POST['r1'])){
    $_SESSION['r1']=$_POST['r1'];
    }
?>

these are my two radio buttons,

<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo ($_SESSION['r1'] == "o") ? 'checked="checked"' : ''; ?> />On</li>
<li><input type="radio" name="r1" value="p" onClick="submit();" <?php echo ($_SESSION['r1'] == "p") ? 'checked="checked"' : ''; ?> />Off</li>

and this is the code in index.php,

<?php 
    if ($_POST['r1']=='o') // Family Filter !!! If 'ON'
    query_posts('cat=-495'); // Remove Post from Category whose Id is 495
    else {echo "";} // When off is selected
?>

is there something wrong with the code inside index.php ??

Once form is submitted parameter page is lost. It appears in query string after clicking on "Older posts". By default it equsls to 1.

You have pass it to query_post function and use something like this

query_posts('cat=-495&page='.$page);

where

$page=(int)$_GET['page']; // I don't remeber exactly parameter name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM