dbo Posted August 27, 2007 Share Posted August 27, 2007 What's the best way to implement a function/variable to determine if the page is a post back in PHP. I've got a few ideas but I'm interested in what you guys have done. Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/ Share on other sites More sharing options...
Fadion Posted August 27, 2007 Share Posted August 27, 2007 If i remember it right, "ispostback" in asp determines if a page is posted, so i guess isset($_POST['variable']) is much the same. Maybe im wrong anyway. Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/#findComment-335309 Share on other sites More sharing options...
dbo Posted August 27, 2007 Author Share Posted August 27, 2007 Yeah that's right... but in ASP/ASP.NET you don't have to specify the name of any variables... was hoping for something more generic. Then you've also got the case of image buttons, etc. The best thing I can come up with right now relies on a naming convention for the html forum elements... btnSearch (for example). Then search the post array for any variables prefixed with btn, then check if it's set or in the case of an image button if x or y coordinates exist. Seems like there has to be a better way though. I think just being able to say, IsPostBack at the top of my form would be more elegant, I dunno. Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/#findComment-335313 Share on other sites More sharing options...
lemmin Posted August 27, 2007 Share Posted August 27, 2007 if (count($_POST)) That should return true if the form posted a variable and false if not. Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/#findComment-335321 Share on other sites More sharing options...
tibberous Posted August 27, 2007 Share Posted August 27, 2007 if(count($_POST) > 1) Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/#findComment-335323 Share on other sites More sharing options...
trq Posted August 27, 2007 Share Posted August 27, 2007 <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { } ?> Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/#findComment-335325 Share on other sites More sharing options...
dbo Posted August 27, 2007 Author Share Posted August 27, 2007 Great! Thanks for the replies guys... you've given me some good ideas. Are there any drawbacks or "gotchas" with the count method on the post array? For some reason I'm more partial to that solution initially. Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/#findComment-335413 Share on other sites More sharing options...
trq Posted August 27, 2007 Share Posted August 27, 2007 Are there any drawbacks or "gotchas" with the count method on the post array? The only gotcha would be if all fileds where left blank prior to posting. count($_POST) will still return 0. Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/#findComment-335422 Share on other sites More sharing options...
dbo Posted August 27, 2007 Author Share Posted August 27, 2007 Hrmm. Won't buttons always have a value unless someone is screwing with the form (which if they are I don't want to play nice with them anyways!)? Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/#findComment-335424 Share on other sites More sharing options...
lemmin Posted August 27, 2007 Share Posted August 27, 2007 It will still work if the fields are left blank, because post will contain keys of the field names, with null values. Also, if the submit button has a name, that will always be in the $_POST array when the button is clicked. Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/#findComment-335425 Share on other sites More sharing options...
dbo Posted August 27, 2007 Author Share Posted August 27, 2007 Ok, great guys. You've given me some good insight to consider. I'll look at both methods more thoroughly before making my final decision. Ultimately this is going to be implemented into a quasi rapid application development framework for PHP. Still trying to do some planning and right now and just throwing out ideas/asking for suggestions as things come to mind. I appreciate all the help. Link to comment https://forums.phpfreaks.com/topic/66890-solved-ispostback-in-php/#findComment-335429 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.