0

I am trying to make a website where you input a value to order food. In php i am trying to make it create a txt file that i can view. I have gotten it to make the file, but instead of a number, it simply displays 'Fries: Array' and the 'Array' should be a number. My php and HTML code is as follows...

HTML:

<input type="number" name="Fries" min="0" max="69"><br>

PHP:

<?php
$path = "Fries.txt";
$fh = fopen("Fries.txt", "w") or die("Unable to open file!");
$fries = array(['Fries']);
$string = 'Fries: '. strval($fries[0]);
fwrite($fh, $string);
fclose($fh);
?>` 

If anyone can tell me how to get php to read HTML form data, that wiuld be great

Genaritis
  • 9
  • 3
  • 1
    There are many knowledge gaps in your code. 1- your not reading any data from your form; 2- you defined `$path` just to NOT use it, since you're passing a string to `fopen`; 3- when you define `array(['Friel'])` you actually have an array at position `0` (hence the 'Array' output). 4- (extra) not using the [file_put_contents](http://php.net/manual/en/function.file-put-contents.php) function. – FirstOne Sep 18 '17 at 02:52
  • Possible duplicate of [How to get input field value using PHP](https://stackoverflow.com/questions/13447554/how-to-get-input-field-value-using-php) – FirstOne Sep 18 '17 at 03:23
  • You should be looking for tutorials, not asking questions, yet tbh. A quick google search would provide the answers... – FirstOne Sep 18 '17 at 03:24

2 Answers2

1

Assuming that you're aware of all of the potential pitfalls of taking user input and writing it to a file without any type of validation: square brackets in PHP are a shortcut for defining a new array. So what you've written is equivalent to:

$fries = array(array('Fries'));

Also, you're assigning your new array the string value "fries," when you say you're trying to get this from your user input. Try the following:

...
$fries = 'Fries: ' . $_REQUEST['Fries'];
fwrite($fh, $string);
...

No need to use strval() - value is already a string.

And as far as validation, you may want to add the following before you assign your $fries variable:

if (is_numeric($_REQUEST['Fries'] && $_REQUEST['Fries'] >= 0 && $_REQUEST['Fries'] <= 69)
beaumontwebdev
  • 301
  • 2
  • 4
  • _Assuming that you're aware of all of the potential pitfalls_, judging by the number / types of mistakes, I wouldn't go anywhere near that assumption. – FirstOne Sep 18 '17 at 03:05
  • Php will not do the type conversion for you, it will already be a string (even with `type="number"`). And `$_REQUEST['fries']` is an _Undefined index: fries_ notice (it should be `Fries`). – FirstOne Sep 18 '17 at 03:22
0

HTML:

<form method="post">
    <input type="number" name="fries" min="0" max="69"><br>
    <input type="submit" name="submit">
</form>

PHP:

<?php
$path = "Fries.txt";
$fh = fopen($path, "w") or die("Unable to open file!");

$string = 'Fries: '. filter_input(INPUT_POST,'fries');
fwrite($fh, $string);
fclose($fh);
?>
Johnny Dew
  • 971
  • 2
  • 13
  • 29