I am using Bootstrap. I am trying to create a working form, but the submit function doesn't work. Here is my form HTML:
<form class="form-horizontal" role="form" action="mph.php" method="POST">
<div class="modal-body">
<fieldset>
<!-- Form Name -->
<legend>Map Problem Reporter</legend>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">What map?</label>
<div class="col-md-6">
<select id="selectbasic" name="selectbasic" class="form-control">
<option value="1">6 Stages of Parkour</option>
<option value="2">20 Stages of Parkour</option>
</select>
</div>
</div>
<!-- Multiple Radios -->
<div class="form-group">
<label class="col-md-4 control-label" for="radios">What kind of problem?</label>
<div class="col-md-4">
<div class="radio">
<label for="radios-0">
<input type="radio" name="radios" id="radios-0" value="1">
Blocks
</label>
</div>
<div class="radio">
<label for="radios-1">
<input type="radio" name="radios" id="radios-1" value="2">
Commands / Command Blocks
</label>
</div>
<div class="radio">
<label for="radios-2">
<input type="radio" name="radios" id="radios-2" value="">
Entities / Items
</label>
</div>
<div class="radio">
<label for="radios-3">
<input type="radio" name="radios" id="radios-3" value="">
Other (describe in the next text field)
</label>
</div>
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Describe the Problem</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea" name="textarea"></textarea>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">What is the priority?</label>
<div class="col-md-6">
<select id="selectbasic" name="selectbasic" class="form-control">
<option value="1">Huge! It breaks the map!</option>
<option value="2">Big</option>
<option value="">Noticeable</option>
<option value="">Barely Noticeable</option>
<option value="">Almost Hidden</option>
<option value="">I do not know</option>
</select>
</div>
</div>
<input type="submit" name="submit" value="Save Data">
</fieldset>
</form>
And here is my current PHP:
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "
";
$ret = file_put_contents('mpr.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
?>
When I click "Save Data" in the form, it doesn't update my mpr.txt with the information. Please help.
I see 2 problems:
1) You have created 2 forms
with
<form class="form-horizontal" role="form">
and
<form class="form-horizontal">
which should be 1
and
2) You are missing the action
attribute in <form>
tag i.e. it should be <form class="form-horizontal" role="form" action="<php file name>" method="<request type>">
correct these and I think you should be through.
You do not see the data in your .txt file
because you do not have any field named field1
and field2
in your html page. The field names have to match in both html and php files to write/read the data correctly. Please correct the names and you should be through.
Also would appreciate if you accept the answer.
The answer is quit obvious! PHP is trying to find field1 and field2 posted. But in your HTML form you did not define these two properties, consequently PHP won't update the file!
EDIT:
If you submit the form and print_r($_POST)
you will get the follow array:
Array ( [selectbasic] => 1 [textarea] => [submit] => Save Data )
As you see, there is no [field1]
or [field2]
in the response!
Which means PHP cannot proceed to to open and update the text file!
To prevent this, you should define field1
and field2
in your HTML form by adding these input tags or updating the existing ones.
For instance, if you are interested in the select
tag and the textarea
tag, you should whether update you HTML code or you php code.
You could go with PHP code by updating your if statement into if(isset($_POST['selectbasic'], $_POST['textarea']))
or in your HTML form change the name="selectbasic"
into name="field1"
and name="textarea"
into name="field2
and keep your current php code and so your current PHP if statement that says: if(isset($_POST['field1']) && isset($_POST['field2']))
I hope this explains it all!