1

i am quite a beginner in PHP and i wanted to create a input: If you click a button, Javascript will submit it and add linebreak (\n) at the end of what you wrote in the input box, and PHP will write the final result into a file called result.txt. However, when checking result.txt, there is nothing written at all.

HTML

<input id="test" type="text">
<form id="formie" action="test.php" method="post">
    <input id="testz" type="text" name="really" hidden="hidden" disabled="disabled"><br>
    <button onclick="trigger1()">Submit</button>
</form>
<!-- 2 input boxes, the hidden one is meant to be set to have the final result and with it i do the request -->
<script>
var int;
function trigger1() {
    int = document.getElementById('test').value;
    document.getElementById('testz').value = int + "\n";
    document.getElementById('formie').submit();
}
</script>

PHP

<?php
$postreq = $_POST["really"];
$finaldata = $postreq;

file_put_contents("result.txt", $finaldata, FILE_APPEND);

header("Location: https://domain.tld/test.html");
exit;
?>

No error when stopping the redirect (domain.tld/test.html) and no error in server logs. Why is this happening? I even tried to check this with jshint.com and phpcodechecker.com and both told me no errors

wreeper
  • 46
  • 6
  • 1
    `No error`...what about warnings? Are those enabled? P.S. This code might try to submit your form twice... easier to add the newline using PHP if that's what you really want to do. – ADyson Jun 21 '21 at 14:22
  • 2
    I dont think a disabled element can have a value. I would open developer console and view the request being sent to the PHP. – user3783243 Jun 21 '21 at 14:25
  • [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) -> Write a title that **summarizes the actual problem**. – Andreas Jun 21 '21 at 14:26
  • nothing in console either – wreeper Jun 21 '21 at 14:35
  • What have you tried to resolve the problem? Is this a JS problem (where the request is not sent properly to the server), or a PHP problem (where the request is not handled properly by the server)? – Nico Haase Jun 21 '21 at 14:39

2 Answers2

1

Credit to @user3783243: The problem lies here:

<input id="testz" type="text" name="really" hidden="hidden" disabled="disabled">

Disabled elements cannot have values

Working code:

<input id="testz" type="text" name="really" hidden="hidden">

wreeper
  • 46
  • 6
1

To answer the question, elements with the disabled attribute are not submitted or you can say their values are not posted

Try removing the "disabled" attribute from your hidden input.

Refer to: Disabled form inputs do not appear in the request, which has reference to the w3 specs explaining that as well.

Wadih M.
  • 12,810
  • 7
  • 47
  • 57