Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to save data from a PHP form  (Read 47427 times)

0 Members and 1 Guest are viewing this topic.

anupam498

    Topic Starter


    Rookie

    How to save data from a PHP form
    « on: November 18, 2008, 02:48:17 PM »
    Hi 
         Everybody,

                             
    I want to use a php form to save the data inputted to it ,within the hosting server as simple text file. As most of the hosting services don't provide mailing PHP and even if so then the mail address must of the of the same domain for which the hosting server is being used. So i wondering is there any way to save the information  instead of sending via email. Kindly Please help me.
                       

                                                       
    The Form Code  

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Your Informations</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <form action="mailer.php" method="post">
    Your Name: <input type="text" name="test">

    Your Email: <input type="text" name="email">

    Your Message:
     <textarea name="message" rows="5" cols="30"></textarea>

    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>


                                                             
    The PHP code for the form

    <?PHP
    $to = "[email protected]";
    $subject = "Results from your Request Info form";
    $headers = "From: Form Mailer";
    $forward = 1;
    $location = "http://XXXXXXXX.com";

    $date = date ("l, F jS, Y");
    $time = date ("h:i A");



    $msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";

    if ($_SERVER['REQUEST_METHOD'] == "POST") {
       foreach ($_POST as $key => $value) {
          $msg .= ucfirst ($key) ." : ". $value . "\n";
       }
    }
    else {
       foreach ($_GET as $key => $value) {
          $msg .= ucfirst ($key) ." : ". $value . "\n";
       }
    }

    mail($to, $subject, $msg, $headers);
    if ($forward == 1) {
        header ("Location:$location");
    }
    else {
        echo "Thank you for submitting our form. We will get back to you as soon as possible.";
    }

    ?>



        Above form and PHP script work fine for servers providing an email PHP script. But most of the free hosting services are not providing it anymore. So it will also work for me if i could make an arrangement to save the inputs of the form..

                        What Minimal Modification in the script can serve the Purpose                                                   

                 
    Thanks.....

                                           
    « Last Edit: November 18, 2008, 03:33:55 PM by anupam498 »

    fffreak



      Adviser

    • That's right I am a final fantasy freak.
    • Thanked: 3
      • Yes
      • JSPCRepair
    • Certifications: List
    • Experience: Guru
    • OS: Windows 7
    Re: How to save data from a PHP form
    « Reply #1 on: December 29, 2008, 03:19:08 PM »
    Try something like this...

    HTML Form Code:

    Code: [Select]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Your Informations</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <form action="mailer.php?savedata=1" method="post">
    Your Name: <input type="text" name="name"><br>
    Your Email: <input type="text" name="email"><br>
    Your Message:<br> <textarea name="message" rows="5" cols="30"></textarea><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>

    PHP Code:
    Code: [Select]
    <?php
    $savedata 
    $_REQUEST['savedata'];
    if (
    $savedata == 1){ 
    $data $_POST['name'];
    $data $_POST['email'];
    $data $_POST['message'];
    $file "YOURDATAFILE.txt"

    $fp fopen($file"a") or die("Couldn't open $file for writing!");
    fwrite($fp$data) or die("Couldn't write values to file!"); 

    fclose($fp); 
    echo 
    "Your Form has been Submitted!";

    }
    ?>

    Computers are the future, not us. Learn everything you can about them while you still can, soon they will be learning about us... Every bit of advice that I give you is best guess, it is your choice whether or not you listen to it.

    kpac

    • Web moderator
    • Moderator


    • Hacker

    • kpac®
    • Thanked: 184
      • Yes
      • Yes
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 7
    Re: How to save data from a PHP form
    « Reply #2 on: December 30, 2008, 06:15:07 AM »
    PHP Code:
    Code: [Select]
    <?php
    $savedata 
    $_REQUEST['savedata'];
    if (
    $savedata == 1){ 
    $data $_POST['name'];
    $data .= $_POST['email'];
    $data .= $_POST['message'];
    $file "YOURDATAFILE.txt"

    $fp fopen($file"a") or die("Couldn't open $file for writing!");
    fwrite($fp$data) or die("Couldn't write values to file!"); 

    fclose($fp); 
    echo 
    "Your Form has been Submitted!";

    }
    ?>


    You forgot one small thing there - a full stop for the $data variable. If you don't have the full stop you will get a PHP error.

    fffreak



      Adviser

    • That's right I am a final fantasy freak.
    • Thanked: 3
      • Yes
      • JSPCRepair
    • Certifications: List
    • Experience: Guru
    • OS: Windows 7
    Re: How to save data from a PHP form
    « Reply #3 on: December 30, 2008, 02:12:58 PM »
    Thanks for catching that, I was quite tired when typing that up, plus I was at work :-\.
    Computers are the future, not us. Learn everything you can about them while you still can, soon they will be learning about us... Every bit of advice that I give you is best guess, it is your choice whether or not you listen to it.

    kpac

    • Web moderator
    • Moderator


    • Hacker

    • kpac®
    • Thanked: 184
      • Yes
      • Yes
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 7
    Re: How to save data from a PHP form
    « Reply #4 on: December 30, 2008, 03:28:04 PM »
    Thanks for catching that, I was quite tired when typing that up, plus I was at work :-\.

    Alright. :)

    We all make small mistakes like that. ;)

    DarthSeverus



      Newbie

      • Experience: Experienced
      • OS: Linux variant
      Re: How to save data from a PHP form
      « Reply #5 on: October 13, 2012, 12:35:51 AM »
      I know, it's a old thread, but I have a question here. This form glues the result to one string like,

      Name1Email1The Message1Name2Email2The Message2Name3Email3The Message3Name4Email4The Message4

      I normaly don't use PHP, now I need this code, but without that error. I read some PHP stuff but couldn't find a solution.

      Thanks for your help.

      DarthSeverus



        Newbie

        • Experience: Experienced
        • OS: Linux variant
        Re: How to save data from a PHP form
        « Reply #6 on: October 16, 2012, 08:45:06 PM »
        I got a very fast answer on Stackoverflow.
        http://stackoverflow.com/questions/12908334/new-line-in-php-output-in-a-text-file

        Quote
        $data = $_POST['name'] . PHP_EOL;
        $data .= $_POST['email'] . PHP_EOL;
        $data .= $_POST['message'] . PHP_EOL;

        kpac

        • Web moderator
        • Moderator


        • Hacker

        • kpac®
        • Thanked: 184
          • Yes
          • Yes
          • Yes
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 7
        Re: How to save data from a PHP form
        « Reply #7 on: October 20, 2012, 04:18:29 AM »
        I got a very fast answer on Stackoverflow.
        http://stackoverflow.com/questions/12908334/new-line-in-php-output-in-a-text-file

        Either that or use this:
        Code: [Select]
        $data = $_POST['name'] . "\n";
        $data .= $_POST['email'] . "\n";
        $data .= $_POST['message'];
        ...where \n means "new line".