Reputation: 1800
This is the PHP script I wrote in a folder of my server:
<?php
$answer = $_POST['ans'];
if ($answer == "yes.") {
$testo = "qwe\r\n";
$documento ="/other/yes.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
else {
$testo = "qwe\r\n";
$documento ="/other/no.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
fclose($identificatore);
header('http://mk7vrlist.altervista.org/other/poll.html');
?>
This code takes the answer from a radiogroup and if the answer is Yes
, it makes a file called yes.txt
. Else it makes a file called no.txt
. This is the image.
This is the HTML code:
<form name="mainform" action="poll.php" method="POST">
<br />
<input type="radio" name="yes" value="yes">Yes, I like it.<br>
<br />
<input type="radio" name="yes" value="no" onclick="apri();">No, I want the old layout.
</fieldset>
//other code...
<input type="submit" value="Send" />
</td>
</table>
</form>
When I click the "Send" button, the script doesn't save anything on the server. Do you know why?
Upvotes: 0
Views: 367
Reputation: 74217
First both your radio buttons should be called name="ans"
and not name="yes"
Your if ($answer == "yes.")
should not have a dot in it.
And your header is not properly formatted:
header('http://mk7vrlist.altervista.org/other/poll.html');
should read as:
header('Location: http://mk7vrlist.altervista.org/other/poll.html');
headers
on PHP.net: http://php.net/manual/en/function.header.phpThis:
<input type="radio" name="yes" value="no" onclick="apri();">
onclick="apri();
does not belong in there. If you're going to use this, would be used in conjunction with a submit
button.
This $testo = "qwe\r\n";
may need to be reformatted as $testo = "qwe" . "\n";
Linux and Windows react differently using \r
<form name="mainform" action="poll.php" method="POST">
<br />
<input type="radio" name="ans" value="yes">Yes, I like it.<br>
<br />
<input type="radio" name="ans" value="no">No, I want the old layout.
</fieldset>
<input type="submit" value="Send" />
</td>
</table>
</form>
<?php
$answer = $_POST['ans'];
if ($answer == "yes") {
$testo = "YES\n";
$documento ="yes.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
else {
$testo = "NO\n";
$documento ="no.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
fclose($identificatore);
header('Location: http://mk7vrlist.altervista.org/other/poll.html');
// echo "ok done";
?>
EDIT (optional as a counter method)
You may also want to use your answers as a counter instead of appending data which will eventually could grow very large.
NOTE: You must first create two files with the number 0
(zero) inside them.
yes_count.txt
and no_count.txt
Here is a tested example of such a way:
<?php
$answer = $_POST['ans'];
if ($answer == "yes") {
$fr_yes = fopen("yes_count.txt", "r");
$text_yes = fread($fr_yes, filesize("yes_count.txt"));
$fw = fopen("yes_count.txt", "w");
$text_yes++;
fwrite($fw, $text_yes);
// will echo the counter but you can use header to redirect after
echo $text_yes;
// header('Location: http://mk7vrlist.altervista.org/other/poll.html');
}
else {
$fr_no = fopen("no_count.txt", "r");
$text_no = fread($fr_no, filesize("no_count.txt"));
$fw = fopen("no_count.txt", "w");
$text_no++;
fwrite($fw, $text_no);
// will echo the counter but you can use header to redirect after
echo $text_no;
// header('Location: http://mk7vrlist.altervista.org/other/poll.html');
}
?>
Upvotes: 1
Reputation: 117
you are trying to write in / (root directory) php user have permissions over the folder?
try this: /project_folder -poll.php -other (create other folder with write permissions).
poll.php
<?php
$base = __DIR__; //if your php version dont work __DIR__ try: dirname(__FILE__);
if ($answer == "yes.") {
$testo = "qwe\r\n";
$documento =$base."/other/yes.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
else {
$testo = "qwe\r\n";
$documento =$base."/other/no.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
fclose($identificatore);
header('http://mk7vrlist.altervista.org/other/poll.html');
Upvotes: 1
Reputation: 302
For debugging purposes, you should use a die statement when trying to fopen.
$fh = fopen($documento, 'a') or die('Could not append to file');
and if error reporting is on, you can always use error_get_last() to get a more detailed description of what's failing.
Upvotes: 1
Reputation: 2158
One problem is you are using $_POST['ans'] but your actual radio button is called 'yes'.
Also there's a stray closing fieldset tag in your form.
Upvotes: 3