display error message in popup - php

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.

Related

Ajax call error not allowing me to view missing details from a form

I don't know what I've done but I think I have some error in my ajax query. I am expecting to have been able to see the missing details not entered in the signup form. I get 500 server error and undefined $("#signupform") errors.
I have tried searching here and tried inserting various comments into my code to try and see if it helps. I have only been coding for 10 weeks so it's all new to me. I took an online course that promised the earth but has zero support whatsoever.
I think the error may be coming from the connection.php file I have not allowing the code to progress to check out my input fields in the table?
I have these pages hosted on a subdomain here is the link http://welcomer.offyoucode.co.uk/WEBSITES/9.Notes%20App/ in case its easier to figure it out from there.
<!--connect to the database-->
<?php
$link = mysqli_connect("#", "#", "#", "#");
if(mysqli_connect_error()){
die("ERROR: Unable to connect:" . mysqli_connect_error());
echo "<script>window.alert('Hi!')</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Online Notes</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="styling.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Arvo&display=swap" rel="stylesheet">
<style>
</style>
</head>
<body>
<!--navbar-->
<nav role="navigation" class="navbar navbar-custom navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand">Online Notes</a>
<button type="button" class="navbar-toggle" data-target="#navbarCollapse" data-toggle="collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="navbarCollapse">
<ul class="nav navbar-nav">
<li class="active">Home<span class="caret"></span></li>
<li>Help</li>
<!--<li>Sign-Up</li>-->
<li>Contact Us</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Login</li>
</ul>
</div>
</div>
</nav>
<!--jumbotron with signup button-->
<div class="jumbotron" id="myContainer">
<h1>Online Notes App</h1>
<p>Your notes with you, wherever you go.</p>
<p>Easy to use, protects all your notes!</p>
<button type="button" class="btn btn-lg green signup" data-target="#signupModal" data-toggle="modal">Sign up - Its free</button>
</div>
<!--login form-->
<form method="post" id="loginform">
<div class="modal" id="loginModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h4 id="myModalLabel">Login:</h4>
</div>
<div class="modal-body">
<!--login message from php file-->
<div id="loginmessage"></div>
<div class="form-group">
<label for="loginemail" class="sr-only">Email:</label>
<input class="form-control" type="email" name="loginemail" id="loginemail" placeholder="Email" maxlength="50">
</div>
<div class="form-group">
<label for="loginpassword" class="sr-only">Password</label>
<input class="form-control" type="password" name="loginpassword" id="loginpassword" placeholder="Password" maxlength="40">
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="rememberme" id="rememberme">
Remember me
</label>
<a class="pull-right" style="cursor: pointer" data-dismiss="modal" data-target="#forgotpasswordModal" data-toggle="modal">
Forgot Password?
</a>
</div>
</div>
<div class="modal-footer">
<input class="btn green" name="login" type="submit" value="Login">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-default pull-left" data-dismiss="modal" data-target="signupModal" data-toggle="modal">Register</button>
</div>
</div>
</div>
</div>
</form>
<!--signup form-->
<form method="post" id="signupform">
<div class="modal" id="signupModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h4 id="myModalLabel">Sign up today and start using our Online Notes App! </h4>
</div>
<!--signup message from php file-->
<div id="signupmessage"></div>
<div class="modal-body">
<div class="form-group">
<label for="username" class="sr-only">Username:</label>
<input class="form-control" type="text" name="username" id="username" placeholder="Username" maxlength="35">
</div>
<div class="form-group">
<label for="email" class="sr-only">Email:</label>
<input class="form-control" type="email" name="email" id="email" placeholder="Email" maxlength="50">
</div>
<div class="form-group">
<label for="password" class="sr-only">Password:</label>
<input class="form-control" type="password" name="password" id="password" placeholder="Choose a password" maxlength="40">
</div>
<div class="form-group">
<label for="password2" class="sr-only">ConfirmPassword:</label>
<input class="form-control" type="password" name="password2" id="password2" placeholder="Confirm password" maxlength="40">
</div>
</div>
<div class="modal-footer">
<input class="btn green" name="signup" type="submit" value="Sign up">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</form>
<!--forgot password form-->
<form method="post" id="forgotpasswordForm">
<div class="modal" id="forgotpasswordModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h4 id="myModalLabel">Forgot Password? Enter your Email address:</h4>
</div>
<div class="modal-body">
<!--forgot password message from php file-->
<div id="forgotpasswordMessage"></div>
<div class="form-group">
<label for="forgotpasswordEmail" class="sr-only">Email:</label>
<input class="form-control" type="email" name="forgotpasswordEmail" id="forgotpasswordEmail" placeholder="Email" maxlength="50">
</div>
</div>
<div class="modal-footer">
<input class="btn green" name="login" type="submit" value="Login">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-default pull-left" data-dismiss="modal" data-target="signupModal" data-toggle="modal">Register</button>
</div>
</div>
</div>
</div>
</form>
<?php
include "footer.php";
?>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="index.js"></script>
</body>
</html>
//Ajax Call for the sign up form
//once the form is submitted
$("#signupform").submit(function(event){
//prevent default php processing
event.preventDefault();
//collect user inputs
var datatopost =
$(this).serializeArray();
console.log(datatopost);
//send them to signup.php using ajax
$.ajax({
url: "signup.php",
type: "POST",
data: datatopost,
success: function(data){
if(data){
$("#signupmessage").html(data);
}
},
error: function(){
$("#signupmessage").html("<div class='alert alert-danger'>There was an error with the Ajax Call. Please try again later.</div>");
},
});
// $.post({}).done().fail();
});
//ajax call successful: show error or success message
//ajax call fails: show ajax call error
//ajax call for the login form
//once the form is submitted
//prevent default php processing
//collect user inputs
//send them to login.php using ajax
//if php files return "success": redirect user to notes page
//otherwise show error message
//ajax call fails: show ajax call error
//ajax call for the forgot password form
//once the form is submitted
//prevent default php processing
//collect user inputs
//send them to login.php using ajax
//ajax call successful: show error or success message
//ajax call fails: show ajax call error
<?php
//<!--start session-->
session_start();
include("connection.php");
//<!--check user inputs-->
// <!--define error messages-->
$missingUsername='<p><strong>Please enter a username</strong></p>';
$missingEmail='<p><strong>Please enter an email address</strong></p>';
$invalidEmail='<p><strong>Please enter a valid email address</strong></p>';
$missingPassword='<p><strong>Please enter a password</strong></p>';
$invalidPassword='<p><strong>Your password should be at least 8 characters long and contain at least 1 capital letter and 1 number!</strong></p>';
$differentPassword='<p><strong>Passwords do not match! </strong></p>';
$missingPassword2='<p><strong>Please confirm your password</strong></p>';
$errors = "";
$username = "";
$email = "";
$password = "";
$myFile = "db.json";
$arr_data = array(); //create empty array
// <!--get username, email, password, password2-->
//get username
if(empty($_POST["username"])){
$errors .= $missingUsername;
}else{
$username = filter_var($_POST["username"], FILTER_SANITIZE_STRING);
}
//get email
if(empty($_POST(["email"]))){
$errors .= $missingEmail;
}else{
$email = filter_var($_POST["email"],
FILTER_SANITIZE_EMAIL);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$errors .= $invalidEmail;
}
}
// get passwords
if(empty($_POST["password"])){
$errors .= $missingPassword;
}elseif(!(strlen($_POST["password"])>= 8 and preg_match('/[A-Z]/', $_POST["password"])and
preg_match('/[0-9]/', $_POST["password"])
)
){
$errors .= $invalidPassword;
}else{
$password = filter_var($_POST["password"], FILTER_SANITIZE_STRING);
if(empty($_POST["password2"])){
$errors .= $missingPassword2;
}else{
$password2 = filter_var($_POST["password2"], FILTER_SANITIZE_STRING);
if($password !== $password2){
$errors .= $differentPassword;
}
}
}
// <!--if there are any errors print errors-->
if($errors){
$resultMessage = '' . $errors . '';
echo $resultMessage;
exit;
}
//<!--no errors-->
// <!--prepare variables for the query-->
$username = mysqli_real_escape_string($link, $username);
$email = mysqli_real_escape_string($link, $email);
$password = mysqli_real_escape_string($link, $password);
$password = md5($password);
//128 bits -> 32 characters
// <!--if username exists in the table print error-->
$sql = "SELECT * FROM users WHERE username = '$username'";
mysqli_query($link, $sql);
if($!result){
echo '<div class="alert alert-danger">Error running the query!</div>';
// echo '<div class="alert alert-danger">' . mysqli_error($link) . '</div>';
exit;
}
$results = mysqli_num_rows($results);
if($results){
echo '<div class="alert alert-danger">That username is already in use. Do you want to log in?</div>'; exit;
}
// <!--else-->
// <!--if email exists in the users table print error-->
$sql = "SELECT * FROM users WHERE email = '$email'";
mysqli_query($link, $sql);
if(!$result){
echo '<div class="alert alert-danger">Error running the query!</div>';
exit;
}
$results = mysqli_num_rows($results);
if($results){
echo '<div class="alert alert-danger">That email is already in use. Do you want to log in?</div>'; exit;
}
// <!--else-->
// <!--create a unique activation code-->
$activationKey = bin2hex(openssl_random_pseudo_bytes(16));
//byte: unit of data = 8 bits
//bit: 0 or 1
//16 bytes = 16*8 = 128 bits
//2*2*2*2*2*2....*2
//16*16.......*16
//32 characters
// <!--insert user details and activation code in the users table-->
$sql = "INSERT INTO users ('username', 'email', 'password', 'activation') VALUES ('$username', '$email', '$password', '$activationKey')";
mysqli_query($link, $sql);
if(!$result){
echo '<div class="alert alert-danger">There was an error inserting the user details into the database</div>';exit;
}
// <!--send the user an email with a link to activate.php with their email and activation code-->
$message = "Please click on this link to activate your account:\n\n";
$message = "http://https://welcomer.offyoucode.co.uk/WEBSITES/9.Notes%20App/activate.php?email=" . urlencode($email) . "&key=$activationKey"; if(mail($email, 'Confirm your Registration', $message, 'From:'.'onlinenotes#gmail.com')){
echo '<div class="alert alert-success">Thank you for registering. A confirmation email address has been sent to $email. Please click on the activation link to activate your account.</div>';
}
?>
Solved! On line 34 in signup.php extra brackets around email input. Solved using ERROR_REPORTING(E_ALL); ini_set('display_errors', 1) to narrow down my fault.

I want to call php inserting query when i am clicking the bootstrap button

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.

How to display a modal window, when validation is complete?

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 :)

Reload Bootstrap Modal on PHP variable not valid

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/

Why can't I POST data from a Modal to PHP?

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".

Categories