POST to server only posts field names and not user responses PHP

Clash Royale CLAN TAG#URR8PPP
POST to server only posts field names and not user responses PHP
I've created a simple form and some PHP to handle it and write to the server:
<div class="main">
<div class= "formholder">
<form action="phone.php" method="get">
<input type="text" placeholder= "First Name:" name="forename" required><br>
<input type="text" placeholder= "Last Name:" name="surname" required><br>
<input type="text" maxlength="12" placeholder="Phone Number: +44" name="phone" required><br>
<input type="submit">
</form>
</div>
</div>
The PHP I'm using is as follows:
<?php
$forename = $_POST["forename"];
$surname = $_POST["surname"];
$phone = $_POST["phone"];
$text = "NAME: $forename $surname <br>
PHONE: $phone<br>";
$file = fopen("./data/responses.html","a+");
fwrite($file, $text);
fclose($file);
?>
Whenever a response is submitted the server adds the field names to the responses.html document, but not the data that the user submitted.
responses.html
The result in the file is;
NAME: <br>
PHONE: <br>
It adds the fields each time, just not the user inputs. Any ideas why? Thanks in advance.
EDIT 1, NEWLINES
I'm trying to make it so that after each response the code starts a new line. I've tried this;
<?php
$file = fopen("./data/responses.html","a+ /n");
fwrite($file, $text);
fclose($file);
?>
Currently the response doc looks like this:
<!--Phone Number Responses-->NAME: Name1 Name1 <br>PHONE: Phone1 <!--Next Person:--><br>NAME: Name2 Name2 <br>PHONE: Name2 <!--Next Person:--><br>
But I'd prefer a new line started for each response;
<!--Phone Number Responses-->
NAME: Name1 Name1 <br>PHONE: Phone1 <!--Next Person:--><br>
NAME: Name2 Name2 <br>PHONE: Name2 <!--Next Person:--><br>
And so on. I added the <!--Next Person:--> comment to differentiate because they all save to the same line.
<!--Next Person:-->
GET
$_POST
method="post"
display_errors
@MagnusEriksson you're right, I'm sorry. Flag removed.
– Dawid Zbiński
Aug 13 at 5:53
1 Answer
1
You can try like below written code:
$forename = $_POST["forename"];
$surname = $_POST["surname"];
$phone = $_POST["phone"];
$text = "NAME: ".$forename." ".$surname." <br>PHONE: ".$phone."<br>";
As Magnus Eriksson said in his comment that to change:
<form action="phone.php" method="get">
to
<form action="phone.php" method="post">
The concatenation in the OP's question is fine. The variables are inside a double quoted string so they will resolve properly. The issue is that the OP is using the wrong method.
– Magnus Eriksson
Aug 13 at 5:46
Ya, I have noted that now & added that pointed.
– Sinto
Aug 13 at 5:46
You should also remove the part about the concatenation (since the OP's version is correct): 3v4l.org/4kabN
– Magnus Eriksson
Aug 13 at 5:48
Any ideas on how to have all responses to go a new line within the code, something like
"n" maybe?– phphelpplease
Aug 13 at 6:17
"n"
can you explain or show a sample, its not clear for me.
n can be used for newline.– Sinto
Aug 13 at 6:30
n
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your form is using
GETwhile you're trying to fetch the data using$_POST. Change your form tomethod="post". A good idea is to turndisplay_errorson in your local PHP environment. Read more here: How do I get PHP errors to display?. If you do that, you should get some "undefined index"-warnings in your PHP.– Magnus Eriksson
Aug 13 at 5:35