-2

I want to make a little game for my friends. This is the part where they have to enter their names and then click the submit button. Their names will then be written in a log file and then redirected to another page. But I can't seem to make it work. Either it won't write their names in the log file or it won't redirect. It probably a simple solution but I can't seem to figure it out. How do I sent data to a log file and redirect to another page with the same button?

    <form method='POST'>
        <textarea name='info'></textarea>
        <input type=submit>
    </form>

<?php
    $name = $_POST['info'];
    file_put_contents("log/" . "$_SERVER[REMOTE_ADDR]" . ".log", $name . PHP_EOL, FILE_APPEND);
?>
  • I don't see a redirect. You may have tried using a header, but without you knowing, it's most likely throwing you a headers sent, this having error enabled that is. – Funk Forty Niner Dec 20 '18 at 01:54

2 Answers2

0

You could use AJAX, but that would require javascript.

The simplest solution solution would probably be:

<?php
        $name = $_POST['info'];
        file_put_contents("log/" . "$_SERVER[REMOTE_ADDR]" . ".log", $name . PHP_EOL, FILE_APPEND);
        header("Location: destination_path.php");
        exit;
    ?>
    <form method='POST'>
        <textarea name='info'></textarea>
        <input type=submit>
    </form>

Of course, replace destination_path.php with whatever your destination path is.

Jonhasacat
  • 140
  • 7
0
<?php
if (isset($_POST['value'])) {
    file_put_contents("log/{$_SERVER['REMOTE_ADDR']}" . ".log", $_POST['info'] . PHP_EOL, FILE_APPEND);
    header('Location: /yourpage.php');
    exit;
} else {
?>
<form method='POST'>
    <textarea name='info'></textarea>
    <input type="submit" name="value">
</form>
<?php
}

Replace yourpage.php with your destination page, this will also get rid of the headers already being sent error.

Love2Code
  • 898
  • 1
  • 7
  • 13