#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Troubles using fwrite


    Hi,


    I have some schoolwork that needs to be done, and im pretty new at PHP. My teacher wanted me to create an HTML conact form, after this you will be sent to page nr two. Where i am listing out the information in the form, a confirmation. This i done. But now he wants me to create an .txt file where the information will be saved.


    Code:
    <html><head><title>Form</title></head><body>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            
    <form action="form.php" method="POST">
            <p>Name</p> <input type="text" name="name">
            <p>Email</p> <input type="text" name="email">
            <p>Phone</p> <input type="text" name="phone">
            <p>Adress</p><input type="text" name="adress">
            
    
    <p>Hva lurer du på?<br/>
        <select size="1" name="dropdown">
    <option>Products</option>
    <option>Shipping</option>
    <option>Other</option>
        </select>
        
    <br />
    <br />
    
        
            <td>Do you want us to call you?<br />
                Yes <input type="radio" name="answer" value="Yes" />
    
                No<input type="radio" name="answer" value="No" />
            </td>
            
            
            
            <br />
            <br />
    <p>Comment</p><textarea wrap="OFF" name="comment" rows="6" cols="25"></textarea><br />      
    <br />
    <input type="submit" value="Send" />
    </form>
    </body>
    </html>
    Now i want the customers information to be saved in a .txt file.

    Code:
    <?php 
    session_start();
    
    $fp =fopen("temp/text.txt","w+");
    fwrite($fp,$_SESSION["name"]."\n");
    fwrite($fp,$_SESSION["phone"]."\n");
    fwrite($fp,$_SESSION["adress"]."\n");
    fwrite($fp,$_SESSION["email"]."\n");
    fwrite($fp,$_SESSION["answer"]."\n");
    fwrite($fp,$_SESSION["dropdown"]."\n");
    fwrite($fp,$_SESSION["comment"]."\n");
    fwrite($fp,"\n");
    fclose($fp);
    ?>
    Name, phone, adress and email is the only ones who saves in the .txt file. The others, i dont know (answer, dropdown, comment). Something wrong with my code? Tell me if you need some more info about this,

    Thanks
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,745
    Rep Power
    9397
    $_SESSION is for session data. You know, with session_start() and all that.

    Use $_POST: that's data from a submitted form (with method=post).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by requinix
    $_SESSION is for session data. You know, with session_start() and all that.

    Use $_POST: that's data from a submitted form (with method=post).
    Something like this?


    PHP Code:
    fwrite($fp,$_POST["name"]."\n");
    fwrite($fp$_POST["email"]."\n");
    fwrite ($fp$_POST["phone"]."\n");
    fwrite ($fp$_POST["adress"]."\n");
    fwrite ($fp$_POST["comment"]."\n");
    fwrite ($fp$_POST["dropdown"]."\n");
    fwrite ($fp$_POST["answer"]."\n"); 
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    With the post method no informasjon is getting saved in my .txt file, its empty
  8. #5
  9. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,745
    Rep Power
    9397
    Are you putting this code in form.php or someplace else? If the latter then what's in form.php?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by requinix
    Are you putting this code in form.php or someplace else? If the latter then what's in form.php?

    PHP Code:
    <?php
    session_start
    ();
    ini_set('session.bug_compat_warn'0);
    ini_set('session.bug_compat_42'0);


    if (isset(
    $_REQUEST["name"]))
    {
    echo 
    "Name: ".$_REQUEST["name"]."<br/> ";

    }
    if (isset(
    $_REQUEST["Phone"]))
    {
    echo 
    "Phone: ".$_REQUEST["Phone"]."<br>";
    }
    if (isset(
    $_REQUEST["email"]))
    {
    echo 
    "Epost: ".$_REQUEST["email"]."<br>";
    }

    if (isset(
    $_REQUEST["dropdown"]))
    {
    echo 
    "You question is about: ".$_REQUEST["dropdown"]."<br>";
    }
    if (isset(
    $_REQUEST["comment"]))
    {
        echo 
    "Your comment: <br>";
        echo 
    "<b>(".$_REQUEST["comment"].")</b><br/>";
    }
    if (isset(
    $_REQUEST["adress"]))
    {
    echo 
    "Adress: ".$_REQUEST["adress"]."<br>";
    }

    $_SESSION["name"]=$_REQUEST["name"];
    $_SESSION["email"]=$_REQUEST["email"];
    $_SESSION["phone"]=$_REQUEST["phone"];
    $_SESSION["adress"]=$_REQUEST["adress"];
    $_SESSION["comment"];$_REQUEST["comment"]; 
    $_SESSION["dropdown"];$_REQUEST["dropdown"];

    echo 
    "<br/>";

    $answer $_POST['answer'];

        if (
    $answer == "Yes") {
            
            echo 
    '<strong>We will call you!</strong>';
            
        }
        
        else {
            
            echo 
    '<strong>We wont call you :(</strong>';
            
        }   
        echo 
    "<br/>";

    ?>
    <a href="form2.php">Continue to the third site</a>

    This is form.php. I'm norwegian so i have to translate my coding into english, sorry for any mispelled words.
  12. #7
  13. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,745
    Rep Power
    9397
    Then you actually should be using $_SESSION in the new code.

    The problem is in form.php.
    PHP Code:
    $_SESSION["comment"];$_REQUEST["comment"];  
    $_SESSION["dropdown"];$_REQUEST["dropdown"]; 
    See those extra semicolons?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by requinix
    Then you actually should be using $_SESSION in the new code.

    The problem is in form.php.
    PHP Code:
    $_SESSION["comment"];$_REQUEST["comment"];  
    $_SESSION["dropdown"];$_REQUEST["dropdown"]; 
    See those extra semicolons?
    Oh my god.... am i blind? Thank you very much!

IMN logo majestic logo threadwatch logo seochat tools logo