Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/71201-solved-separate-script-files/
Share on other sites

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")

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!  :-\

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.

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).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.