0

I got this php code from this question found at this URL PHP write file from input to txt, I was able to edit the code to make it work for what I am trying to do, however I am having some difficulty in getting this to work.

This is the php code that I have

<?php
$txt = "data.txt";
if (isset($_POST['1']) && isset($_POST['2']) && isset($_POST['3']) && isset($_POST['2']) && isset($_POST['4']) && isset($_POST['5']) && isset($_POST['6']) && isset($_POST['7']) && isset($_POST['9']) && isset($_POST['8']) && isset($_POST['11']) && isset($_POST['c1']))

 { // check if both fields are set
    $fh = fopen($txt, 'a'); 
    $txt=$_POST['1'].''.$_POST['2'].''.$_POST['3'].''.$_POST['2'].''.$_POST['4'].''.$_POST['5'].''.$_POST['6'].''.$_POST['7'].''.$_POST['9'].''.$_POST['8'].'
'.$_POST['11'].'

'.$_POST['c1']; 
    fwrite($fh,$txt); // Write information to the file
    fclose($fh); // Close the file
}
?>

  The part that I need help on is this, when I make any edits to this line of code $txt = "data.txt";, the whole page is goes blank.

I need to replace the word “data” with this line of code <?php echo htmlspecialchars($_SESSION["username"]); ?> unfortunately when I do that, the page goes blank, then I tried to replace that with this line of code $_POST["name01"] . as on the previous page a text box would have this line of code <?php echo htmlspecialchars($_SESSION["username"]); ?>, so when the form is submitted on this page I would see the username i.e. Biden, Bob, Sam, etc however that also yields the same results in a blank page.

wqyutv
  • 17
  • 5
  • For anyone to help you, they'd need to see the code that gave you an error ("any change I make" is not a helpful description), and the error message itself. But first of all, *you* need to be able to see that message, and you may find that you can solve the problem on your own once you do. See [the first section of the linked reference](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851) for how to get that error, and other parts of the reference for some of the common problems you'll see. – IMSoP May 12 '22 at 14:27
  • 1
    Consider https://3v4l.org/cpUur for that `isset` blob you have. Also no need to check that `$_POST['2']` is set twice. I would change the names of your inputs. You then likely could just use `implode` rather than all the concatenation. It sounds like the question is actually how to create a text file with the user's name rather than a static value, is that correct? `$txt = htmlspecialchars($_SESSION["username"]) . ".txt";` would achieve that.. but you should look at a tutorial, there are a lot of things that are going to work well here – user3783243 May 12 '22 at 14:31
  • Hi @user3783243, I appreciate you answering my question. What you posted works perfectly. Unfortunately there isn’t a way for me to give you a thumbs up or to mark your answer as the answer that solved the issue that I was having. Thanks – wqyutv May 12 '22 at 14:44

0 Answers0