Reputation: 25
I'm new here.
Anyway, I'm working on an application system that logs your answers to the form into a text file called data.txt It was working for a while and then I tinkered with some stuff and it's not working anymore.
<html>
<head>
<link rel="stylesheet" href="style.css"
<meta charset="utf-8">
<title>Apply</title>
<link rel="icon" type="image/png" href="favicon.png">
</head>
<body>
<h1 class="unselectable">PvPCity Application</h1>
<form target=_blank action="index.php" method="post">
<input type="text" name="IGN" placeholder="IGN" autocomplete="off"><br>
<input type="text" name="Discord" placeholder="Discord" autocomplete="off"><br>
<input type="text" name="Age" placeholder="Age" autocomplete="off">
<textarea name="Why" placeholder="Why do you want to be staff?" autocomplete="off"></textarea>
<input onclick="window.location.href = 'http://pvpcity.dx.am/submitted/index.html';" type="submit" value="Apply" />
</form>
</body>
</html>
<?php
if( isset($_POST['IGN'] ) && isset( $_POST['Discord'] ) && isset( $_POST['Age'] ) && isset( $_POST['Why'] ) )
{
$txt=' ##### IGN: '.$_POST['IGN'].' ##### Discord: '.$_POST['Discord'].' ##### Age: '.$_POST['Age'].' ##### Why: '.$_POST['Why'] . PHP_EOL;
file_put_contents('data.txt', $txt, FILE_APPEND);
}
?>
Here is the code. If you could tell me what is wrong that would be great. The code here is from index.php The submit button redirects to a different page when you click. I honestly have no idea why it's not working
Upvotes: 1
Views: 82
Reputation: 698
I believe you don't need onclick="window.location.href = ...
Your submission can go to same file (index.php), you just need a better condition and since your inputs are not marked as required you might use something like this:
<?php
if(!empty($_POST)){
$txt = '';
foreach ($_POST as $key => $value){
$txt = $txt. " ##### ".$key.': '.$value;
}
file_put_contents('data.txt', $txt, FILE_APPEND);
}
?>
Upvotes: 1
Reputation: 1692
<html>
<head>
<link rel="stylesheet" href="style.css"
<meta charset="utf-8">
<title>Apply</title>
<link rel="icon" type="image/png" href="favicon.png">
</head>
<body>
<h1 class="unselectable">PvPCity Application</h1>
<form target=_blank action="" method="post">
<input type="text" name="IGN" placeholder="IGN" autocomplete="off"><br>
<input type="text" name="Discord" placeholder="Discord" autocomplete="off"><br>
<input type="text" name="Age" placeholder="Age" autocomplete="off">
<textarea name="Why" placeholder="Why do you want to be staff?" autocomplete="off"></textarea>
<input type="submit" value="Apply" />
</form>
</body>
</html>
<?php
if( isset($_POST['IGN'] ) && isset( $_POST['Discord'] ) && isset( $_POST['Age'] ) && isset( $_POST['Why'] ) )
{
$txt=' ##### IGN: '.$_POST['IGN'].' ##### Discord: '.$_POST['Discord'].' ##### Age: '.$_POST['Age'].' ##### Why: '.$_POST['Why'] . PHP_EOL;
if(file_put_contents('data.txt', $txt, FILE_APPEND)){
header('Location: http://pvpcity.dx.am/submitted/index.html');
}else{
echo 'data is not saved';
}
}
?>
I did some alteration in that above code for redirect to another website. You don't want to redirect on click, you can do that via PHP header().
header('Location: http://pvpcity.dx.am/submitted/index.html');
This will redirect after submit the form...
Upvotes: 0
Reputation: 33804
You should allow PHP to perform the redirection rather than allow people to potentially spoof others from your site. That said - you can simplify the collection of POSTed data a little by processing the POST array. The form will submit OK and redirect but the remote site will perhaps inaccurately report back that the application has been submitted - presumably this will be hosted on that site also so not necessarily an issue.
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$payload=array();
foreach( $_POST as $field => $value )$payload[]=sprintf('##### %s %s', $field, $value );
file_put_contents( 'data.txt', implode( ' ',$payload ).PHP_EOL, FILE_APPEND );
header('Location: http://pvpcity.dx.am/submitted/index.html');
}
?>
<html>
<head>
<link rel="stylesheet" href="style.css"
<meta charset="utf-8">
<title>Apply</title>
<link rel="icon" type="image/png" href="favicon.png">
</head>
<body>
<h1 class="unselectable">PvPCity Application</h1>
<form target=_blank method="post">
<input type="text" name="IGN" placeholder="IGN" autocomplete="off"><br>
<input type="text" name="Discord" placeholder="Discord" autocomplete="off"><br>
<input type="text" name="Age" placeholder="Age" autocomplete="off">
<textarea name="Why" placeholder="Why do you want to be staff?" autocomplete="off"></textarea>
<input type="submit" value="Apply" />
</form>
</body>
</html>
Upvotes: 1