murfitUK Posted December 29, 2008 Share Posted December 29, 2008 I've been trawling for some scripts and came across two small examples that I want to use, but they had different ways of checking if a submit button had been clicked. These were just small bits of code so obviously I'll expand on them. But I would like to know what the difference is and which one is the best to use.Suppose the button is named "clicked".The first example had:if ($_POST['clicked']) { do this }The second had:if (isset($_POST['clicked'])) { do this }They both seem to do the same thing on the limited testing I have done. I've had a look through php.net but can't quite see what the difference is. Thanks. Link to comment Share on other sites More sharing options...
jeffman Posted December 29, 2008 Share Posted December 29, 2008 PHP throws a warning if you try to access a variable that has never been set. You might not see the warning if the server admins have suppressed error-reporting, but it's there, and it's something to keep track of. Good writers check all post/get data before trying to access it. Link to comment Share on other sites More sharing options...
murfitUK Posted December 29, 2008 Author Share Posted December 29, 2008 So, isset($_POST['clicked']) is the best one to use to verify that the button has indeed been clicked? Link to comment Share on other sites More sharing options...
jeffman Posted December 29, 2008 Share Posted December 29, 2008 Using isset() or !empty() is essential to good programming, yes. Note that isset returns true if a variable has been set to false, 0, or an empty string. It returns false if the variable has not been set. (Like a checkbox that didn't get checked, so it's not part of the POST data.) So what isset() tells you about your button will depend on your form and any javascript that goes with it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.