I made a form for newsletter subscription in my footer website. The code for this is:
<h4>Newsletter Subscribe:</h4>
<form method="POST" action="<?php URL::show("Newsletter", "create") ?>">
<div class="col-md-12">
<div class="input-group">
<input type="text" class="form-control" name="subscriber" placeholder="john.doe#domain.com">
<input type="submit" class="btn btn-primary" value="Subscribe">
</div>
</div>
</form>
When I click on the submit button will call the createAction method from NewsletterController that looks like this:
public function createAction(){
if($_SERVER["REQUEST_METHOD"] == "POST"){
$validation = true;
$error = "";
if(trim($_POST["subscriber"]) == ""){
$validation = false;
$error = "The field 'subscriber' is required!";
}
if(trim($_POST["subscriber"]) != ""){
if(!preg_match('/^([a-z0-9]+([_\.\-]{1}[a-z0-9]+)*){1}([#]){1}([a-z0-9]+([_\-]{1}[a-z0-9]+)*)+(([\.]{1}[a-z]{2,6}){0,3}){1}$/i', $_POST["subscriber"])){
$validation = false;
$error = "The field 'subscriber' must contain a valid input!";
}
}
if($validation){
$checkSubscriber = $this->newsletterRepository->checkSubscriber($_POST["subscriber"]);
if(!$checkSubscriber){
$newsletter = new Newsletter();
$newsletter->setEmail($_POST["subscriber"]);
$result = $this->newsletterRepository->insert($newsletter);
if(!$result){
$error = "There was an error in the newsletter
}
}else{
$error = "The e-mail already registered for newsletter!";
}
}
}else{
URL::redirect("Restaurants", "listAll");
}
}
Now, after I made all the validations and inserting the data in database, I don't want to go to another webpage, I want to display a popup with confirmation message if everything was ok or an error message if there was an error. Something like this:
<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Newsletter Subscription</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container-fluid">
<?php if($result): ?>
<p style="color:green">Newsletter subscription successfuly!</p>
<?php elseif($error): ?>
<p style="color:red"><?php echo $error; ?></p>
<?php endif; ?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
But I don't know how to trigger the modal after all the operations and validation are done. Can anyone help please?
One of the easiest ways I can think is using javascript. After the validation is done successfully you can call a javascript function like this:
echo "<script type="text/javascript">the_function_you_want_to_call_from_javascript_file();
</script>";
Than in your javascript file you can call the modal and make your own customization.
Hope it helps :)
Related
My problem is I am trying to call the PHP file while clicking the button on my bootstrap but it is not working for me here by I paste my code of bootstrap and PHP kindly guide me for the solution
Thanks in advance
I am tired of changing button to form and all of these but not working I am working on PHP 7 and Bootstrap 4
<?php include "includes/db.php"; ?>
<?php
// echo "<h1> Testing </h1>";
// $db = mysql_select_db("cateory", $connection); // Selecting Database from Server
function(){
if (isset($POST['submit'])){
// $connect=mysqli_query($connection,)
$cat_title=$POST['cat_title'];
if($cat_title==""||empty($cat_title)){
echo "The field should not empty";
}
else{
$query="INSERT INTO category (cat_title)";
$query .=" VALUE ('{$cat_title}')";
$create_category_query = mysqli_query ($connection,$query);
if(!$create_category_query){
die ('QUERY FAILED'.mysqli_error($connection));
}
}
}
}
?>```
<!-- ADD CATEGORY MODAL -->
<div class="modal fade" id="addCategoryModal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-success text-white">
<h5 class="modal-title">Add Category</h5>
<button class="close" data-dismiss="modal">
<span>×</span>
</button>
</div>
<div class="modal-body">
<form action="" method="post">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title">
</div>
</div>
<div class="modal-footer">
<?php insert_categories(); ?>
<button class="btn btn-success" type="submit" data-dismiss="modal">Save Changes</button>```
</div>
</form>
</div>
</div>
Do change your code like:
<?php
if ( isset( $_POST['submit']) ) {
// $connect=mysqli_query($connection,)
$cat_title = $_POST['cat_title'];
if($cat_title==""||empty($cat_title)){
echo "The field should not empty";
} else {
$query="INSERT INTO category (cat_title)";
$query .= " VALUE ('{$cat_title}')";
$create_category_query = mysqli_query ($connection,$query);
if(!$create_category_query){
die ('QUERY FAILED'.mysqli_error($connection));
}
}
}
?>
<!-- ADD CATEGORY MODAL -->
<div class="modal fade" id="addCategoryModal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-success text-white">
<h5 class="modal-title">Add Category</h5>
<button class="close" data-dismiss="modal">
<span>×</span>
</button>
</div>
<div class="modal-body">
<form action="" method="post">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="cat_title">
</div>
<div class="modal-footer">
<?php insert_categories(); ?>
<button class="btn btn-success" type="submit" data-dismiss="modal">Save Changes</button>```
</div>
</form>
</div>
</div>
</div>
</div>
Problems with your code:
Your HTML formatting itself is wrong. you have broken your form tag with div of modal-body. You have to reconsider your HTML design.
No need to wrap PHP code in a function with no name.
You just have to use the code only.
You have named your input as title while saving/checking data as cat_title.
Check your whole codebase again.
Iam displaying pop up for login form in home page.when i given wrong email/password pop up gone , when i open pop up error message displayed in popup.
my home-page
<div class="modal fade" id="signin" role="dialog">
<div class="modal-dialog ">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"> Sign in </h4>
</div>
<div class="modal-body">
<div class="tabiib-popbox">
<div class="col-xs-12">
<ul class="nav nav-tabs poptabs">
<li class="tab signin"><a data-toggle="tab" href="#home"> Sign In</a></li>
</ul>
<div class="tabiib-signin">
<div class="tab-content">
<div id="home" class="tab-pane fade in">
<form class="pop-l" action="<?php echo base_url('LoginPopup');?>" method="post" name="signin" enctype="multipart/form-data">
<?php echo $this->session->flashdata('Message');?>
<div class="form-group">
<label for="email">Mobile Number / Email ID </label>
<input type="text" name="emailMobileNumber" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password</label>
<input type="password" name="password" class="form-control" id="pwd">
</div>
<div class="pop-b text-center">
<input type="submit" class="btn popbtn" value="Sign In">
</div>
</form>
</div>
Controller:
public function LoginPopup() {
$postDatas = $this->input->post();
$postData = str_replace (" ", "", $postDatas);
$postData['userType'] = 3;
$this->load->model('modelname');
$response = $this->modelname->login($postData);
if(isset($response['success_message'])) {
$user = set_user_session($response['data']);
$this->session->set_userdata('userdata', $user);
$sessionData = $this->session->userdata('userdata');
$this->session->set_userdata('Login',1);
$details = getDet($sessionData['userRef']);
$this->session->set_userdata('Details', $details);
redirect('');
}else{
$msg = '<div class="alert alert-danger"><i class="fa fa-ban"></i>';
$msg .= 'Mobile Number/Email or Password does not match';
$msg .= '</div>';
$mesg=$this->session->set_flashdata('Message', $msg);
redirect('');
}
}
But i need popup will stay and display error message in that pop up until we given correct email/password popup will not hide
The best way is that, use ajax to submit form . But if u don't want to use ajax then at first, if error comes, write error message in session. then after redirection in home page, check session if error is or not. If error is there then show the model with error message. But it's not a flash message remember it.
Try this
Replace
$msg = '<div class="alert alert-danger"><i class="fa fa-ban"></i>';
$msg .= 'Mobile Number/Email or Password does not match';
$msg .= '</div>';
$mesg=$this->session->set_flashdata('Message', $msg);
redirect('');
to
$data['msg'] = '<div class="alert alert-danger"><i class="fa fa-ban"></i>';
$data['msg'] .= 'Mobile Number/Email or Password does not match';
$data['msg'] .= '</div>';
$this->load->view('LoginView',$data); #LoginView is a view where you login form is present
Add some lines in LoginView
if(uri_string()=="/LoginPopup")
{
echo $msg;
}
If you use redirect('') then definitely page will be redirected and popup will not be shown. Use ajax for form submission it will solve your problem.
I am trying to get it where if there is an error when the user registers it will pop up with a modal like this. (I have it right now with a button to test it out)
So my question is how do I get it to pop up with out a button? Like if someone happened to forget their username, the modal would pop up with the error, But How do I get it where If there is an error the modal will pop up with it.?
<div class="buttonWrapper">
<button type="button" class="btn btn-success btn-df float-button-dark waves-effect waves-effect waves-button waves-float waves-dark" data-toggle="modal" data-target="#myModal2">Primary</button>
<div class="modal modal-default fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<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" id="myModalLabel1">Whoops! Something Happened.</h4>
</div>
<div class="modal-body">
<?php
//check for any errors
if(isset($error)){
foreach($error as $error){
echo '<p> - '.$error.'</p>';
}
}
//if action is joined show sucess
if(isset($_GET['action']) && $_GET['action'] == 'joined'){
echo "<h2 class='alert alert-success'>Registration successful, please check your email to activate your account.</h2>";
}
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
Maybe this could be useful:
<html>
<body>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
$('#idForm').on('submit', function (e) {
e.preventDefault();
var inputs = {
user : {
field : $('#fieldUser'),
error : 'field user required !'
},
password : {
field : $('#fieldPassword'),
error : 'field password required !'
},
email : {
field : $('#fieldEmail'),
error : 'field email required !'
}
};
var errors = new Array();
for(input in inputs) {
//console.log(inputs[input].field.empty())
if(inputs[input].field.val() === "") {
//alert(inputs[input].error)
errors.push(inputs[input].error);
}
}
// errors var contain all errors
if(errors.length > 0) {
// here show your modal with errors.
//$('#idModal').show();
//alert(errors);
var errorsJoin = errors.join('\n');
alert('please review this errors : \n' + errorsJoin);
} else {
alert('sign up successfully');
}
})
})
</script>
<form method="POST" id="idForm">
user : <input type="text" id="fieldUser"> <br>
pass :<input type="text" id="fieldPassword"> <br>
email :<input type="text" id="fieldEmail"> <br>
<input type="submit" value="Sign Up">
</form>
</body>
</html>
Having some problems with the error handling of a bootstrap modal. In the modal I have two inputs (both are required). I want to be able to display a simple "required" message if the form is submitted without one of the fields populated.
I tried doing this with PHP and it works in a page by itself, but when I put it in a modal the modal closes on submit and then if you re-open the modal you see the error messages. I really want the modal to stay open or re-open if the input fields are not valid when the user submits.
Any help would be appreciated! Thanks!
HTML (Start of the modal):
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModal" 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>
<h4 class="modal-title" id="myModalLabel">Add a vCenter</h4>
</div>
<div class="modal-body">
PHP (To graph the input and report any errors):
if ( !empty($_POST)) {
// keep track validation errors
$vcenternameError = null;
$vcenterownerError = null;
// keep track post values
$vcentername = $_POST['vcentername'];
$vcenterowner = $_POST['vcenterowner'];
// validate input
$valid = true;
if (empty($vcentername)) {
$vcenternameError = 'Please enter vCenter Name';
$valid = false;
}
if (empty($vcenterowner)) {
$vcenterownerError = 'Please select vCenter Owner';
$valid = false;
}
// insert data
if ($valid) {
$sql = "INSERT INTO ";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
header("Location: index.php");
}
}
?>
HTML (the rest of the modal):
<div class="panel-body">
<form class="form-horizontal" action="index.php" method="post">
<div class="<?php echo !empty($vcenternameError)?'error':'';?> form-group">
<div class="alert alert-warning">
Please input the FQDN of the vCenter and select an owner.
</div>
<label class="control-label">vCenter Name</label>
<div class="controls">
<input name="vcentername" type="text" placeholder="vCenter Name" value="<?php echo !empty($vcentername)?$vcentername:'';?>" class="form-control">
<?php if (!empty($vcenternameError)): ?>
<div class="alert alert-warning alert-dismissable">
<?php echo $vcenternameError;?>
</div>
<?php endif; ?>
</div>
</div>
<div class="<?php echo !empty($vcenterownerError)?'error':'';?> form-group">
<label class="control-label">vCenter Owner</label>
<div class="controls">
<div class="btn-group" data-toggle="button" role="group" aria-label="...">
<label class="btn btn-default">
<input type="radio" name="vcenterowner" value="Team1"> Team1
</label>
<label class="btn btn-default">
<input type="radio" name="vcenterowner" value="Team2"> Team2
</label>
<label class="btn btn-default">
<input type="radio" name="vcenterowner" value="Team3"> Team3
</label>
</div>
<?php if (!empty($vcenterownerError)): ?>
<div class="alert alert-warning alert-dismissable">
<?php echo $vcenterownerError;?>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<div class="form-actions">
<button type="submit" class="btn btn-success">Add</button>
<a class="btn btn-default" href="index.php">Back</a>
</div>
</form>
</div>
</div>
</div>
</div>
You can use jQuery to open the modal again if the modal contains errors when the page loads:
var modal = $('#createModal');
if(modal.find('div.alert-warning').length)
modal.modal();
But for the best user experience, you should call your PHP script with an ajax request: http://api.jquery.com/jquery.ajax/
Using the following code, a modal within a form with method="POST" but in the php-part the submitted value is not read. Can anyone tell me how to accomplish this?
<?php
if ($error) {
echo '<div class="alert alert-danger">'.$error.'</div>';
}
if ($message) {
echo '<div class="alert alert-success">'.$message.'</div>';
}
?>
<div class="container">
<button class="btn btn-success" data-toggle="modal" data-target="#myModal">
Launch modal
</button>
<form method="post">
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">You will receive an Access Code at your new email-address</h4>
</div>
<div class="modal-body">
<label for="text">Fill in your Access Code right here</label>
<input type="text" name="AccessCode" class="form-control"/>
</div>
<div class="modal-footer">
<input type="submit" name="submit" value="Confirm" data-dismiss="modal" class="btn btn-success btn-lg marginTop" />
</div>
</div>
</div>
</div>
</form>
</div>
The php part looks like this:
<?php
if ($_POST['submit']=='Confirm') {
print_r($_POST);
$error="No errors";
$message="Approved!";
} else {
$error="Error!";
$message="Nothing received";
}
?>
Form needs an action ..
<form method="post">
Has no idea where it is supposed to post the information to.
<form method="post" action="phpPageToSendTo.php">
will work..
To test, at the top of your page in the php section add in
$testValue = "";
if(isset($_POST["AccessCode"]))
{
$testValue = $_POST["AccessCode"] ;
echo "<h1> BIG LETTERS WITH THE ACCESS CODE :" . $testValue . "</h1>";
}
?>
Now in your form HTML item use the action="" Run the page and test the form.
Resolution :
And it seems you cannot have another event attached to your submit.. data-dismiss="modal".