Results 1 to 7 of 7

Thread: Post to another PHP file

  1. #1

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Post to another PHP file

    Can someone please show me a simple example where I can gather information from one form in a php file and then post it to another php file?

    This website gives a great example on how to post to itself http://www.tizag.com/phpT/examples/formvar.php but I would like to post to another file. Please let me know how to do that.

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Post to another PHP file

    The "Action" property of a form specifies where to post the form to, for example:

    HTML Code:
    <form name="myForm" method="post" action="another_page.php">
    The above would post to "another_page.php".
    Chris

  3. #3

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Post to another PHP file

    Great thanks, also in another_page.php I can grab the data that was submitted using the following code:

    Code:
    $Fname = $_POST['Fname'];
    print $Fname;

    Assuming $Fname was in the original php file.

  4. #4
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Post to another PHP file

    Thats right.

    On that tutorial your using it posts to itself using <?php echo $PHP_SELF;> well all that is doing is getting the filename and path of the current PHP script, and inserting it into the action property of the form. If you do View->Source on your page when loaded, you will see what it inserts.
    Chris

  5. #5

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Post to another PHP file

    Whats the benefit to posting to self?

    Whats the benefit to posting to another file?

    Im trying to figure out which to use where, now that I know how to do it.

  6. #6
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Post to another PHP file

    There really is no benefit... It all depends on what your doing.

    For example, the Search Dropdown menu on this page has a search bar, when you submit it, it submits to search.php (which is the main search page).

    If you go to search.php manually, and type in your search, then it will post to it self because that it the page that does the mysql transaction.
    My usual boring signature: Something

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Post to another PHP file

    You should think about form actions in the context of verbs and nouns and decide what is appropriate.

    Forget about PHP scripts for a second, if you may.

    Every location that you navigate to represents a resource. These are identified by URIs (Uniform Resource Identifiers); over HTTP, these are specifically URLs (Uniform Resource Locators).
    Code:
    http://example.com/
    http://example.com/books/
    http://example.com/books/search/
    HTTP defines some actions (verbs) which clients (web browsers, automated tools, people using telnet...) can apply to resources (nouns). The most common of these are GET and POST.

    GET means "give me a representation of this resource that I might be able to understand." The client tells the server what formats (e.g. HTML, plain text) and languages (e.g. US English) it prefers, and the server gives the client the best match out of those.
    Code:
    GET http://example.com/
    Accept: text/html, text/plain, text/*
    Accept-encoding: utf-16, utf-8, ISO-8859-1
    Accept-language: en-US, en, *
    POST means "take this data and do something logical and related with it." The action taken should be obvious from the noun (the URI) and the variables given in the POSTDATA. For example, I might want to add a new book to our example website.
    Code:
    POST http://example.com/books/
    ...
    name=Introduction+to+HTTP&author=penagate
    I post the book data to http://example.com/books/.


    Or I might want to search the database.
    Code:
    POST http://example.com/books/search/
    ...
    
    query=Introduction+to+HTTP
    Here it should be obvious that we are searching a database at http://example.com/ for a book about HTTP. It would make less sense to post this request to http://example.com/ or http://example.com/books/, since we are specifically performing a search.

    The server-side scripts which serve the actual content and perform the actions requested by the client can be organised differently again. There is nothing which requires one URI to represent one script. In actual fact, all of the above actions might be controlled by one file of PHP code.

    Your job as the system architect is to design a sane back-end architecture, a sane URI schema, and map the two together. In Apache you can achieve nice-looking URLs by using the mod_rewrite engine; other web servers have other methods. If you can't do this then you might have to create one script for each URI which then includes other scripts to do the actual work. Just don't let back-end architecture dictate your URIs and vice versa.
    Last edited by penagate; Nov 4th, 2008 at 01:00 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width