1

I am new to PHP and have a question about how to append information to a .txt file using a form with a button with type="button", the appending should be ; separated in the .txt file. Can someone help me on how to accomplish the appending from the form to the .txt file?

(Each time you fill out the form and append the information to the .txt file a list-element should be created beside the form and containing only the information of the two first input-elements, taken from the .txt file). The form looks like this:

<?php
    include("header.php");
?>

        <form id="info" method="post" action="">
                <p id="formrubrik">Add information here:</p><br/>
                <p>Title:</p>
                <textarea class="field" id="title" name="names" rows="1" cols="20"></textarea><br/><br/>
                <p>Grade</p>
                <select class="field" name="grade" id="star">
                    <option value="0">Choose grade...</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                </select><br/><br/>

                <p>Link:</p>
                <textarea class="field" id="link" name="links" rows="1" cols="20"></textarea><br/><br/>

                <p>Photo link:</p>
                <textarea class="field" id="picture" name="photo" rows="1" cols="20"></textarea><br/><br/>

                <p>Description:</p>
                <textarea class="field" id="handling" name="description" rows="3" cols="20"></textarea><br/><br/>

                <button value="0" id="add" type="submit">Save</button><br/>

            </form>

            <div id="list_elements">

                <ul id="elements">
                </ul>

            </div>

<?php
    include("footer.php");
?> 
spovell
  • 133
  • 3
  • 13

2 Answers2

2

Too many things wrong with your form

A. No form action action="yourfile.php"

B. button type was set button instead of submit

C. No Validation in place

D. Not Checking if data is posted before attempting to write

Too many things are wrong why don't you start with learning the basic principles of PHP to append data to a file can simple be done with file_put_contents :

file_put_contents($myFile, $data . PHP_EOL, FILE_APPEND);
Baba
  • 94,024
  • 28
  • 166
  • 217
  • ok, just need some direction in what to do to make the form work with php, thanks for the feedback anyway – spovell Mar 19 '13 at 23:43
  • what should i change for C? what do you mean with validation? – spovell Mar 19 '13 at 23:45
  • I have a validation in a javscript file, never done anything with php before so just playing around with it... trying to understand it better... – spovell Mar 19 '13 at 23:50
  • and what about D, how do I check if data is posted? – spovell Mar 19 '13 at 23:56
  • Try : https://www.google.com.ng/search?q=vlidae+from+in+php&aq=f&oq=vlidae+from+in+php&aqs=chrome.0.57.5616&sourceid=chrome&ie=UTF-8#hl=en&sclient=psy-ab&q=how+to+validate+form+in+php&oq=how+to+validate+form+in+php&gs_l=serp.3..0i13l2j0i7i30l2.5145.6304.1.6483.7.7.0.0.0.4.255.1358.2-6.6.0...0.0...1c.1.7.psy-ab.tft--RI0y98&pbx=1&bav=on.2,or.r_cp.r_qf.&bvm=bv.44011176,d.Yms&fp=d8c52a8f43053c0c&biw=944&bih=1071 – Baba Mar 19 '13 at 23:58
1

Here is a little form that submits data posted by you, into a text file, by separating them with ;, however unlike your demand, I used type='submit' instead of ..='button' as, I have been under the impression that, buttons do not submit data into form, entirely with PHP.

<?php 

if(isset($_POST['button'])) {


    $myFile = "example.txt";
    $fh = fopen($myFile, 'a') or die("can't open file");
    $stringData = $_POST['button']. ' ; ';
    fwrite($fh, $stringData);

    fclose($fh); 

}

If you want to check whether a user has submitted a data or not, you can use this example.

<?php 

if(isset($_POST['button'])) {

    $data = $_POST['info'];

    if(!empty($date) && isset($date))
   { $myFile = "example.txt";
     $fh = fopen($myFile, 'a') or die("can't open file");

     fwrite($fh, $stringData);
     fclose($fh); 

   } else {
  echo 'the data you have entered is empty, please enter again.';
    }
Community
  • 1
  • 1
samayo
  • 16,163
  • 12
  • 91
  • 106
  • thanks, will take a look, you are probably right, the button needs to be type="submit" – spovell Mar 19 '13 at 23:55
  • I suppose you know, how to check if a user submitted empty values, that is why I did not include it. But, yes, the submit type is always needed. And, finally if you find this answer helpful, you should mark it as accepted / upvote – samayo Mar 19 '13 at 23:58
  • i check if the form isnt filled in correctly in javscript so it isnt possible to send at all if everything isnt filled in... cant mark it as upvote because i joined stackoverflow a couple of days ago and you need 15 in reputation before its possible but when I tested some more and read some more maybe it will be filled in as accepted :) – spovell Mar 20 '13 at 00:10
  • @spovell plus, you must know the difference between upvote and accept. Accept is given to the only best answer, upvote can be given to any answer you find helpful, or like – samayo Mar 20 '13 at 00:19
  • its upvoted :) dont have time to check it now, but will make it accepted later if i find the problem to be solved, thank you for the help! – spovell Mar 20 '13 at 00:28
  • @crypticツ Child? I have a pets older, and more mature than you will ever be, and for you to accuse me of rep-whoring is like teresko saying Jesus was rude. So, back off and don't worry about me. – samayo Mar 23 '13 at 02:12
  • @crypticツ Did I ever tell you why I hate/ignored you? – samayo Mar 23 '13 at 02:22
  • Good in that case likewise, I don't care about ANYTHING you have to say. – samayo Mar 23 '13 at 02:25