-1

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.

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.

  • 3
    Your form is using `GET` while you're trying to fetch the data using `$_POST`. Change your form to `method="post"`. A good idea is to turn `display_errors` on in your local PHP environment. Read more here: [How do I get PHP errors to display?](http://stackoverflow.com/questions/1053424/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 '18 at 05:35
  • 1
    @MagnusEriksson you're right, I'm sorry. Flag removed. – Dawid Zbiński Aug 13 '18 at 05:53

1 Answers1

0

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">
Sinto
  • 3,710
  • 11
  • 32
  • 59
  • 1
    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 '18 at 05:46
  • 1
    You should also remove the part about the concatenation (since the OP's version is correct): https://3v4l.org/4kabN – Magnus Eriksson Aug 13 '18 at 05:48
  • Any ideas on how to have all responses to go a new line within the code, something like `"\n"` maybe? – phphelpplease Aug 13 '18 at 06:17
  • can you explain or show a sample, its not clear for me. `\n` can be used for newline. – Sinto Aug 13 '18 at 06:30
  • If you need a new blank line use a `\n` or if you need a new line text as `` then, you have to add it in concating as `PHONE: ".$phone."\n \n";` – Sinto Aug 13 '18 at 06:56
  • I have it as `$text = "NAME: ".$forename." ".$surname." PHONE: ".$phone."\n"
    ";` but it doesn't go to the next line?
    – phphelpplease Aug 13 '18 at 07:02
  • Nevermind, got it. `".$phone."\n`, had an extra `"`. Thank You :) – phphelpplease Aug 13 '18 at 07:03
  • @Sinto. If on the `responses.php` page I wanted to echo part of the submission how would that work? I'd need to find a way of using `uswords` to capitalise the user's name. `

    Thank you, php echo $forename; ?>for your submission

    `. The text shows but the user's name is not echoed?
    – phphelpplease Aug 13 '18 at 18:29
  • In HTML: `

    Thank you, =ucfirst($forename); ?>for your submission

    ` In PHP: `echo "

    Thank you, ". ucfirst($forename)." for your submission

    ";`
    – Sinto Aug 14 '18 at 05:16
  • `=` is a short code for ` – Sinto Aug 14 '18 at 05:19