little olmi
little olmi

Reputation: 5

Changing yes/no value in database, on click of hyperlink

sorry about my limited coding skills and so on, but hopefully you can see what I am attempting. I want to scrap the form checkboxes and have 2 simple 'yes' 'no' hyperlinks, dependant on if image is hidden or shown. Will javascript do this? This is what I have so far, which was working but like I say, I just want 2 links instead of checkboxes.

if ($_POST['option']) {    

    if ($_POST['option'] == 'yes') {$hidden = 0;}
    if ($_POST['option'] == 'no') {$hidden = 1;}

@mysql_query('UPDATE Image SET Hidden = ‘.$hidden.’ WHERE ID = '.$image->ID.'');

        header ('Location: ' . $_SERVER['REQUEST_URI']);
            exit;    
            }

<p>Show image? 
        <form method="post" action="?">
<input type="checkbox" name="option" value="yes">Yes 
<input type="checkbox" name="option" value="no" >No 
<input type="submit" name="submit" value="Go!" /> 
        </form>

So then I can have HTML such as - Show Image? Yes / No (This image is shown) or (this image is not shown)

Any points in the right direction would be greatly appreciated, Many thanks!

Hi Guys, Sorry, I don’t think I explained myself properly. All images have a default value of ‘Hidden = 0’ in the image database table, so they are all currently shown on the page. Here is the code -

// there is  some SQL here that fetches all images
// here is the actual code that shows images:

foreach($ids as $id)
    {
    $tmp = new Image($id,true);
    if (!$tmp->ID) continue;
<p>
<img src=”/myurl/’.$tmp->ID.’.jpg”>
</p>
}

What I want is, in that loop, underneath each image tag, is to have some HTML :

<p>Show Image? Yes / No </p>

I want the ‘yes’ and ‘no’ to be hyperlinks, which state will depend on the images ‘Hidden’ value. So if the image is shown (all currently are), the word ‘Yes’ won’t be a clickable hyperlink, only ‘No’ will be. If I then click ‘No’ I need it to post a query on click, to set Hidden = 1, to hide the image. If the image is already hidden, then ‘Yes’ would be the only clickable link, which if clicked would post value Hidden = 0, so the image is shown.

I hope that makes sense. The other problem I have is the fact that there are multiple images, so the form or whatever system I use, needs to distinguish which image it is changing the Hidden value for. In the code, the image’s unique id field is accessed like this: $tmp->ID

Upvotes: 0

Views: 434

Answers (4)

little olmi
little olmi

Reputation: 5

Just to reaffirm, the following form is my latest revision and works perfectly, updating the database and radio buttons on the page. However, it is still not the solution I want. I want hyperlinks only, as in the above examples posted by Boshi and Class, but for some reason my site won't accept those urls, the system churns me out to a different page and nothing works. Here is the latest revision which works. Is there another solution? -

if ($_POST[''.$tmp->ID.'']) {    

    if ($_POST[''.$tmp->ID.''] == 'yes') {$hidden = 1;}
    if ($_POST[''.$tmp->ID.''] == 'no') {$hidden = 0;}

@mysql_query('UPDATE Image SET Hidden = '.$hidden.' WHERE ID = '.$tmp->ID.' LIMIT 1');

        header ('Location: ' . $_SERVER['REQUEST_URI']);
            exit;    
            }

        <p>Hide image? 
        <form method="post" action="?">
<input type="radio" name="'.$tmp->ID.'" value="yes" '.($tmp->Hidden ? 'checked' : '').'>Yes 
<input type="radio" name="'.$tmp->ID.'" value="no" '.(!$tmp->Hidden ? 'checked' : '').'>No 
<input type="submit" name="submit" value="Go!" /> 
        </form>

Upvotes: 0

Boshi
Boshi

Reputation: 230

Try this

if(isset($_GET['cmd']))
{
    if($_GET['cmd']=='yes')
    {$hidden = 0;}
    if($_GET['cmd']=='no')
    {$hidden = 1;}

@mysql_query('UPDATE Image SET Hidden = '.$hidden.' WHERE ID = '.$image->ID.'');

        header ('Location: ' . $_SERVER['REQUEST_URI']);
            exit;    
           }

?>
<p>Show image? <br />
      <a href="yesNoLink.php?cmd=yes">Yes</a> <br /> <!--Replace page url according to yours -->
      <a href="yesNoLink.php?cmd=no">No</a>

Upvotes: 0

Shannon
Shannon

Reputation: 837

Easiest way would be to use a hidden input field that will be submitted with the rest of your form and using javascript to change the value of that hidden field.

HTML:

 <p>Show image? 
            <form method="post" action="?">
    <input type="hidden" name="option" value="default" />

    <a href="#" class="option" data-value="yes">Yes</a>
    <a href="#" class="option" data-value="no">No</a>
    <input type="submit" name="submit" value="Go!" /> 
            </form>

JS (requires JQuery):

$(".option").click(function (e) {

    $("input[name=option]").val($(this).attr("data-value"));

});

Then when you submit the form $_POST should have the "option" value.

Upvotes: 0

Class
Class

Reputation: 3160

If you want hyperlinks just use something like

<a href="myphpfile.php?option=yes">Yes</a>
<a href="myphpfile.php?option=no">No</a>

Also you will probably want to use single quotes ''s and not 's for your query. Also I'd recommend using mysqli or PDO so you can use prepared statements instead of mysql_ functions which are prone to mysql injections.

Upvotes: 1

Related Questions