-2

My task is to put data from a form with 24 input fields into a txt file. Not very adapt at PHP coding I got a few snippets of code from the web, the file_put_contentsnippet from an example with 2 input fields. It worked (=it wrote data to file and showed the 'thank_you'-page). I added another field - it worked. Ok, so now I added all my fields - it did not write to file and produced a blank white page.

I went back to the 3 fields version, and started adding fields 1 by 1. When it stopped working at 8 fields, I introduced a 2. isset(POST[]) element. That way I could add another 6 fields, and it worked. (In between I separately saved the code when it successfully printed data from 11 fields to my file). When on the 7th field it again produced a blank page and did not save to file, I removed tha last addition - and to my BIG SURPRISE the very code that had worked before I added the last one didn't work any more?! I continued to remove element by element - it didn't work anymore?! So I reverted to the saved version of the code - and it still does not work.

So I went over every single letter, sign and number - all to no avail. Even though I havent changed anything in the html form, I ched this too, because everything points to a problem in the form that points to my php file. All in vain. I am aware that it has to be something I did and now overlook - please point out to me where my mistake lies.

This is the version that worked 2 days ago, and I saved it before expanding:

<?php
function resetForm($form) {
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox')
     .removeAttr('checked').removeAttr('selected');
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

if(isset($_POST['q01' | 'q02' | 'q03' | 'q04' | 'q05' | 'q06' | 'q07']) &&  isset($_POST['q08' | 'q09' | 'q99' | 'q98'])) {
    $data = $_POST['q01'] . '#' . $_POST['q02'] . '#' . $_POST['q03'] . '#' . $_POST['q04'] . '#' . $_POST['q05'] . '#' . $_POST['q06'] . '#' . $_POST['q07'] . '#' . $_POST['q08'] . '#' . $_POST['q09'] . '#' . $_POST['q99'] . '#' . $_POST['q98'] . "\n";
    $ret = file_put_contents('data/data.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        header('Location:danke.html');
    }
 exit (0);
} 
?>

Just to make sure I will also post the relevant lines of the form:

<form id="umfrage" class="appnitro" method="post" action="data2text.php"  accept-charset="UTF-8">

 <input id="saveForm" class="button_text" type="submit" name="submit" value="Fertig!" />
  • what are you trying to do here `isset($_POST['q01' | 'q02' | 'q03' | 'q04' | 'q05' | 'q06' | 'q07']) && isset($_POST['q08' | 'q09' | 'q99' | 'q98']))`? – Funk Forty Niner Sep 18 '15 at 14:33
  • not to mention a missing closing form tag – Funk Forty Niner Sep 18 '15 at 14:34
  • 1
    _“it did not write to file and produced a blank white page”_ – that means that first of all you need to configure your error_reporting properly, see http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – CBroe Sep 18 '15 at 14:38
  • 1
    not enough code for a solution yet alone diagnose this, on top of flawed conditionals. Voted to close as unclear. – Funk Forty Niner Sep 18 '15 at 14:41

1 Answers1

0

your resetForm function does not seem to be PHP.

isset($_POST['q01' | 'q02' | 'q03' | 'q04' | 'q05' | 'q06' | 'q07'])

what are you expecting this to do? this does binary operations on string... This is not how you check array key availability. you should check each key individually:

if (isset($_POST['q01']) || isset($_POST['q02']) .....) {

note the double pipes!

NDM
  • 6,731
  • 3
  • 39
  • 52
  • Uggh.... I'd use an `in_array()` for all of what OP's code/arrays lol rather than all of those conditionals. That's just me, of course ;-) – Funk Forty Niner Sep 18 '15 at 14:35
  • `in_array()` to check if a key exists? anyway, he's not even grasping the concept of logical operators and array access, let's do one step at a time here... – NDM Sep 18 '15 at 14:37
  • oh sure, I do it all the time. but yeah... the logic is flawed and the question is unclear. edit: oh and assigning values to an array. – Funk Forty Niner Sep 18 '15 at 14:38
  • `in_array` checks if a value exists, not if a key is set – NDM Sep 18 '15 at 14:41
  • Thank you, @NDM, checking key availability individually did the trick. I still do not understand why it did work in this weird code in the first place, but at least my problem is solved. I just took the code snippets and tried to modify them just like one might start use an electronic device without reading any manual, just by guessing the functionalities of the different buttons. Sort of. – Georg Parlow Sep 18 '15 at 17:05