Reputation: 455
The following code is the code i am using:
<form action="sendinfo.php" method="post" id="form">
-Title:<br>
<input type="text" class="u-full-width" name="title" placeholder="Insert a title" >
<br>-Date<br>
<input class="u-full-width" type="text" name="date" placeholder="Click to add a date"><hr>
<br>-Buildyear:<br>
<input class="u-full-width" type="text" name="buildyear" placeholder="click to add buildyear">
<br>-rebuild<br>
<input id="1" type="radio" name="rebuildyes" value="yes"><label for="1">tes</label>
<input id="2" type="radio" name="rebuildno" value="No"><label for="2">No</label><hr>
<input type="submit" value="send" class="u-full-width">
</form>
The other code is :
<?php
//variables
$title = $_POST['title'];
$date = $_POST['date'];
$buildyear = $_POST['buildyear'];
$rebuildyes = $_POST['rebuildyes'];
$rebuildno = $_POST['rebuildno'];
//in browser
echo "<h1>The inserted data has been locally stored as " . $title . " " . $date . ".txt</h1>";
echo "<p>click <a href='index.html'>Here</a> to return to the previous screen</p>";
//write text file.
$file = fopen($title . " " . $date . ".txt","w");
if ($date) fwrite($file, "Date: " . $date . "\r\n");
fwrite($file, "\r\n");
if ($buildyear) fwrite($file, "Buildyear: " . $buildyear . "\r\n");
if ($rebuildyes) fwrite($file, "Rebuild? " . $rebuildyes . "\r\n");
if ($rebuildno) fwrite($file, "Rebuild? " . $rebuildno . "\r\n");
?>
Result of this code is a generated text file with the data filled in in the form given on the first page. The problem here is that i can't get the radio buttons to work, atleast i can't get the data out of them. When they are empty they should not be written down in the file, when they are selected they should be.
Also, please dont tell me this is a dangerous way of coding PHP since i know it gives users acces to write where ever the PHP has acces to, but this "project" will only be accesable for a small amount of people and won't actually be hosted server wise. That being said if you have a better way for me building this project, not risk wise, just PHP wise, please tell me since i'm just starting coding PHP.
Thanks
Upvotes: 1
Views: 252
Reputation: 131
This is the code I tested, the file is written correctly and the value for "rebuild" is also correct :
<form action="sendinfo.php" method="post" id="form">
-Title:<br>
<input type="text" class="u-full-width" name="title" placeholder="Insert a title" >
<br>-Date<br>
<input class="u-full-width" type="text" name="date" placeholder="Click to add a date"><hr>
<br>-Buildyear:<br>
<input class="u-full-width" type="text" name="buildyear" placeholder="click to add buildyear">
<br>-rebuild<br>
<input id="1" type="radio" name="rebuild" value="yes"><label for="1">Yes</label>
<input id="2" type="radio" name="rebuild" value="no"><label for="2">No</label><hr>
<input type="submit" value="send" class="u-full-width">
</form>
PHP :
<?php
//variables
$title = $_POST['title'];
$date = $_POST['date'];
$buildyear = $_POST['buildyear'];
//in browser
echo "<h1>The inserted data has been locally stored as " . $title . " " . $date . ".txt</h1>";
echo "<p>click <a href='index.html'>Here</a> to return to the previous screen</p>";
//write text file.
$file = fopen($title . " " . $date . ".txt","w");
if ($date) fwrite($file, "Date: " . $date . "\r\n");
fwrite($file, "\r\n");
if ($buildyear) fwrite($file, "Buildyear: " . $buildyear . "\r\n");
if (isset($_POST['rebuild']) && $_POST['rebuild'] == 'yes') {
fwrite($file, "rebuild? yes \r\n");
}
elseif (isset($_POST['rebuild']) && $_POST['rebuild'] == 'no') {
fwrite($file, "rebuild? no \r\n");
}
fclose($file);
?>
Upvotes: 1
Reputation: 261
<input type="radio" name="rebuild" value="yes"><label>Yes</label>
<input type="radio" name="rebuild" value="No"><label>No</label>
and get the value using
if(isset($_POST['submit'])){
echo $rebuild = $_POST['rebuild'];
die;
}
Upvotes: 1
Reputation: 131
Make your radio buttons a group :
<input id="1" type="radio" name="rebuild" value="yes"><label for="1">tes</label>
<input id="2" type="radio" name="rebuild" value="no"><label for="2">No</label><hr>
Then, check if the user selected the 'yes' value :
if (isset($_POST['rebuild']) && $_POST['rebuild'] == 'yes') {
// YOUR CODE
}
Upvotes: 0