I have a simple login form, at the top of this page i check for the form being submitted to i can process the login if it's been submitted.
if (!empty($_POST)) {
echo "form submitted";
}
else {
?>
<form style="margin-bottom: 0px !important;" class="form-horizontal" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="content">
<h4 class="title">Login Access</h4>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" placeholder="Username" id="username" class="form-control">
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" placeholder="Password" id="password" class="form-control">
</div>
</div>
</div>
</div>
<div class="foot">
<button class="btn btn-primary" data-dismiss="modal" type="submit">Log in</button>
</div>
</form>
<?php } ?>
I've tested with a var_dump($_POST);, but it doesn't display any post variables when submitted. I'm using XAMPP in my production enviroment, so i'm wondering if thats the issue?
The form doesn't have method="post", so you are making a GET request and $_POST will be empty.
Even if that wasn't the case, none of your form controls have name attributes, so they cannot be successful (so won't generate any data to put in the form).
By default forms submit their values via GET. To change this you need to set the method attribute of the <form> tag to POST
<form method="post" style="margin-bottom: 0px !important;" class="form-horizontal" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
^^^^^^^^^^^^^^^
HERE
As Quentin pointed out already, you are also missing the name attribute in your form elements. So even your $_GET would be empty if you checked it.
Related
How do I give an error message inside modal after clicking update?
In my example here I gave a required attribute on the input field.
I want to change the error message based on Form Validation Best Practices.
(This code is part of CRUD PHP file for updating data in a table I had)
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="form-update" action="proses_edit.php" name="modal_popup" enctype="multipart/form-data" method="POST">
<div class="form-group" style="padding-bottom: 20px;">
<label for="Name">Name</label>
<input type="hidden" name="modal_id" id="edit-id" class="form-control" value="<?php echo $r['modal_id']; ?>"/>
<input type="text" name="modal_name" id="edit-name" class="form-control" value="<?php echo $r['modal_name']; ?>" required />
</div>
<div class="form-group" style="padding-bottom: 20px;">
<label for="Description">Age</label>
<input type="text" id="edit-description" class="form-control" value="<?php echo $r['description']; ?>" required />
</div>
<div class="modal-footer">
<button class="btn btn-success" type="submit">
Update
</button>
<button type="reset" class="btn btn-danger" data-dismiss="modal" aria-hidden="true">
Cancel
</button>
</div>
</form>
</div>
</div>
</div>
Here’s how form validation works with Bootstrap, taken from the documentation:
HTML form validation is applied via CSS’s two pseudo-classes, :invalid and :valid. It applies to <input>, <select>, and <textarea> elements.
Bootstrap scopes the :invalid and :valid styles to parent .was-validated class, usually applied to the <form>. Otherwise, any required field without a value shows up as invalid on page load. This way, you may choose when to activate them (typically after form submission is attempted).
As a fallback, .is-invalid and .is-valid classes may be used instead of the pseudo-classes for server side validation. They do not require a .was-validated parent class.
To achieve somewhat of what you're trying to do, taken from the example here you can use custom styles and create tooltips for each input and display a response based on the validation result. You will need to add the novalidate property to your <form> in order to prevent the default browser validation for this to work.
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="needs-validation" novalidate>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationCustom01">First name</label>
<input type="text" class="form-control" id="validationCustom01" placeholder="First name" value="Mark" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustom02">Last name</label>
<input type="text" class="form-control" id="validationCustom02" placeholder="Last name" value="Otto" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustomUsername">Username</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupPrepend">#</span>
</div>
<input type="text" class="form-control" id="validationCustomUsername" placeholder="Username" aria-describedby="inputGroupPrepend" required>
<div class="invalid-feedback">
Please choose a username.
</div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationCustom03">City</label>
<input type="text" class="form-control" id="validationCustom03" placeholder="City" required>
<div class="invalid-feedback">
Please provide a valid city.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="validationCustom04">State</label>
<input type="text" class="form-control" id="validationCustom04" placeholder="State" required>
<div class="invalid-feedback">
Please provide a valid state.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="validationCustom05">Zip</label>
<input type="text" class="form-control" id="validationCustom05" placeholder="Zip" required>
<div class="invalid-feedback">
Please provide a valid zip.
</div>
</div>
</div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
</label>
<div class="invalid-feedback">
You must agree before submitting.
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit form</button>
</form>
<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
'use strict';
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
</script>
More information on Bootstrap Validation: https://getbootstrap.com/docs/4.0/components/forms/#validation
I have made an application using codeigniter, and everything was working fine few day ago. But suddenly, yesterday, when I ran the application, I am getting error, when I am submitting data from the forms which contain enctype="multipart/form-data". Null data is received at the controller. When I remove enctype, than everything goes fine, but due to image upload, I have to keep enctype in some form tags.
<form action="<?= site_url('nasty_v2/dashboard/uploadPaid?key=').$this->my_func->scpro_encrypt("betul"); ?>" method="POST" role="form" enctype="multipart/form-data">
<div class="portlet box purple-sharp">
<div class="portlet-title">
<div class="caption">
<i class="fa fa-image"></i>Upload Payment Proof For
</div>
</div>
<div class="portlet-body flip-scroll" align="center">
<span style = "color : #b706d6;"><h2><strong>#<?= (120000+$orid); ?></strong></h2></span>
<div class="form-group">
<div class="fileinput fileinput-new" align="center" data-provides="fileinput">
<div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px; line-height: 150px;"></div>
<div>
<span class="btn red btn-outline btn-file">
<span class="fileinput-new"> Select image </span>
<span class="fileinput-exists"> Change </span>
<input type="hidden" value="" name="title"><input type="file" name="fileImg"> </span>
Remove
</div><div class="clearfix"> </div><button type="submit" class="btn btn-primary btn-circle"><i class="fa fa-upload"> Submit</i></button>
</div>
</div>
</div>
<input type="hidden" name="or_id" id="inputOr_id" class="form-control" value="<?= $or_id; ?>">
</div>
</form>
I checked the HTTP request using Telerik Fiddler 2, and the found that form is successfully submitting data in request, but that data is not reaching controller. What should be the issue?
I have this form
<div class="row">
<div class="col-lg-8 offset-2">
<form class="form-signin" method="POST">
{{ csrf_field() }}
<div class="form-label-group mt-1">
<label class="text-dark-orange" for="name">Naam</label>
<input type="text" id="name" name="name" class="form-control sentje-inputfield-payment" placeholder="Vul hier uw naam in" required>
</div>
<div class="form-label-group mt-1">
<label class="text-dark-orange" for="note">Notitie</label>
<textarea type="text" id="note" name="note" class="form-control sentje-inputfield-payment" placeholder="Vul hier eventueel een notitie in"></textarea>
</div>
</div>
</div>
<div class="row mt-5">
<div class="col-lg-8 offset-2">
<button type="submit" class="btn btn-block btn-lg sentje-button">Betalen</button>
</form>
</div>
</div>
When I click the button it sends the $paymentrequest with it. (I already sent that parameter to the view) How would I also pass on the data that has been putted in the form?
Give your form an ID and make the action that of the <a> URL:
<form action="{{ route('paymentrequest.preparePayment', [$paymentrequest->unique_link, $paymentrequest]) }}" id="my-form" class="form-signin" method="POST">
Change your <a> to just button with the type="submit" and a form="<form-id>"
<button type="submit" form="my-form" class="btn btn-block btn-lg sentje-button">Betalen</button>
Using the form property allows you to close your form tag and keep the button outside and allows you to still submit it and keep your code neater.
I have a site that has two contact forms (One is avaliable in a modal and the other is on the bottom of the page). Both forms have the same structure e.g same inputs and names etc... they both also have the same action method.
My question is - how can I use the same action method on both forms where the $_POST variables are dependant on what form is submitted? For example if I fill out the second contact form, the action gets the data from the first contact form.
I thought that one way would be to give both forms different id's e.g firstForm and secondForm and then pass the id of the form as a parameter to the action method. Although this could work I am not sure if it is an efficient way or if there is an easier way?
I have completely disregarded the fact that I could actually make two separate action methods but that is just ridiculous and a lot of redundant programming when I know I can just have one action method to serve both forms, but I'm just at a loss of how to implement it properly.
This is the first one:
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" >
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Flick me an email!</h3>
<p>I will get back to you ASAP!</p>
</div>
<div class="modal-body">
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="assets/php/sendemail.php">
<div class="form-group"><input class="form-control required" type="text" name="name" placeholder="Name" data-placement="top"></div>
<div class="form-group"><input class="form-control email" type="email" name="email" placeholder="example#email.com" data-placement="top" ></div>
<div class="form-group"><textarea class="form-control" name="message" placeholder="Your message here.." data-placement="top" style="height: 200px;" ></textarea></div>
<div class="form-group"><button type="submit" id="modal-submit" class="btn btn-danger btn-lg">Send Message</button><button class="btn" data-dismiss="modal" aria-hidden="true" style="float: right;">Cancel</button> </div>
</form>
</div>
</div>
</div>
</div>
And the second one:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="assets/php/sendemail.php">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required"
placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="email" name="email" class="form-control" required="required"
placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control"
rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" id="submit" class="btn btn-danger btn-lg">Send Message
</button>
</div>
</div>
</div>
</form>
In each of your forms you can add a hidden input element between your <form>...</form> tags to indicate which form is being submitted.
In your first form you could have this element:
<input type="hidden" name="type" value="form1">
Then in your second form add this element
<input type="hidden" name="type" value="form2">
When your form is submitted, the receiving PHP script can read the type value as a post variable that could you act on, e.g.,
if ($_POST['type']=='form1') {
// Do this
} else if ($_POST['type']=='form2') {
// Do this instead
}
Maybe you don't want to add a hidden input and you could add a parameter to the GET string of your form's action.
So, in your first form you can do this - add ?type=form1 to the end of your action:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="assets/php/sendemail.php?type=form1">
And, in your second form you can do this - add ?type=form2 to the end of your action:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="assets/php/sendemail.php?type=form2">
In the sendmail.php script you can access the value using the $_GET variable:
if ($_GET['type']=='form1') {
// Do this
} else if ($_GET['type']=='form2') {
// Do this instead
}
Place a:
<input type="hidden" name="formPosted" value="1">
inside every form that has action to specific php file, and for each form change input value, then in php use if else to see which form is submitted :) and then your code... :)
I have a HTML login form that is processed and displayed to the user by PHP if they are not logged in. This form is set to POST to /user.php which in turn accesses my authentication class, and either creates a session and returns true or false and does not log the user in.
My problem is that whatever I try, the HTML form simply will not POST values to the PHP processing part.
This is my current source code:
Log in form:
<div class="panel-body">
<!-- BEGIN ifLogInError -->
<div class="alert alert-danger">
<p>Either your username or password was incorrect. Please try again. Forgotten your password?</p>
</div>
<!-- END ifLogInError -->
<form class="form-horizontal" method="post" action="{ROOT}/user.php">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" value="" placeholder="Your username">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" value="" placeholder="Your password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" value="Sign in" />
</div>
</div>
</form>
</div>
User.php (as it currently stands):
echo "<pre>";
var_dump($_POST);
var_dump(file_get_contents("php://input"));
echo "</pre>";
phpinfo();
Both var_dumps return the following, suggesting the form is not POSTing:
array(0) {
}
string(0) ""
What am I doing wrong here?
You haven't given your <input>s a name attribute.
Needs <input name="myname">
Then in the output file put $_POST['myname']