1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Saving Input Data using PHP

Discussion in 'PHP' started by keeganray, Feb 4, 2010.

  1. #1
    Hi,

    I am writing my first website and I am trying am very unfamiliar with PHP. I have text inputs on my website that ask the user to input their first name, last name, email, etc., but I don't know how to write a PHP script that will capture and save that data once the submit button is pressed. I want the script to save the data to a text file and save successive entries in the same text file. Can someone help me write a script that will do this or point me to a site that will teach me relatively quickly how to do this.

    Thanks,

    Keegan
     
    keeganray, Feb 4, 2010 IP
  2. Haney

    Haney Peon

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you looking for the data to be stored in a database? if so do you have a mysql database? or do you want it stored in a text file?
     
    Haney, Feb 4, 2010 IP
  3. keeganray

    keeganray Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I don't have a mysql database. So I guess a text file would be easier.

    Thanks.
     
    keeganray, Feb 4, 2010 IP
  4. Haney

    Haney Peon

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    send the HTML for the form. so I can know what to label everything...
     
    Haney, Feb 4, 2010 IP
  5. keeganray

    keeganray Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The web address is navier.aa.washington.edu/downloads
     
    keeganray, Feb 4, 2010 IP
  6. keeganray

    keeganray Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    with a .html at the end of that
     
    keeganray, Feb 4, 2010 IP
  7. Haney

    Haney Peon

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So, looking at your HTML, your input names have spaces. I am not sure if that poses a problem. I would suggest that you change that. for this example, I assumed you will change it...

    Here is the HTML that should be changed.
                    <p>
                      <label for="First Name">First Name:</label>
                       <input name="firstname" type="text" id="first_name">
                    </p>
                    <p>
                      <label for="Last_Name">Last Name: </label>
                      <input name="lastname" type="text" id="last_name">
                    </p>
    
                
    HTML:
    Here is your php code. Name this log.php or whatever you want... You will have to point the form's action="" to this script.
    
    <?
    // Gets info from form.
    $firstname=$_POST['firstname'];  // your HTML has them as "First Name       "... change the HTML to firstname
    $lastname=$_POST['lastname'];    // change this in your HTML too...
    $email=$_POST['email'];
    $uni_org=$_POST['university_organization'];
    $usercountry=$_POST['country'];
    
    // checks that it is all there...
    
    if (!$firstname or !$lastname or !$email or !$uni_org or !$usercounrty){
    echo "There was an error. Information is missing";
    } else {
    // Writes to flat file.
    $file1 = "filename.txt";  // <----- change filename
    $open3 = fopen($file1, "a");
    fwrite($open3, "Name: ".$firstname." ".$lastname."\n"."Email: ".$email."\n"."University Organization: ".$university_organization."\n"."Country: ".$usercountry."\n\n\n");
    fclose($open3);
    echo "Saved to file.";
    }
    ?>
    
    PHP:
    let me know if there is any problems.
     
    Haney, Feb 4, 2010 IP
  8. keeganray

    keeganray Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks,

    I'll try it tomorrow when I have access to the dreamweaver software.

    -Keegan
     
    keeganray, Feb 4, 2010 IP
  9. thtarcman

    thtarcman Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Possibly consider using an SQLite database, i'm not sure if your hosting service supports it, but it's highly possible.
     
    thtarcman, Feb 4, 2010 IP
  10. keeganray

    keeganray Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I am using Redhat Linux for my server. Is a SQL database free?
     
    keeganray, Feb 4, 2010 IP
  11. clinton

    clinton Well-Known Member

    Messages:
    2,166
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    110
    #11
    MySQL and Postgres is free software so yes. However Oracle and Microsoft's SQL is not, I don't think.
     
    clinton, Feb 4, 2010 IP
  12. Haney

    Haney Peon

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hey, I just realized I made a typo error...

    original line...
    fwrite($open3, "Name: ".$firstname." ".$lastname."\n"."Email: ".$email."\n"."University Organization: ".$university_organization."\n"."Country: ".$usercountry."\n\n\n");
    PHP:
    change line to...
    fwrite($open3, "Name: ".$firstname." ".$lastname."\n"."Email: ".$email."\n"."University Organization: ".$uni_org."\n"."Country: ".$usercountry."\n\n\n");
    PHP:
    Sorry about that. If you get a MySQL DB, let me know.
     
    Haney, Feb 5, 2010 IP
  13. BrettOwnz

    BrettOwnz Member

    Messages:
    286
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #13
    I would highly recommend setting up a MySQL database if that is at all possible. Most hosting plans will come with support for MySQL, and usually they will have access to phpMyAdmin, the software used for creating MySQL databases. If you CAN create a MySQL database then you can use the following code for your pages.

    On the HTML page:

    
    <form action='process.php' method='post'>
    <p>
    <label for='firstname'>First Name: </label>
    <input type='text' name='firstname'></input>
    </p>
    <p>
    <label for='lastname'>Last Name: </label>
    <input type='text' name='lastname'></input>
    </p>
    </form>
    
    HTML:
    In Process.php:

    
    <?php
    
    //Edit the following according to your database settings
    
    $db_name = "DATABASENAME";
    $db_user = "DATABASEUSERNAME";
    $db_pass = "DATABASEPASS";
    $db_host = "DATABASEHOST";
    
    //Do not edit anything below
    
    $con = mysql_connect($db_host,$db_user,$db_pass);
    
    $firstname = mysql_real_escape_string($_POST['firstname']);
    $lastname = mysql_real_escape_string($_POST['lastname']);
    
    $sql = "INSERT INTO `users` (firstname,lastname) VALUES ('$firstname','$lastname');
    
    $res = mysql_query($sql) or die("Could not perform query: ". mysql_error() ."");
    
    mysql_close($con);
    
    ?>
    
    PHP:
    Now, in this case your database would be set up as follows:

    Thats it. If you need any more explanation let me know - taking the sql route and learning SQL is very important in core PHP knowledge. I would definitely recommend using it!

    Good luck with the site!
    -Brett
     
    BrettOwnz, Feb 5, 2010 IP
  14. keeganray

    keeganray Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hi Haney,

    Sorry it took so long to reply. I had to get PHP installed on my server first. I have run your code and I'm getting this error:

    Parse error: syntax error, unexpected T_VARIABLE in /var/www/html/logscript.php on line 13

    My logscript.php looks like this:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Downloads</title>
    </head>
    
    <body>
    
    <?PHP
    // Gets info from form.
    $firstname=$_POST['firstname'];  
    $lastname=$_POST['lastname'];   
    $email=$_POST['email'];
    $uni_org=$_POST['university_organization'];
    $usercountry=$_POST['country'];
    
    // checks that it is all there...
    
    if (!$firstname or !$lastname or !$email or !$uni_org or !$usercounrty){
    echo "There was an error. Information is missing";
    } else {
    // Writes to flat file.
    $file1 = "log.txt";  // <----- change filename
    $open3 = fopen($file1, "a");
    
    fwrite($open3, "Name: ".$firstname." ".$lastname."\n"."Email: ".$email."\n"."University Organization: ".$uni_org."\n"."Country: ".$usercountry."\n\n\n");
    fclose($open3);
    echo "Saved to file.";
    }
    ?>
    
    </body>
    </html>
    
    PHP:
     
    keeganray, Feb 11, 2010 IP
  15. avimusicstore

    avimusicstore Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Downloads</title>
    </head>

    <body>

    <?PHP

    //http://localhost/shop_dev/scrape.php?firstname=hi&lastname=by&email=0ne@two&university_organization=bbb&country=IN

    // Gets info from form.
    $firstname=$_REQUEST['firstname'];
    $lastname=$_REQUEST['lastname'];
    $email=$_REQUEST['email'];
    $uni_org=$_REQUEST['university_organization'];
    $usercountry=$_REQUEST['country'];

    // checks that it is all there...
    echo "$firstname . $lastname . $email . $uni_org . $usercountry";
    if (!$firstname or !$lastname or !$email or !$uni_org or !$usercountry){
    echo "There was an error. Information is missing";
    } else {
    // Writes to flat file.
    $file1 = "log.txt"; // <----- change filename
    $open3 = fopen($file1, "a");

    fwrite($open3, "Name: ".$firstname." ".$lastname."\n"."Email: ".$email."\n"."University Organization: ".$uni_org."\n"."Country: ".$usercountry."\n\n\n");
    fclose($open3);
    echo "Saved to file.";
    }
    ?>

    </body>
    </html>

    Test This running fine now as u have done a mistake in "usercountry" spelling just.
     
    avimusicstore, Feb 12, 2010 IP
  16. keeganray

    keeganray Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Thanks avimusicstore,

    That code worked. I need to get the code to save the system time into along with the user's information into the text file. Also I want to send a link for the actual download page to the user's email that they have inputted.

    Thanks in advance for your help,

    -Keegan
     
    keeganray, Feb 16, 2010 IP
  17. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #17
    http://php.net

    Search for "date" and "mail". PHP's site has some of the best documentation you can ask for. If you get it working and notice the links won't come out as links make sure the content type is html. This is under example #4 in the mail function documentation.
     
    AntelopeSalad, Feb 16, 2010 IP
  18. keeganray

    keeganray Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Thanks AntelopeSalad.
     
    keeganray, Feb 16, 2010 IP
  19. Marshton

    Marshton Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #19
    I suggest you go to www.w3schools.com and learn more about MySQL Databases when used with PHP. It is very rarely seen nowadays that data is saved into a text file [apart from server logs, etc], as the database means that you can search through the data faster than you can with a text file.

    Just a thought.
     
    Marshton, Feb 17, 2010 IP