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'] . "\n";
$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!
Related
I am having trouble with a file upload portion of a project. I have created the form with the inputs but when i try to "upload" the file it does not get posted into the $_FILES array. Code posted below. I have declared an enctype within the from tag but the file still does not get posted into the array it only shows Array()
here is my form code:
<?php
require("includes/application_top.php");
$pageTitle = "Add A Product";
require("includes/header.php");
print_r($_FILES);
?>
<div class="container-fluid py-4">
<div class="row ">
<div class="col">
<form method="POST" action="s.php" enctype="multipart/form-data">
<!-- Product Name input -->
<div class="form-outline">
<input type="text" id="ProductName" class="form-control" />
<label class="form-label" for="ProductName"> Product Name</label>
</div>
</div>
<div class="col">
<!-- Product Description input -->
<div class="form-outline">
<input type="text" id="ProductDesc" class="form-control" />
<label class="form-label" for="ProductDesc">Product Description</label>
</div>
</div>
</div>
<hr />
<div class="row">
<div class="col">
<!-- Product Price input -->
<div class="form-outline">
<input type="number" min="0.00" max="10000.00" step="0.01" id="ProductPrice" class="form-control" />
<label class="form-label" for="ProductPrice">Product Price </label>
</div>
</div>
<div class="col">
<!-- Img Upload input -->
<div class="form-outline input-group mb-3 ">
<input type="file" id="ImageUpload" class="form-control" />
<input type="submit" value="Upload" />
</div>
</div>
</div>
</form>
</div>
<?php
require("includes/footer.php")
?>
Any form field must have a name attribute in order to populate PHP's $_GET , $_POST or $_FILES superglobals after submitting the form.
$_GET and $_POST depend on the form method, and $_FILES is specific to file uploads (HTTP POST method only).
Your file input should then look like this:
<input type="file" id="ImageUpload" class="form-control" name="image_upload" />
Given the image_upload name I wrote, you should be able to get the file informations using $_FILES['image_upload].
Read about file uploads in the documentation: Handling file uploads.
You have to provide a name attribute to your fields, both $_POST and $_FILES work on the name attribute, not the ID. id is mostly used when you do something with the field using javascript, like when submitting the form with an ajax request.
In your case, you would need names:
<input type="file" id="ImageUpload" class="form-control" />
would become
<input type="file" name="ImageUpload" class="form-control" />
The same goes for your other input fields, you need a name attribute before you will see then in $_POST.
I have a problem, I am trying to make some forms in bootstrap however it messed up.
Once I converted the forms to bootstrap related they are no longer doing the job they're supposed to do.
What I am trying to do is to save form logs to a txt file, but they won't save.
When I add name="test" it won't work, instead it writes in the url.
URL: localhost/save.php?John
save.php
<?php
$myfile = fopen("test.txt", "a+");
$txt = "Name : ".$_POST['test451']." -> Surname: ".$_POST['loki'];
fwrite($myfile, $txt);
fclose($myfile);
?>
index
<form action="/save.php" class="needs-validation" novalidate>
<div class="row">
<div class="col-md-6 mb-3">
<label for="firstName">First name</label>
<input type="text" class="form-control" id="firstName" name="loki" placeholder="" value="" required>
<div class="invalid-feedback">
Valid first name is required.
</div>
</div>
<div class="col-md-6 mb-3">
<label for="lastName">Last name</label>
<input type="text" class="form-control" id="lastName" name="test451" placeholder="" value="" required>
<div class="invalid-feedback">
Valid last name is required.
</div>
</div>
</div>
</form>
Seeing the comment you left with this code: (edit: you added that in the question just now in an edit)
<form action="/save.php" class="needs-validation" novalidate>
Forms default to a GET method if there is no POST implied. So you're getting the ?John back because of it.
Add method="post" in your form.
i got template php from internet. then i want make other insert. but i dont know this code work
in code have submit button like this
<input type="submit" class="btn btn-primary" value="Добави">
and form like this
<div class="form-group">
<label>Nama</label>
<input type="text" class="form-control" name="name" required="required">
</div>
<div class="form-group">
<label> Deskripsi</label>
<textarea class="form-control" name="description"></textarea>
</div>
<div class="form-group">
<label>Gambar</label>
<input type="file" name="image" class="form-control" required="required">
</div>
<div class="form-group">
<label>Harga</label>
<input type="text" name="price" class="form-control" required="required">
</div>
<div class="form-group">
<label>
Cafe Yang Menjual</label>
<select name="cafe_id" class="form-control" required="required">
<option value="">Silahkan Pilih Restoran Yang Akan Menjual Makanan Ini</option>
<?php foreach($admin->getAllMenus() as $menu) { ?>
<option value="<?php echo $menu->id; ?>"><?php echo $menu->nama; ?></option>
<?php } ?>
</select>
</div>
and php syntax from that food.php
if(isset($_POST["name"])){
$image_name=$_FILES["image"]["name"];
$image=$_FILES["image"]["tmp_name"];
$image_name=time().$image_name;
move_uploaded_file($image, "../foods/".strtolower($image_name));
$_POST["image"]=strtolower($image_name);
$admin->addFood($_POST);
}
and modul code for insert
public function addFood($data){
if($this->db->query("insert into foods(name,image,description,price,cafe_id) values('".$data['name']."','".$data['image']."','".$data['description']."',".$data['price'].",".$data['cafe_id'].")")){
echo"<script>window.location.href='index.php?page=foods';
alert('Успешно добавяне на храна');</script>";
}
else{
echo"<script>window.location.href='index.php?page=foods';
alert('Проблем при добавяне на храна');</script>";
}
}
i dont know how that code connect with that submit button. with value value="Добави"
that i know is isset($_POST["value submit button"])) but in this code there is name.
I'm not sure I totally understand your question but basically a <form> HTML element can be used to transfer data to the server, such as:
<form method="POST" action="food.php">
...
</form>
Then when the submit method is triggered, the data will be POSTed to food.php.
Just to be clear, you need to have PHP installed and running on a webserver before this will work; you can't just copy PHP code into a HTML file and hope it works.
I am trying to update a mysql database but every time I get the Notice: Array to string conversion. I really can't figure out what I'm doing wrong here. Can someone kindly help?
My form
<div class="panel-body">
<form role="form" method="post" action="client_post.php" class="form-horizontal">
<fieldset>
<div class="form-group">
<label class="col-sm-3 control-label">Hospital No:
</label>
<div class="col-sm-5">
<input required class="form-control" name="Hospital_no" placeholder="Patients' Hospital Number">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Date of New Status</label>
<div class="form-group">
<label class="col-sm-3 control-label">New Status</label>
<div class="col-sm-5">
<select required name ="art_status" class="form-control">
<option></option>
<option value = "ART Restart" name="ART Restart">ART Restart</option>
<option value = "ART Transfer Out" name="ART Transfer Out" >ART Transfer Out</option>
<option value = "Pre ART Transfer Out" name="Pre ART Transfer Out">Pre ART Transfer Out</option>
<option value = "Lost to Followup" name="Lost to Followup">Lost to Followup</option>
<option value = "Stopped Treatment" name="Stopped Treatment">Stopped Treatment</option>
<option value = "Known Death" name="Known Death">Known Death</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-5">
<input class="btn btn-primary" type="submit" value="Update" name="submit" >
</div>
</div>
</fieldset>
</form>
I'm trying to make the update from this page (client.php)
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
$Hosp_no = ['Hospital_no'];
$art_status = ['art_status'];
$art_date = ['art_start_date'];
$update_client = "UPDATE `pat_reg` SET `art_status` = '$art_status', `art_start_date` = '$art_date' WHERE `Hospital_no` = '$Hosp_no'";
if (mysqli_query($dbcon,$update_client))
{
echo"<script>alert('Record Updated')</script>";
//echo"<script>window.open('client_update.php','_self')</script>";
}
$dbcon->close();
}
Please help.
Using the $_POST superglobal allows you to access the values submitted in the form.
Change the following:
$Hosp_no = ['Hospital_no'];
$art_status = ['art_status'];
$art_date = ['art_start_date'];
to this:
$Hosp_no = $_POST['Hospital_no'];
$art_status = $_POST['art_status'];
$art_date = $_POST['art_start_date'];
PS. You should be escaping the user's input before querying the database (or, ideally, use prepared statements) or you may find yourself being hacked very quickly if released to the public.
here is my html
<form name="station" method="post" action="/stations/new" role="form">
<div class="form-group">
<label class="control-label required" for="station_name">Name</label>
<input type="text" id="station_name" name="station[name]" required="required" maxlength="255" class="form-control" >
</div>
<div class="form-group">
<div class="checkbox">
<label for="station_active">
<input type="checkbox" id="station_active" name="station[active]" value="1" />Active</label>
</div>
</div>
<div class="form-group">
<button type="submit" id="station_submit" name="station[submit]" class="btn btn-primary">Ajouter</button>
</div>
<input type="hidden" id="station__token" name="station[_token]" class="form-control" value="aze123aze" >
</form>
i want to get my form using the crawler. I tried the selectButton method like this
$form = $crawler->selectButton('station[submit]')->form(array());
but i get the error : InvalidArgumentException: The current node list is empty.
what is the problem ?
Unfortunately I have no enough rating to just write a comment instead of put it in the answer.
So, could you please show how are you getting $crawler? Problem might be:
$crawler not point to DOM which contains this form
this form appears on page after some java script actions(Ajax for example), but not sure that this is your case.
The selectButton method accept the value The button text. So Try with:
$form = $crawler->selectButton('Ajouter')->form(array());
Hope this help