write multiple form data in a .txt file with php script

Discussion in 'PHP' started by kakkalo, Jan 28, 2009.

  1. #1
    hi guys, i am trying to make a form and make it as so that when its submitted all the data will be save on a .txt file. i was able to save one field succesfulle but not others.

    here is my php scripts

    <?php
    $saving = $_REQUEST['saving'];
    if ($saving == 1){
    $data = $_POST['email'];
    $file = "savepic.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 "Request Successfully Send.";
    }
    ?>
    Code (markup):

    and here is the form that i want to save

    
    <form id="form_130089" class="appnitro" name="form1" method="post" action="save.php?saving=1">
    			
    			
    						
    			<ul >
    			
    					<li id="li_1" >
    		<label class="description" for="fname">Name </label>
    		<span>
    			<input id="fname" name= "fname" class="element text" maxlength="255" size="8" value=""/>
    			<label>First</label>
    		</span>
    		<span>
    			<input id="lname" name= "lname" class="element text" maxlength="255" size="14" value=""/>
    			<label>Last</label>
    		</span><p class="guidelines" id="guide_1"><small>Your name (It will not be published or shown)
    </small></p> 
    		</li>		<li id="li_2" >
    		<label class="description" for="email">Email </label>
    		<div>
    			<input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/> 
    		</div><p class="guidelines" id="guide_2"><small><b>MUST</b> put an VALID email address. It will not be published just for contacting purposes. </small></p> 
    		</li>		<li id="li_3" >
    		<label class="description" for="nickname">Nickname </label>
    		<div>
    			<input id="nickname" name="nickname" class="element text medium" type="text" maxlength="255" value=""/> 
    		</div><p class="guidelines" id="guide_3"><small>Your nick name WILL BE published with your picture.</small></p> 
    		</li>		<li id="li_5" >
    		<label class="description" for="pictitle">Picture Title </label>
    		<div>
    			<input id="pictitle" name="pictitle" class="element text medium" type="text" maxlength="255" value=""/> 
    		</div><p class="guidelines" id="guide_5"><small>If you want to give your picture a title</small></p> 
    		</li>		<li id="li_4" >
    		<label class="description" for="imgurl">Image URL </label>
    		<div>
    			<textarea id="imgurl" name="imgurl" class="element textarea medium"></textarea> 
    		</div><p class="guidelines" id="guide_4"><small>Put your picture url here.
    </small></p> 
    		</li>
    			
    					<li class="buttons">
    			    <input type="hidden" name="form_id" value="130089" />
    			    
    				<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
    		</li>
    			</ul>
    		</form>	
    
    Code (markup):

    as you can see it only saves the email field. so can it save all the other field.

    i am fairly new to this so if you can help me i will be thank full. thank you
     
    kakkalo, Jan 28, 2009 IP
  2. stangparts

    stangparts Member

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #2
    No expert here but it looks to me as if you are only retrieving one form variable. Looking at this the information saved in the document should be the email.

    IMO you are going to need to create an array to collect the form data and then write the array to the text file.
     
    stangparts, Jan 28, 2009 IP
  3. kakkalo

    kakkalo Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    can you help with how to make a array??
     
    kakkalo, Jan 28, 2009 IP
  4. kakkalo

    kakkalo Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok i got how to save more then one variable

    
    <?php
    $saving = $_REQUEST['saving'];
    if ($saving == 1){
    $data = $_POST['fname'];
    $data .= $_POST['lname'];
    $data .= $_POST['email'];
    $data .= $_POST['nickname'];
    $file = "savepic.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 "Request Successfully Send.";
    }
    ?>
    
    Code (markup):

    but my new problem is how can it make each input go to a different line.
     
    kakkalo, Jan 28, 2009 IP
  5. red_mamba

    red_mamba Peon

    Messages:
    63
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    if you want to save the whole array use this:

    
    file_put_contents($fileName, serialize($_POST));
    
    to retrive data from array use
    
    $fileData = file_get_contents($fileName);
    $arrayData = unserialize($fileData);
    
    
    PHP:
    if you want new line add "\r\n" to end of the line, which is carriage return

    that's it :)
     
    red_mamba, Jan 28, 2009 IP
  6. zaeem.liaqat

    zaeem.liaqat Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    what if button is image this is
    <input type="hidden" name="form_id" value="130089" />
    <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />

    in

    <img src=""> like what i have to do
     
    zaeem.liaqat, Dec 23, 2011 IP