So hello everyone I have a big problem right now with a button that is made depending on the variables that are sent to the page in this case it's a button the when it's clicked it will access a DB and extract everything in the DB and put it into excel format. The problem right now is when the button is made it always execute the DB search and file making. I know it has to do it because of the way the button is made right now, is there anyway of changing this?
if ($action_db) echo "<button class=\"btn\" onClick='".getUDB()."'>" . DBUser . "</button>";
to this:
if ($action_db) echo "<but开发者_开发技巧ton class=\"btn\" onClick=\"getUDB()\">" . DBUser . "</button>";
and sending the event trough java-script so it executes the getUDB() method that's on my PHP file?
Thanks.
OK so I went and used part of the suggestion that Sam said and put it on a JQuery and the change is like this
if ($action_db) echo "<button class=\"btn\" id=\"UDB\">" . DBUser . "</button>";
JQuery:
<script type="text/javascript">
$(document).ready(function(){
$("#UDB").click(function(){
<?getUDB();?>
});
});
OK so the problem is that when I select the clients menu or any other part of the menu to access that part of the webpage it's automatically doing the getUDB method and won't let me access anything else. Any suggestions???
If you don't want the PHP function getUDB()
to be called when you load the page, then you're right - you want to move this out into a Javascript function.
By doing echo "foo" . getUDB() . "bar"
, I'm assuming your page is loading slowly. To solve this, you can use Javascript with an AJAX call. So you probably want to:
- Create a separate PHP file that calls
getUDB()
. - Create a Javascript function on your original page that issues a GET request to this new, separate PHP file. It's easiest to do this using jQuery, which makes AJAX requests nice and easy - check out this page.
- Write the Javascript needed to handle the response from that page appropriately.
SitePoint has a good tutorial on jQuery and AJAX, check it out.
You need to send a request to the server to initiate anything server-side in response to an event, either by a traditional post-back or via AJAX.
I suggest you make your buttons into forms, substituting the ID of the user for the value of the hidden input:
if ($action_db) {
?>
<form action="/getUDB">
<input type="hidden" name="DBUser" value="<?= DBUser ?>" />
<button class=\"btn\" type="submit"><?= DBUser ?></button>
</form>
<?
}
And then set up a second script to handle POST requests to /getUDB
精彩评论