Why can't I POST data from a Modal to PHP? - 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".

Related

How to pass value of a form to another form PHP?

I have a form A in index.php which I need to pass the value of 'notebook_id' to formB in create_new_note.php. I used POST method to do this but it is not working.
form A in index.php
<!-- Modal -->
<div class="modal" id="newNotebookModal" tabindex="-1" aria-labelledby="newNotebookModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<center><h5 class="modal-title" id="newNotebookModalLabel">Create a New Notebook</h5></center>
</div>
<div class="modal-body">
<form class="forms-sample" method="post">
<div class="form-group">
<label for="notebook_name_label">Notebook Name</label>
<input type="text" class="form-control" name="notebook_name" id="notebook_name" placeholder="Notebook Name">
</div>
</div>
<div class="modal-footer">
<button class="btn btn-outline-secondary btn-fw">Cancel</button>
<input type="submit" class="btn btn-primary" name="submit" value="Create"/>
</div>
</form>
</div>
</div>
</div>
</div>
Form B
<form class="forms-sample" method="post">
<div class="form-group">
<label for="note_title_label">Title</label>
<input type="hidden" name="notebook_id" value="<?php echo $notebook_id; ?>">
<input type="text" class="form-control" name="note_title" id="note_title" placeholder="Title">
</div>
<div class="form-group">
<textarea class="form-control" name ="note_content"id="note_content" rows="40"></textarea>
</div>
<button class="btn btn-outline-secondary btn-fw">Cancel</button>
<input type="submit" class="btn btn-primary" name="submit" value="Create"/>
</form>
I used MVC framework, hence this is my Controller code and Model code
// Controller
function addNotebook($std_id) {
$notebook = new ManageNotesModel();
$notebook->std_id = $std_id;
$notebook->notebook_name = $_POST['notebook_name'];
if($notebook->addNotebook() > 0) {
$message = "Your notebook has been created!";
echo "<script type='text/javascript'>alert('$message');
window.location = '../ManageNotesView/create_new_note.php'</script>";
}
}
// Model
function addNotebook(){
$sql = "insert into notebook(std_id, notebook_name)values(:std_id, :notebook_name)";
$args = [':std_id'=> $this->std_id, ':notebook_name'=>$this->notebook_name];
$stmt = DB::run($sql, $args);
$count = $stmt->rowCount();
return $count;
}
Please help me, I'm new to this I tried to make way through it, but cant seem to figure out whats wrong
As I see your code is creating a new row in the database. After saving row into database you want to know the last inserted id of the entity called Notebooks. Reuse that value and pass it into your template of creating titles. If your MVC framework does not know the last inserted id, you should customize it and add functionality to do that.
Check last_inserted_id

codeigniter get value after submit form

i have modal using form selection ajax with submit button
modal code
<div class="modal-body">
<div class="row" id="modal-body">
<div class="col-md-12">
<form class="form-horizontal">
<div class="box-body">
<div class="form-group">
<label for="project-loa" class="col-sm-4 control-label">Select Project</label>
<div class="col-sm-8">
<select id="project-load-modal" class="form-control" name="proj_name" style="width: 100%">
</select>
</div>
</div>
</div>
<!-- /.box-body -->
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button class="btn btn-primary">Download</button>
</div>
my value selection option is fine
controller
public function generate(){
$proj_name = $this->input->get('proj_name');
echo $proj_name;
}
i want get value from my selection form using controller.
when the submit is click, i get nothing in my console. what i do wrong?
//give id to submit button button_id
$("#button_id").on('click', function () {
var proj_name = $("#project-load-modal").val();
$.post(base_url + 'generator_url'+proj_name, function (response) {
if (response.code == '1') {
//message if you want to show
} else {
//message if you want to show
}
});
}
//make route
$route['generator_url/(:any)']='ControllerName/generate/$1';
Add action and method attribute to form element
<form class="form-horizontal" action="<?php echo base_url().'C_MRCR_A/generate'; ?>" method="get">
Take submit button inside the form element.
<button type="submit" class="btn btn-primary">Download</button>
</form>

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/

Categories