I'm actually developing a Web site where you can register yourself from a form.
I've create a code in PHP for managing the errors which could happen (wrong number, mail...)
Here is my problem : When I send the form, I cant manage to stay on it to overwrite the form with the errors encounters (if there are some).
I'm using Bootstrap form the form and I have absolutely no clue of how to do that.
This is the my HTML
<div class="modal fade" id="ins" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<form id="FormIns" action="Index.php" method="post">
<div class="modal-header">
<h4 class="text-center">Inscription</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="text" class="form-control" name="lastname" id="lname" placeholder="Nom" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="text" class="form-control" name="firstname" id="fname" placeholder="Prénom" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="text" class="form-control" name="nid" id="nidentifiant" placeholder="Identifiant" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="password" class="form-control" name="npass" id="npassword" placeholder="Mot de Passe" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="password" class="form-control" name="cnpass" id="cnpassword" placeholder="Confirmer Mot de Passe" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="mail" class="form-control" name="mail" id="mail" placeholder="Adresse Email" maxlength="25">
</div>
</div><br/>
<div class="row">
<div class="col-md-offset-3 col-md-6">
<input type="text" class="form-control" name="phone" id="phone" placeholder="Numéro de Téléphone" maxlength="25">
</div>
</div><br/>
<div class="modal-footer">
<input class="btn btn-primary" type="submit" value="Inscription" name="log">
<a class="btn btn-default" data-dismiss="modal">Annuler</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Firstly, you should add labels to your form fields. These help with the page as well as give a good entry point for error messages.
A simple approach would be to set the form-action to the form page, and put validation errors in an associative array with values like
$errors['name'] = "Name did not match"; and so on and so forth.
Then you add a <span> at the end of the label like (roughly) this:
<span>
<?php echo (isset($errors['name'])) ? $errors['name'] : "";?>
</span>
</label>
This would make PHP evaluate the errors and put them into the label so they can be easily connected to the field.
Lastly, to continue, you add a redirect to the next page, if the validation is successful, so create a redirect function and send the validated data to the next page.
€: for good effects, you can use php to append the alert or warning class to the corresponding form-control groups or inputs.
If you are using a script to check that the values are right, the first thing you have to do is stop the form action.
<script type="text/javascript">
$("#FormIns").submit(function(e){
e.preventDefault();
// your code to check the form goes here
//once you finished and everything is right then:
$( "#FormIns" ).submit();
});
</script>
Related
This side sumit and my question is how to received customer order details in my email id when customer fill the checkout page and click on order now button using JavaScript and PHP i add my cart code below please check it i am new in developing please help.
"How to get customer details and order in mail using javascript and php?"
thanks in advance.
This is how my cart look
<section id="" class="container-fluid">
<article id="" class="row">
<div id="mask"></div>
<div class="modal fade" id="cart" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Cart</h5>
</div>
<form name="contactform" method="post" action="payscript.php">
<div class="modal-body">
<div class="container">
<div class="row">
<div class="col-md-6 order-md-4 mb-6">
<table id="cartents" class="show-cart table">
</table>
<div>Total price: <span class="total-cart" id="cartContent"></span></div>
<br>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Order now</button>
</div>
</div>
<div class="col-md-6 order-md-1">
<h4 class="mb-3">Billing address</h4>
<form class = "needs-validation" novalidate method="post" action="payscript.php">
<div class="mb-3">
<label for="fullname">Full name</label>
<div class="input-group">
<input type="text" class="form-control" id="phonenumber" placeholder="Enter your full name" required>
<div class="invalid-feedback" style="width: 100%;">
Your full name is required.
</div>
</div>
</div>
<div class="mb-3">
<label for="username">Phone no.</label>
<div class="input-group">
<input type="text" class="form-control" id="phonenumber" placeholder="Enter your phone no." required>
<div class="invalid-feedback" style="width: 100%;">
Your Phone no. is required.
</div>
</div>
</div>
<div class="mb-3">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" placeholder="Enter your email">
<div class="invalid-feedback">
Please enter a valid email address for shipping updates.
</div>
</div>
<div class="mb-3">
<label for="address">Address</label>
<input type="text" class="form-control" id="address" placeholder="Enter your address" required>
<div class="invalid-feedback">
Please enter your shipping address.
</div>
</div>
</div>
</div>
</form>
</article>
</section>
The form
You have a form that is being used in order to submit some data. Inside your form you have some input tags. Presumably you intend to submit the values of your inputs. In order to do so, edit your input tags and add a name attribute, like
<input type="text" class="form-control" id="fullname" name="fullname" placeholder="Enter your full name" required>
Receiving the values
You need to have a PHP code corresponding to the path that is being requested when the form is being submitted. In this case that would be payscript.php. Since you have method="post", in payscript.php you can refer to the received parameters via $_POST, like $_POST["fullname"].
The template
You will need to build a template. One way to do it is to generate via PHP a text, like
$message = "Hello {$_POST["fullname"]}";
Sending the email
For this purpose you can use the mail function, like:
mail(
"foo#bar.lorem", //To
"Hello", //Subject,
$message
);
Email support
In order to be able to support email sending, you will need to prepare your server to do so. Read more here: https://www.tutorialspoint.com/php/php_sending_emails.htm
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
On button click, four bootstrap tabs are displayed and by default first tab information is displayed.
But if the user clicks on the second tab instead of filling the information in the first tab an error message should be displayed and he should not be allowed to click on any of the tabs unless he fills the information in the first tab.
After filling the required information on the first tab he should be directed to the second tab and the information filled by the user in the first tab should be saved in the database on click of save button, which is there on the first tab.
For answer your question I just wrote a simple snippet where you can get when the form is filled or not.
inside isValid, true or false you can run all your custom code.
For enable disabled the bootstrap form I just removed or add the 'data-toggle="tab"' without add class or other css.
For check if the form is filled I added a class "required" for the fields that you want to controll and wrote a function where check 'on click' event if that fields are not null.
Alternatively if you want to implement a ready plugin for validate your form and spend less time to code your custom code well check this link
http://formvalidation.io/examples/bootstrap-wizard/
function validateForm() {
var isValid = true;
$('#installationForm .form-group .required').each(function() {
if ( $(this).val() === '' )
isValid = false;
});
return isValid;
}
$('.next-tab').on('click', function(event) {
var result = validateForm();
if (result) {
$('.next a').attr('data-toggle', 'tab');
} else {
$('.next a').removeAttr('data-toggle');
alert('You have to fill the form before!');
}
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<form id="installationForm" class="form-horizontal">
<ul class="nav nav-pills">
<li class="active">Site information</li>
<li class="next"><a class="next-tab" href="#database-tab" data-toggle="tab">Database</a></li>
</ul>
<div class="tab-content">
<!-- First tab -->
<div class="tab-pane active" id="basic-tab">
<div class="form-group">
<label class="col-xs-3 control-label">Site name</label>
<div class="col-xs-5">
<input type="text" class="required form-control" name="name" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">URL</label>
<div class="col-xs-7">
<input type="text" class="required form-control" name="url" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Owner email</label>
<div class="col-xs-5">
<input type="text" class="required form-control" name="email" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Description</label>
<div class="col-xs-7">
<textarea class="required form-control" name="description" rows="6"></textarea>
</div>
</div>
</div>
<!-- Second tab -->
<div class="tab-pane" id="database-tab">
<div class="form-group">
<label class="col-xs-3 control-label">Server IP</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="dbServer" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Database name</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="dbName" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Database user</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="dbUser" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Password</label>
<div class="col-xs-5">
<input type="password" class="form-control" name="dbPassword" />
</div>
</div>
</div>
<!-- Previous/Next buttons -->
<ul class="pager wizard">
<li class="previous">Previous</li>
<li class="next">Next</li>
</ul>
</div>
</form>
<div class="modal fade" id="completeModal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Complete</h4>
</div>
<div class="modal-body">
<p class="text-center">The installation is completed</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal">Visit the website</button>
</div>
</div>
</div>
</div>
I am new to both bootstrap and php. I have created a registration modal form to register users for my site and it registers perfectly. However, I would like to retrieve a users details from my database based on their User ID and display their details in a registration form for them to edit and update.
What I don't know how to do is display the records in the form fields...
Please help by providing a simple syntax for this.
I am using PHP and MySql
This is my modal form
<div class="modal fade" id="updatelecModal" tabindex="-1" role="dialog" aria-labelledby="updatelecModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<!--End of modal-->
<!-- Start of form-->
<form id="updatelecForm" class="form-horizontal" role="form" method="post" action="#" nonvalidate>
<div class="form-group">
<label for="student_id" class="col-sm-3 control-label">Lecturer ID</label>
<div class="col-lg-9">
<input type="text" class="form-control" name="lec_id" required placeholder="Enter User ID">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Full name</label>
<div class="col-lg-4">
<input type="text" class="form-control" name="firstName" required placeholder="First name" />
</div>
<div class="col-lg-4">
<input type="text" class="form-control" name="lastName" required placeholder="Last name" />
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-3 control-label">Email</label>
<div class="col-lg-9">
<input type="email" class="form-control" name="lec_email" required placeholder="#######.com">
</div>
</div>
<label class="col-xs-3 control-label" name="lec_gender">Gender:</label>
<label class="radio-inline control-label">
<input type="radio" name="optionsRadio" id="gender" value="Male">Male
</label>
<label class="radio-inline control-label">
<input type="radio" name="optionsRadio" id="gender" value="Female">Female
</label>
<div class="form-group">
<label for="password" class="col-sm-3 control-label">Password</label>
<div class="col-sm-7">
<input type="password" id="password" class="form-control" name="password" required placeholder ="Enter your Password">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-3 control-label">Confirm Password</label>
<div class="col-sm-7">
<input type="password" id="password2" class="form-control" name="cpassword" data-validate-linked="password" required placeholder ="Re-enter your Password">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Register</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="reset" class="btn btn-default">Reset</button>
</div>
</form>
<!--End of registration form-->
</div>
</div>
</div>
</div>
I dont know how to include the php in this form as it disappears
Also if you could include links for further reading on php I would really appreciate.
Without seeing any code it's difficult to give more specific help. However, what I would do is store the user's id in a session. When they view the form brab the id from the session and do a SQL call to your db, something like:
SELECT firstName,lastName,email, FROM table_name WHERE id=$id
Obviously what you select, the table_name, and the where clause depends on your own tables and variables.
Run your SQL command and store the columns into variables. Then place the variable into the value of the input:
<input type="text" value="<?php echo $name != '' ? $name : ''; ?>" />
Basically, what I did above is a shorthand if statement that says if $name isn't blank they print it, otherwise print nothing.
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']