help to add data to text file

Discussion in 'PHP' started by JEET, Feb 5, 2006.

  1. #1
    Hello,
    I have a PHP form and I want to send the data submitted into a text file .
    Like
    <input type="text" name="name">
    <input type="text" name="email">
    <input type="text" name="url">
    <input type="button" value="submit">
    Then the data submitted is added to an existing text file on server something like :
    jeet ~ ~ http://www.domain.com
    sj ~ ~ http://www.2domain.com

    And the submitter gets a thankyou message .
    What will be the code to do this ?
    I am totally new to PHP . Please help .
    Thank you for your time.
    Regards
    Jeet
     
    JEET, Feb 5, 2006 IP
  2. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    from http://uk2.php.net/manual/en/function.fwrite.php

    <?php
    $filename = 'test.txt';
    $somecontent = "Add this to the file\n";
    
    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {
    
       // In our example we're opening $filename in append mode.
       // The file pointer is at the bottom of the file hence
       // that's where $somecontent will go when we fwrite() it.
       if (!$handle = fopen($filename, 'a')) {
             echo "Cannot open file ($filename)";
             exit;
       }
    
       // Write $somecontent to our opened file.
       if (fwrite($handle, $somecontent) === FALSE) {
           echo "Cannot write to file ($filename)";
           exit;
       }
      
       echo "Success, wrote ($somecontent) to file ($filename)";
      
       fclose($handle);
    
    } else {
       echo "The file $filename is not writable";
    }
    ?> 
    PHP:
     
    wwm, Feb 5, 2006 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Terrific !
    Thanks for that code .
    Although now I am in a new problem .
    When submit is clicked , I can call this php page but how do I set the "add string" to the values in the form ?
    Like in the form :
    <form name="1" action="add-text.php" method="post">
    <input type="text" name="firstname">
    <input type="text" name="email">
    <input type="text" name="url">
    <textarea rows="4" cols="40" name="des"> </textarea>
    <input type="button" value="submit">
    </form>

    I want to make one single string of all the values and seperate each value by "~" sign.
    Like
    jeet ~ ~ http://www.domain.com ~ whole message

    How will I do it ?

    Thank you very much for the time you took to help me .
    I honestly appreciate the help .
    Regards
    Jeet
     
    JEET, Feb 6, 2006 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Any help please ?
    Thank you
    Regards
    Jeet
     
    JEET, Feb 7, 2006 IP
  5. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    here u go coded a quick script that will do what u want

    theres still some work to do but the code is quite clear and commented so u can take it from here

    :)

    <?php
    
    
    //do this if the form was submitted
    if ($POST['submited'] == 1) {
    	
    	//filename of our file that will be written
    	$filename = 'test.txt';
    	
    	//construct our line
    	$line = $POST['name'].' ~ '.$POST['email'].' ~ '.$POST['url'].' ~ '.$POST['description']."\n\n";
    	
    	// Let's make sure the file exists and is writable first.
    	if (is_writable($filename)) {
    
    		//check if the file can be opened
    	   	if (!$handle = fopen($filename, 'a')) {
    	    	echo "Cannot open file ($filename)";
    	        exit;
    	   }
    
    	   if (fwrite($handle, $line) === FALSE) {
    	       	echo "Cannot write to file ($filename)";
    			exit;
    	   }
    
    	   echo "Success, wrote ($somecontent) to file ($filename)";
    
    	   //close file, IMPORTANT!!
    	   fclose($handle);
    
    	} 
    	else {
    	   echo "The file $filename is not writable";
    	}
    	
    }
    else {
    	
    	print '
    <html>
    <head><title>form</title>
    </head>
    
    <body>
    	<form name="1" action="test.php" method="post">
    	<input type="hidden" name="submited" value="1" />
    	<input type="text" name="name" />
    	<input type="text" name="email" />
    	<input type="text" name="url" />
    <textarea rows="4" cols="40" name="description"> 
    </textarea>
    	<input type="button" value="submit" />
    	</form>
    </body>';
    	
    }
    
    ?>
    PHP:
     
    wwm, Feb 7, 2006 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    WOW !
    I can't believe you coded a script for me .
    Thank you a lot !!!

    I am not sure if I would be able to make it work if anything goes wrong .People call themselves "PHP guru's" , you can call me "PHP idiot"...
    I will try this and let you know how it goes .
    Once again , "Thank You a hundred times!"
    Bye
    Regards
    Jeet
     
    JEET, Feb 8, 2006 IP
  7. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #7
    Hi ,
    I think there is something wrong here and I am not able to figure out what .
    This is what is happenning .
    When I click the "submit" button , nothing happens .
    Please tell me which line in the code above needs to be modified and what should be written instead .

    Thank you
    Regards
    Jeet
     
    JEET, Feb 11, 2006 IP
  8. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i didnt actual test it i coded it quite quckly

    the problem is on line 5


    if ($POST['submited'] == 1) {



    change to


    if ($POST['submited'] == '1') {
     
    wwm, Feb 11, 2006 IP
  9. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #9
    Hi,
    I think the php script is not getting executed .
    This is what I have here .
    Is the Form action field set OK ?
    The file is named add1.php
    
    <?php
    //do this if the form was submitted
    if ($POST['submited'] == "1") {
    //filename of our file that will be written
    $filename = '1test.txt';
    //construct our line
    $line = $POST['name'].' ~ '.$POST['email'].' ~ '.$POST['url'].' ~ '.$POST['description']."\n\n";
    // Let's make sure the file exists and is writable first.
    if (
    is_writable
    ($filename)) {
    //check if the file can be opened
    if (!$handle =
    fopen
    ($filename, 'a')) {
    echo
    "
    Cannot open file ($filename)";
    exit
    ;
    }
    if (
    fwrite
    ($handle, $line) === FALSE) {
    echo
    "
    Cannot write to file ($filename)";
    exit
    ;
    }
    echo
    "
    Success, wrote ($somecontent) to file ($filename)";
    //close file, IMPORTANT!!
    fclose
    ($handle);
    }
    else {
    echo
    "
    The file $filename is not writable";
    }
    }
    else {
    print
    '
    <html>
    <head><title>form</title>
    </head>
    <body>
    <form name="1" action="add1.php" method="post">
    <input type="hidden" name="submited" value="1" />
    <br> name <input type="text" name="name" />
    <br> email <input type="text" name="email" />
    <br> url <input type="text" name="url" />
    <br> message <textarea rows="4" cols="40" name="description">
    </textarea>
    <br><input type="button" value="submit" />
    </form>
    </body>';
    }
    ?>
    
    Code (markup):
    Thanks
    jeet
     
    JEET, Feb 11, 2006 IP
  10. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #10
    ok this works perfectly (i tested there was small mistake in my previous code)

    u need to paste the code below into test.php and make a text file test.txt in the same folder

    of course ull have to work on the script some more to tailor it to ur site, but the functionality is there ;)

    
    
    <?php
    
    
    
    //do this if the form was submitted
    if ($_POST['submited'] == '1' ){
    	
    		
    	//filename of our file that will be written
    	$filename = 'test.txt';
    	
    	//construct our line
    	$line = $_POST['name'].' ~ '.$_POST['email'].' ~ '.$_POST['url'].' ~ '.$_POST['description']."\n\n";
    	
    	// Let's make sure the file exists and is writable first.
    	if (is_writable($filename)) {
    		
    		//check if the file can be opened
    		if (!$handle =fopen($filename, 'a')) {
    			echo "Cannot open file ($filename)";
    			exit;
    		}
    	
    		if (fwrite($handle, $line) === FALSE) {
    			echo "Cannot write to file ($filename)";
    			exit;
    		}
    	
    		echo "Success, wrote ($somecontent) to file ($filename)";
    		
    		//close file, IMPORTANT!!
    		fclose($handle);
    	}
    	else {
    		echo "The file $filename is not writable";
    	}
    }
    else {
    	
    	print '
    <html>
    <head><title>myform</title>
    </head>
    <body>
    
    <form 	id = "sform" 
    		name = "sform" 
    		action = "test.php" 
    		method = "post">
    		
    <input type="hidden" name="submited" value="1" />
    <br> name <input type="text" name="name" />
    <br> email <input type="text" name="email" />
    <br> url <input type="text" name="url" />
    <br> message <textarea rows="4" cols="40" name="description">
    </textarea>
    <br>
    		<script type="text/javascript" language="JavaScript">
    		<!--
    			var submitted = false;
    							
    			function SubmitTheForm() {
    			if(submitted == true) { return; }
    				document.sform.submit();
    				document.sform.b_submit.value = \'Wait...\';
    				document.sform.b_submit.disabled = true;
    				submitted = true;
    			}
    							
    		//-->
    		</script>
    		<input type="button" name="b_submit" value="Submit" onclick="return SubmitTheForm();" />
    							
    		<noscript>
    			<inputtype="submit" name="b_submit" value="Submit" />
    		</noscript>
    
    </form>
    </body>';
    }
    ?>
    
    
    PHP:
     
    wwm, Feb 11, 2006 IP
  11. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #11
    Fantastic !!!
    Very very thanks .
    Thanks for all the time and trouble you took for me .
    Thanks once again .

    Best Wishes
    Jeet
     
    JEET, Feb 11, 2006 IP