0

I have a problem with using , during click of the link, I need to update a field in the database and redirect to another page after.

I have this code:

<a href="#" onclick="<?php  

                        $sql="UPDATE MyDB.mytable SET Date = '".date("Y-m-d H:i:s")."' 
                                WHERE ID='" . $id . "'";
                        if (!mysql_query($sql)) ///Cannot query
                        {
                            $logger->error(mysql_error()); 
                        }

                        if ($sql)
                        {
                             $logger->debug('OK');
                        }
                        else
                        {
                             $logger->debug( 'NOt OK');
                        }
                      ?>"> </a>

After the php end tag '?>' can I add my path to be directed to? like:

<a href="#" onclick="<?php  

                        $sql="UPDATE MyDB.mytable SET Date = '".date("Y-m-d H:i:s")."' 
                                WHERE ID='" . $id . "'";
                        if (!mysql_query($sql)) ///Cannot query
                        {
                            $logger->error(mysql_error()); 
                        }

                        if ($sql)
                        {
                             $logger->debug('OK');
                        }
                        else
                        {
                             $logger->debug( 'NOt OK');
                        }
                      ?> ../index.php"></a>

Is that even possible? What is the right thing to do it? Thanks a lot!

1

3 Answers 3

1

this is not the right way.

There can be multiple ways you could take to do this. But I'd suggest you to place the DB update code in the target page (that I assume you mentioned as index.php). If you only want to trigger the DB update code on clicking of the link, use a page in middle to redirect the flow.

So, your page flow will be:

Current Page (Link Clicked, simple href to middleman.php) ==> middleman.php (just run the DB update code here and use header Location syntax to index.php) ==> index.php

codes:

page in which you have the link source.php

<.... html contents ....>
<a href='middleman.php'>Visit the page</a>
<.... more html contents ....>

middleman.php

<?php  

$sql="UPDATE MyDB.mytable SET Date = '".date("Y-m-d H:i:s")."' WHERE ID='" . $id . "'";
if (!mysql_query($sql)) ///Cannot query
{
   $logger->error(mysql_error()); 
}

if ($sql)
{
 $logger->debug('OK');
}
else
{
 $logger->debug( 'NOt OK');
}

header("Location: index.php"); //redirects to index.php
?>

index.php

do whatever you want
0

When a page is rendered, php code will run once. Whenever you see a webpage, it's only html, always, with no live access to the php code. So, you cannot execute php blocks directly from for example a javascript event. In your case the sql query would execute once, when you load the page.

kishu27 just posted one of the proper ways to do it, and the best option for you in this case. If you only wanted to update the database, without being redirected to another page, an ajax call to a php page with the database code would be a good alternative.

2
  • thanks for the ref Joel. I would however like to emphasize that any AJAX/JS based method is not a good practice considering some client's bad compatiblity with JS, or the JS framework in use. Its best if it's handled on backend and the flow that I described has a minimal user experience trouble. Querying through ajax will leave user wondering for a second or two whether the click was even accepted or not, sometimes making them click the link more than once :)
    – kishu27
    Mar 22, 2012 at 8:13
  • 1
    Oh, absolutely. In this case your approach is definitely the best. I was merely trying to point out the two most common ways to communicate with php scripts, since @tinks clearly went about it the wrong way. Cheers. Mar 22, 2012 at 8:25
-1

Using location.pathname

<a href="#" onclick="location.pathname='<?php ... ?>'"></a>
3
  • is using href="#" correct? it gives me an error saying that cannot open the internet site internet site and its specifying its own url and not the path assigned to location.pathname..
    – tinks
    Mar 22, 2012 at 7:38
  • href="#" is valid, but the approach ZloiGremlin suggested is not what you want to do in this case. You're getting the error because of what the php block returns when it's executed. Mar 22, 2012 at 7:48
  • you can change event onclick to "location.pathname='<?php ... ?>';return false;" Mar 22, 2012 at 7:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.