phpFirstJoy Posted September 29, 2007 Share Posted September 29, 2007 Hi, I was reading the PHP manual and came across a form example where the action redirects to a separate PHP script file. I'm just wondering if this is the better way to do it? I normally put everything in the same file as my form. For example, if I have a MakePost.php file, that one file will contain the form, the script, everything. Should I make another file like addpostscript.php and then in my MakePost.php, redirect the action to addpostscript.php? I hope this makes sense! ??? -- Unsure Quote Link to comment https://forums.phpfreaks.com/topic/71201-solved-separate-script-files/ Share on other sites More sharing options...
jbingman Posted September 30, 2007 Share Posted September 30, 2007 you could use a $_GET variable and make that a seperate page inside of the one file. //form content ... ... if($_GET['page']== Addpost) { //execute this script } is that what your looking for? Quote Link to comment https://forums.phpfreaks.com/topic/71201-solved-separate-script-files/#findComment-358144 Share on other sites More sharing options...
deadimp Posted September 30, 2007 Share Posted September 30, 2007 Look into the include() series (include, include_once, require, require_once) of functions. What that does is execute the code in that separate file in the scope of the main file. One random thing to keep in mind is that you can have a script return to stop execution, but it only works at that file's level: index.php <?php echo "Hello<br>"; $type='return'; require("test.php"); echo "I'm still alive<br>"; ?> test.php <?php echo "Included script<br>"; if ($type=='return') return; //Have the script return if ($type=='die') die(); //Kill it all echo "Included still alive<br>"; ?> Output for $type='return': Hello Included script I'm still alive $type='die' Hello Included script If you're looking to redirect a page to another immediately (without a meta-refresh), use header("Location: $url") Quote Link to comment https://forums.phpfreaks.com/topic/71201-solved-separate-script-files/#findComment-358197 Share on other sites More sharing options...
phpFirstJoy Posted September 30, 2007 Author Share Posted September 30, 2007 Mmmm I think I'll just copy and paste the example from the manual: <form action="foo.php" method="post"> Name: <input type="text" name="username" /><br /> Email: <input type="text" name="email" /><br /> <input type="submit" name="submit" value="Submit me!" /> </form> Let's assume that the form above is located in SuperDuper.php. So when SuperDuper.php loads, the form appears in the browser. When the user enters in the data and then clicks submit, the data is then "posted" to foo.php. foo.php has some coding that processes the data entered from the user. This is what the manual does but what I normally do is the following: <form action="SuperDuper.php" method="post"> Name: <input type="text" name="username" /><br /> Email: <input type="text" name="email" /><br /> <input type="hidden" name="status" value="submitted" /> <input type="submit" name="submit" value="Submit me!" /> </form> So the above form is still in SuperDuper.php but when the user clicks submit, the data is "posted" to SuperDuper.php (note the hidden field). There's some script/code at the beginning of SuperDuper.php that checks to see if the form was submitted by the user. If so, it processes the user entered data. It doesn't call upon another script (such as foo.php). It does everything in this one file. Which method of programming is better? I hope that clears up any confusion! :-\ Quote Link to comment https://forums.phpfreaks.com/topic/71201-solved-separate-script-files/#findComment-358215 Share on other sites More sharing options...
steelmanronald06 Posted September 30, 2007 Share Posted September 30, 2007 there isn't really a difference. some people like to divide logic up. Such as frontend and backend. The form people see is your frontend, everything a person can see is frontend/part of your template. the foo.php, where the form submits to, is a backend file. since people never see the backend file it executes and redirects: //logic here to process the form on foo.php //redirect foo.php to a success.php page it is just a way to orgainze your site. Quote Link to comment https://forums.phpfreaks.com/topic/71201-solved-separate-script-files/#findComment-358388 Share on other sites More sharing options...
phpFirstJoy Posted September 30, 2007 Author Share Posted September 30, 2007 But wouldn't it affect the time it takes to load/process or something of the sort depending on which method I use? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/71201-solved-separate-script-files/#findComment-358547 Share on other sites More sharing options...
Daniel0 Posted September 30, 2007 Share Posted September 30, 2007 But wouldn't it affect the time it takes to load/process or something of the sort depending on which method I use? :-\ No. You'll have to load multiple pages whether you choose to use the same file or multiple files to handle the form and the backend (data processing when the form has been submitted). Quote Link to comment https://forums.phpfreaks.com/topic/71201-solved-separate-script-files/#findComment-358596 Share on other sites More sharing options...
phpFirstJoy Posted October 1, 2007 Author Share Posted October 1, 2007 Oh, ok. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/71201-solved-separate-script-files/#findComment-359099 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.