SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Evangelist Fergal's Avatar
    Join Date
    Nov 2003
    Location
    Ireland
    Posts
    492
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question re form action and fopen()

    PHP Code:
    <?php

    if(isset($_POST['name']) && !empty($_POST['name'])) {
    $name htmlentities($_POST['name']);

    $handle fopen('names.html''a');
    fwrite($handle"<br />".$name);
    fclose($handle);
    //if

    ?>

    <h4>Add your name to the list!</h3>
    <form action="simple.php" method="POST">
    Your name:<br />
    <input type="text" name="name" /><br />
    <input type="submit" value="Add Your Name">
    </form>
    The script above works fine for adding names to a list in a html page.

    I want to change the form action from "simple.php" to "names.html", so that once someone submits the form they are brought to the html page with the list of names. However, when I change the form action to "names.html", the name submitted in the form, is not added to the list of names in the html file. Why is that?

    How can I alter the script so that when the form is submitted the name is added to the list and the user is brought to the names.html page.

    Thanks!
    Fergal Crawley (Previous Username: Proudirish.com)
    Business Advice Forum - Webmaster and Business Forum < Get a free link & win $5,000
    Forum Coin New World Currency

  2. #2
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    219
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Why don't you just add a header('Location: names.html'); after the fclose($handle);

  3. #3
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What code processes your form data in the file 'names.html'

    For php code to work, the file must have the extension '.php'
    You can't simply change a php file extension to html and expect to work.

    Why do you need to do this anyway? Just echo your html in the php file.
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  4. #4
    SitePoint Evangelist Fergal's Avatar
    Join Date
    Nov 2003
    Location
    Ireland
    Posts
    492
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vectorialpx View Post
    Why don't you just add a header('Location: names.html'); after the fclose($handle);
    Thank you vectorialpx, that works perfectly.

    Thanks 2ndmouse, that helps me understand why it wouldn't work.

    Quote Originally Posted by 2ndmouse View Post
    ...Just echo your html in the php file.
    I want to create a new html file for the list of names submitted from the form.
    Fergal Crawley (Previous Username: Proudirish.com)
    Business Advice Forum - Webmaster and Business Forum < Get a free link & win $5,000
    Forum Coin New World Currency

  5. #5
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    does this help - untested, but it should work

    PHP Code:
    <?php 

    if(isset($_POST['name']) && !empty($_POST['name'])) { 
    $name htmlentities($_POST['name']); 

    $handle fopen('names.html''a'); 
    fwrite($handle"<br />".$name); 
    fclose($handle);
    $file file('names.html');
    echo 
    '<pre>';
    print_r($file);
    echo 
    '</pre>'
    //if 
    else{
    echo 
    '
    <h4>Add your name to the list!</h3> 
    <form action="simple.php" method="POST"> 
    Your name:<br /> 
    <input type="text" name="name" /><br /> 
    <input type="submit" value="Add Your Name"> 
    </form>'
    ;
    ?>
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  6. #6
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    926
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vectorialpx View Post
    Why don't you just add a header('Location: names.html'); after the fclose($handle);
    I want to add to this answer that the most proper way in this case is to do a redirection with the 303 "See Other" http status code, which is just intended for the purpose of redirecting from a POST request - this way you will avoid some minor edge-case inconveniences in some browsers. Moreover, Location requires an absolute URL. The code therefore should be something like that:

    PHP Code:
    header("Location: http://" .$_SERVER['HTTP_HOST']. "/names.html"true303); 

  7. #7
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    219
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    Location requires an absolute URL
    In a simple structure, it does not require.
    However, your 303 completion is a better practice.

    One other small thing: after the header it's recomended to add an exit;

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    926
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vectorialpx View Post
    In a simple structure, it does not require.
    What does it mean "in a simple structure"? Can you back up your claim? The HTTP specification clearly states that The field value consists of a single absolute URI.

  9. #9
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    219
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    What does it mean "in a simple structure"? Can you back up your claim? The HTTP specification clearly states that The field value consists of a single absolute URI.
    A simple structure is: a no-rewrite interface (just simple.php, no rewrite engine) and a browser (not talking about console).
    The www.w3.org directive is about general headers but the browser also accepts relative paths, that's what I wanted to say with "not required".
    The browser knows "where he is" and will redirect relative to the current folder.

    However, having an absoluteURL is a better practice but it's not required.
    In a simple script (like this - a simple form submit) you don't have to use an absolute URL.

    As an example of "not required", this works in any browser (or, tell me one that fails):
    PHP Code:
     <?php
    if( isset($_GET['redirect']) ) {
        
    header('Location: ' basename(__FILE__));
        exit;
    }
    echo 
    'Initial. <a href="' basename(__FILE__) . '?redirect">Test redirect</a>';
    ?>

  10. #10
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    926
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    It's interesting, I've found that in the next version of HTTP standard relative URL are to be allowed. So just to be safe, I'll wait till the new stadard becomes official for while.

    Yes, browsers have no problem with relative URLs, in fact I believe they should be officially allowed just to make life easier.

  11. #11
    SitePoint Evangelist Fergal's Avatar
    Join Date
    Nov 2003
    Location
    Ireland
    Posts
    492
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks again everyone for your very helpful replies. I am just learning PHP at the moment and the header function is working fine for the moment.

    Quote Originally Posted by vectorialpx View Post
    ...One other small thing: after the header it's recomended to add an exit;
    Thanks for that tip, I've added it to my script. What is the advantage of using exit; like that?
    Fergal Crawley (Previous Username: Proudirish.com)
    Business Advice Forum - Webmaster and Business Forum < Get a free link & win $5,000
    Forum Coin New World Currency

  12. #12
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    219
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Proudirish.com View Post
    Thanks for that tip, I've added it to my script. What is the advantage of using exit; like that?
    After the header was send, the script will not stop. PHP just sends a header... but things are still working after your redirect. Example:
    PHP Code:
    <?php
    header
    ('Location: http://www.google.com');
    file_put_contents('file.txt''we have it');
    so, just to be sure:
    PHP Code:
    <?php
    header
    ('Location: http://www.google.com');
    exit; 
    // we stop here!
    file_put_contents('file.txt''we have it');

  13. #13
    SitePoint Evangelist Fergal's Avatar
    Join Date
    Nov 2003
    Location
    Ireland
    Posts
    492
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the explanation vectorialpx.
    Fergal Crawley (Previous Username: Proudirish.com)
    Business Advice Forum - Webmaster and Business Forum < Get a free link & win $5,000
    Forum Coin New World Currency


Bookmarks

Posting Permissions

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