0

Because of the layout I can't use another <form> tag so I am trying to use javascript to update the database.

Since the code is just changing the URL with window.location.href it's not reloading the page to run the if statement.

Here is my code:

$check_view = $member->getUsersView($user_id);
if((isset($_GET['view-all'])) && $check_view != $_GET['view-all'])
{
    $view_all = $_GET['view-all'];
    $database->query('UPDATE users SET view_all = :viewall WHERE id = :userid', array(':viewall' => $view_all, ':userid' => $user_id));
}

<div onclick="javascript:window.location.href = \'page.php?action=list&'.(isset($_GET['view-all']) && $_GET['view-all']=="No" && $check_view=="No" ? 'view-all=Yes' : 'view-all=No').'\';">'.(isset($_GET['view-all']) && $_GET['view-all']=="No" && $check_view=="No" ? 'Show All' : 'Hide').'</div>

Anyone able to give me a hand with this?

EDIT: Thanks to the help of Matt I was able to get it working:

if(isset($_GET['view-all']))
{
    $check_view = $member->getUsersView($user_id);

    if($check_view != $_GET['view-all'])
    {
        $view_all = $_GET['view-all'];

        $database->query('UPDATE users SET view_all = :viewall WHERE id = :userid', array(':viewall' => $view_all, ':userid' => $user_id));
    }
}

$users_view = $member->getUsersView($user_id);

$view_all = "No";
$hide_show = "Hide";
if((isset($_GET['view-all']) && ($_GET['view-all']=="No")) || ($users_view=="No"))
{
    $view_all = "Yes";
    $hide_show = "Show All";
}

$data = '<a href="page.php?action=list&view-all='.$view_all.'">'.$hide_show.'</a>';
3
  • 2
    Why use a <div> + onclick instead of just a regular <a>? Commented Jul 31, 2012 at 15:50
  • You need to show the client-side output for your DIV, not your server-side PHP code. Commented Jul 31, 2012 at 15:52
  • For the love of GOD please define your onclick function in a <script> block and then call the function from there! Commented Jul 31, 2012 at 16:04

1 Answer 1

1
<a href="page.php?action=list&view-all=<?=(isset($_GET['view-all']) && $_GET['view-all']=="No" && $check_view=="No" ? "Yes" : "No"?>">
<?=isset($_GET['view-all']) && $_GET['view-all']=="No" && $check_view=="No" ? 'Show All' : 'Hide'?>
</a>

The above code will work but is UGLY AS SIN. Try cleaning it up:

<?php
$view_all = "No";
$hide_show = "Hide";
if (isset($_GET['view-all']) && $_GET['view-all']=="No" && $check_view=="No") {
    $view_all = "Yes";
    $hide_show = "Show All";
}
?>

<a href="page.php?action=list&view-all=<?= $view_all ?>"><?= $hide_show ?></a>
Sign up to request clarification or add additional context in comments.

3 Comments

Any idea why "Show All" displays again after I click it? I have to click it a second time to get "Hide" to display. The "Hide" works fine, if I click it "Show All" displays.
@Draven not really, I just reorganized your code so it would be cleaner; I didn't check it for logical accuracy (sorry).
No problem. I just realized why. I am defining $check_view before it's updating the database with the new value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.