Deleting from mysql | Member | | Join Date: Jan 2008
Posts: 59
| | |
Hi,
There is a 'delete' link in home.php page, when it is clicked a row in the database is deleted. How do I write the code for this. I am using this line of code
[HTML]<a href='delete.php?id=1'>Delete</a>[/HTML] ,
then when link is clicked, it is directed to delete.php page. At the last I have used this line of code to redirect back to home.php page
[HTML]echo '<META HTTP-EQUIV="Refresh" Content="0; URL=home.php">';
exit;[/HTML]
But here the problem is when I click the link, the delete.php page is entered and a blank page appears and again return to home.php page. How can I code in such a way that, when the delete link is clicked it should not redirect to delete.php page but stay in the home.php page itself and perform the actions in delete.php page.
please help me to solve this problem
With regards
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,938
| | | re: Deleting from mysql
You would have to create an AJAX request.
http://www.tizag.com/ajaxTutorial/ajaxphp.php
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 311
| | | re: Deleting from mysql
I have not done this myself so I do not know for sure, but I don't think you need a background query using Ajax, although it may be a more elegant solution.
I think your problem is what you are echoing. Try to use the "header" function of PHP to do a redirect to the page you wish to show. This will give you an error if you already print out anything to the user's browser before you invoke header() but you can write your delete.php script so that this does not happen.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,938
| | | re: Deleting from mysql
OP Said : Quote:
How can I code in such a way that, when the delete link is clicked it should not redirect to delete.php page but stay in the home.php page itself and perform the actions in delete.php page.
He wants to stay ON home.php. Therefore, you use ajax to perform a query in the background without a page refresh.
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 311
| | | re: Deleting from mysql
No need to bicker about this. The elegant solution is of course to stay on the home page and use a background query, but that is not what he was trying to do with his script and the echo statement to "refresh".
Another way to do it is to do the tasks in the default.php script and then redirect to the script that presented the home page. I just mention that possibility and point out the header() function.
There are some reasons to refresh the page and not just do a background query using, for example, Ajax. It could be that the results that are shown on the page need to be updated after the deletion, and in many cases this can be accomplished a lot easier by just rebuilding the page rather than moving elements around with dynamic html or javascript.
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,747
| | | re: Deleting from mysql
And lets not forget, while AJAX is a very handy and good looking thing, having any feature depend on client-side code is a very bad idea. You have no guarantee that the client-side code is actually run by your client.
I would recommend having a link that sends you to a delete.php page, which would delete the row, give a success message, and then send you back.
Alternatively, you could always have JavaScript re-write your links...
That is; have PHP output the links as if you were using the above scenario, but print a JavaScript function in the header that would re-write all the delete links, replacing the link with a JavaScript function that would send a AJAX request.
That way, it will look good on browsers that support it without breaking it on browsers that don't.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,938
| | | re: Deleting from mysql Quote:
Originally Posted by Atli And lets not forget, while AJAX is a very handy and good looking thing, having any feature depend on client-side code is a very bad idea. You have no guarantee that the client-side code is actually run by your client. While this is true, it's slightly over-used.
Anyone browsing with javascript turned off, should not be browsing.
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,747
| | | re: Deleting from mysql Quote:
Originally Posted by markusn00b While this is true, it's slightly over-used.
Anyone browsing with javascript turned off, should not be browsing. Even so, you should always assume your user is the dumbest person on the planet, using the least helpful browser available, even if that is never the case.
In any case. If you have your web depend on JavaScript, at least try to detect if your visitors have their JavaScript disabled and inform them that the 1980's are over.
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Deleting from mysql Quote:
Originally Posted by markusn00b While this is true, it's slightly over-used.
Anyone browsing with javascript turned off, should not be browsing. Several people that I know have NoScript installed to protect themselves against XSS ( https://addons.mozilla.org/en-US/firefox/addon/722). One of these individuals develops for a well-known internet security company -- not an internet lightweight by any means.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,938
| | | re: Deleting from mysql
Once again, I stand with my pants down, my pride dissolved.
| | Member | | Join Date: Jan 2008
Posts: 59
| | | re: Deleting from mysql
Thank you for the reply' s.
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas
Posts: 5,435
| | | re: Deleting from mysql Quote:
Originally Posted by markusn00b Once again, I stand with my pants down, my pride dissolved. I think they have a pill for that....
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,747
| | | re: Deleting from mysql Quote:
Originally Posted by pbmods Several people that I know have NoScript installed to protect themselves against XSS I never leave localhost without it :)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,414 network members.
|