I have this bootstrap modal view:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<form method = "POST" action = "<?php echo base_url('Monitor/myfct') ?>">
<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="myModalLabel">Add missed Tara or Harvest Product</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label class="font-noraml">
Select event type
</label>
<div class="input-group">
<select data-placeholder="Select" class="form-control chosen-select" style="width:350px;" tabindex="2">
<option value="Tara">
Tara Machine
</option>
<option value="Harvest">
Harvest Product Machine
</option>
</select>
</div>
</div>
<div class="form-group">
<label class="font-noraml">
Product list
</label>
<div class="input-group">
<select id ="harvestAjaxData" data-placeholder="Select" class="form-control chosen-select" style="width:350px;" tabindex="2">
</select>
</div>
</div>
<div class="form-group">
<label class="font-noraml">
Machine list
</label>
<div class="input-group">
<select id ="machineAjaxData" data-placeholder="Select" class="form-control chosen-select" style="width:350px;" tabindex="2">
</select>
</div>
<div class="form-group">
<label for="inputimpurities">Impurities</label>
<input type="text" class="form-control" id="inputimpurities" placeholder="Impurities" style="width:350px;">
</div>
<div class="form-group">
<label for="inputhumidity">Humidity</label>
<input type="text" class="form-control" id="inputhumidity" placeholder="Humidity" style="width:350px;">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
Except first one, fields are completed on ajax call with dynamic data (coming from mysql tables) and 2 user input fields, and the controller function where I send that form is this one Monitor/myfct:
public function myfct() {
$data= $this->input->post();
print_r($data);
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
}
I use it to test my form submit, I get the javascript alert(so the call was successful), but the print $data comes with a empty Array, also tried to print $_POST, same output. What is wrong in my modal view? Should I put the name="" for the options?
ps. I should add how the options from ajax look like:
for(var i = 0; i< data.length;i++){
$('#harvestAjaxData').append('<option value="'+data[i]['name']+'">'+data[i]['name']+'</option>');
}
Try giving name attributes to selects
Related
I am making an add button and when clicked it brings up a modal where data can be entered but when clicked, this record gets sent through and to avoid mistakes, I wish to add a confirmation(yes or no) pop up to make sure the user is certain on submitting. Has anyone worked with this before?
Heres my modal code:
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Subject add</h5>
</div>
<form action="/test/save-subject" method="POST">
{{ csrf_field() }}
<div class="modal-body">
<div class="mb-3">
<label for="recipient-name" class="col-form-label">Subject ID:</label>
<input type="text" name="subjectcode" class="form-control" id="recipient-name" maxlength="6" style="text-transform:uppercase" required>
</div>
<div class="mb-3">
<label for="message-text" class="col-form-label">Description:</label>
<textarea name="detail" class="form-control" id="message-text" required></textarea>
</div>
<div class="mb-3">
<label for="recipient-name" class="col-form-label">Email Address:</label>
<input type="text" name="emailaddress" class="form-control" id="recipient-name" required>
</div>
<div>
<input type="checkbox" name="staff" value="1"> <label>Staff Update</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
I have created an Edit Item popup Bootstrap modal on home page. Now what I want is to update the selected item details using the PHP.
Here i am passing item id through the URL in order to select the item
Now when I press edit Button it opens a Popup with all the details in it( already filled) but when I try to click on Save Changes Button it just do nothing and My data not updated. I have also used POST method but its not working.
Here is the Button that opens the Popup Modal
<?php
$res= mysqli_query($db,"SELECT* FROM `books`;");
if(mysqli_num_rows($res)>0){
while($row= mysqli_fetch_assoc($res)){
if($row!=0)
{
$bid= $row['book_id'];
?>
<ul>
<li>
<a onclick="$('#editModal<?php echo $row['book_id']?>').modal('show');" class="btn-show-modal edit-btn" data-toggle="modal"><i style="padding-right: 140px;" class="fas fa-edit"></i></a>
</li>
<!--Here is the Popup Modal for Edit -->
<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="updatebook.php" method="POST">
<div class="form-group">
<label for="isbn" class="col-form-label">ISBN</label>
<input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
</div>
<div class="form-group">
<label for="title" class="col-form-label">Enter Book Title</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
</div>
<div class="form-group">
<label for="author" class="col-form-label">Enter Author Name</label>
<input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
</div>
<div class="form-group">
<label for="image" class="col-form-label">Image URL</label>
<input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
</div>
<div class="form-group">
<label for="description" class="col-form-label">Enter Book Description</label>
<textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
</div>
<div class="form-group">
<label for="url" class="col-form-label">Book URL</label>
<input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" name="save">Save Changes</button>
</div>
</div>
</div>
</div>
<?php
}
}
}
else
{
echo"<h1 style='color: white;font-size:58px; font-family: Poppins; font-weight: 600;' class=m-2>U!Oh Nothing Found Here!</h1>
<button onclick=\"window.location.href='add_books_form.php'\" class=\"btn btn-info bg-primary m-5\">Contribute</button>";
}
?>
</ul>
Here is the updatebook.php Page
<?php
include "connection.inc.php";
if(isset($_POST['save']))
{
$id= $_POST['id'];
$isbn= $_POST['isbn'];
$title= $_POST['title'];
$author= $_POST['author'];
$image= $_POST['image'];
$desc= $_POST['description'];
$url= $_POST['url'];
$res = mysqli_query($db, "UPDATE books SET isbn= '$isbn', title= '$title', author= '$author',image= '$image', description= '$desc', url= '$url' WHERE book_id= '$id'");
?>
if($res)
{
<script type="text/javascript">
alert("Data Updated Successfully");
window.location.href= "home.php";
</script>
}
else
{
<script>
alert("Not Updated!");
window.location.href="home.php";
</script>
}
<?php
}
?>
Please Help me to solve this Update Issue i am facing for a long time. I have researched all through but didn't got any solution.
Change your Save button type to "submit" and remove the <a> hyperlink.
Use <form> as a part of both modal-body and modal-footer so the submit button will be part of it.
Take book_id in a hidden field so it will be the part of POST.
Look at the modal code below:
<div id="editModal<?php echo $row['book_id']?>" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit This Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="updatebook.php" method="POST">
<input type="hidden" name="id" value="<?php echo $row['book_id'];?>">
<div class="modal-body">
<div class="form-group">
<label for="isbn" class="col-form-label">ISBN</label>
<input type="text" class="form-control" name="isbn" value="<?php echo $row['isbn'];?>">
</div>
<div class="form-group">
<label for="title" class="col-form-label">Enter Book Title</label>
<input type="text" class="form-control" name="title" value="<?php echo $row['title'];?>">
</div>
<div class="form-group">
<label for="author" class="col-form-label">Enter Author Name</label>
<input type="text" class="form-control" name="author" value="<?php echo $row['author'];?>">
</div>
<div class="form-group">
<label for="image" class="col-form-label">Image URL</label>
<input type="text" class="form-control" name="image" value="<?php echo $row['image'];?>">
</div>
<div class="form-group">
<label for="description" class="col-form-label">Enter Book Description</label>
<textarea class="form-control" name="description" value="<?php echo $row['description'];?>"></textarea>
</div>
<div class="form-group">
<label for="url" class="col-form-label">Book URL</label>
<input type="text" class="form-control" name="url" value="<?php echo $row['url'];?>"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="save">Save Changes</button>
</div>
</form>
</div>
</div>
</div>
i have tried to create form input fields dynamically and i succeeded but when all other fields are being submitted, only the dynamically created fields are not please help out.. thanks..
my javascript is working fine, its creating the fileds just fine inside the modal..
the problem is when i am submitting it only the dynamically added fields are not submitted..
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method="post">
<div class="col-md-6">
<div class="form-group">
<h6> Contact person in case of emergency</h6
<input class="form-control" placeholder="Contact person" required="" pattern="([A-Z][a-z]*\s*[A-Z]*[a-z]*\s*[0-9]*)" title="Alphbetical characters only, capitalize first letter, full name then phone number" value="<?php echo $info[0]['contactperson'];?>" name="contactperson" type="text">
</div>
</div>
<input class="form-control" hidden placeholder="familymembers" id="fm" value="<?php echo $info[0]['familymembers'];?>" name="familymembers" type="number">
<div class="col-md-6">
<div class="form-group">
<h6> </h6>
<button class="btn btn-success">Edit Family Members List</button>
</div>
</div>
//this is a script to add new input fields daynamically
<script type="text/javascript">
function getfmembers(){
var fn=document.getElementById('fm').value;
for (var i = 0; i < 1; i++) {
document.getElementById('fm').value++;
k=document.getElementById('fm').value;
var btn='<div class="form-group"><input class="form-control" name="fmlis" placeholder="Name of the family member" id="'+k+'" type="text"></div>';
$("#after").after(btn);
alert(document.getElementById(k).name);
}
}
function removefmembers(){
var fn=document.getElementById('fm').value;
$("input").remove("#"+fn);
document.getElementById('fm').value--;
}
</script>
// this is the modal to submit the inputs and its inside the form tag
<div class="modal fade" id="seefmlist" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit Family Members List</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="row">
<div class="col-md-3">
<span onclick="getfmembers()" class="btn btn-sm btn-success">Add Member</span>
</div>
<div class="col-md-3">
<span onclick="removefmembers()" class="btn btn-sm btn-danger">Delete Last Member</span>
</div>
</div>
<br>
<div id="after">
<?php
$flist=explode(",",$info[0]['familylist']);
if (count($flist)>1) {
for ($qq=0; $qq < count($flist)-1 ; $qq++) {
echo '<div class="form-group"><input class="form-control" id="'.($qq+1).'" value="'.$flist[$qq+1].'" name="fmlist'.($qq+1).'" type="text"></div>';
}
}
?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Update changes</button>
</div>
</div>
</div>
</div>
</div>
<div class="text-center">
<button type="submit" name='submitupdate' class="btn btn-primary mt-4">Update Employee information</button>
</div>
</form>
line 31 :
for (var i = 0; i < 1; i++) {
only runs once, if you wanted to have this add more than one you will need to fix
also probably the problem you are hung up on
line 34 : name="fmlis" later in your code you have name="fmlist'.($qq+1).' so, on line 34 you probably want to concat k+1 to the name...
I have two modal in one page. One is for edit and the other is for create. In both modal contain select2 option. And the option I get from my database. I want to use the data for both of modal. But I get error in modal create and no error for modal edit. In modal create, it can't display the data in select option. Can anyone help me?
I try this
public function detail_produk(){
$data['item']=$this->admin_model->get_all_detail_produk();
$data['ukuran'] = $this->admin_model->get_ukuran();
$data['sidebar'] = $this->load->view('sidebar','',TRUE);
$data['header'] = $this->load->view('header','',TRUE);
$this->load->view('detail_produk', $data);
}
The View
<div class="modal fade" role="dialog" id="detail_user">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="form-group row">
<label class="col-sm-2 col-form-label">Ukuran</label>
<div class="col-sm-10">
<select class="form-control select2" style="width: 100%;" id="ukuran">
<option selected="true" disabled="disabled">Pilih Ukuran</option>
<?php foreach ($ukuran as $ukuran) { ?>
<option value="<?php echo $ukuran['id'];?>"><?php echo $ukuran['size'];?></option>
<?php } ?>
</select>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="button-simpan" class="btn btn-primary mr-auto" >Simpan</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Kembali</button>
</div>
</div>
</div>
</div>
<div class="modal fade" role="dialog" id="tambah">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<form id="form1">
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Ukuran</label>
<div class="col-sm-10">
<select class="form-control select2" style="width: 100%;" id="ukuran_id">
<option selected="true" disabled="disabled">Pilih Ukuran</option>
<?php foreach ($ukuran as $ukuran) { ?>
<option value="<?php echo $ukuran['id'];?>"><?php echo $ukuran['size'];?></option>
<?php } ?>
</select>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="button-tambah" class="btn btn-primary mr-auto" >Tambah</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Kembali</button>
</div>
</div>
Thank You :)
foreach ($ukuran as $ukuran)
You can not use same variable. Do something like follows
foreach ($ukuran as $value){
//now you can
echo $value['id'];
}
I have this form that lets you choose or add an option to a dropdown field, the problem is when you click the add option, it calls down the modal but it also submits the whole form, but i put a required tag on an input field so it doesn't really pass the form unless completed
Here's the form:
<div class="col-lg-12">
<form action="<?php echo base_url(); ?>products/purchase" role="form" accept-charset="utf-8" method="post">
<div class="form-group">
<div class="col-lg-12">
<label class="control-label">Name</label>
<input type="text" name="name" class="form-control inline" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-10">
<label class="control-label">Class</label>
<div class="input-group">
<select name="class_ID" class="form-control" required>
<option value="">Select Class</option>
<?php if(!empty($cls)){
if (is_array($cls)){
foreach ($cls as $row) {?>
<option value="<?php echo $row['class_id']?>"><?php echo $row['class_Name']; ?></option>
<?php }
}
}
else{ ?>
<option value=""></option>
<?php }?>
</select>
<span class="input-group-btn">
<button class="btn btn-theme" data-toggle="modal" data-target="#addClass"><i class="fa fa-plus"></i></button>
</span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-12 pull-right">
<input type="submit" class="btn btn-success pull-right" value="Order" style="margin-left:5px;">
</form>
Back
</div>
</div>
</div>
And the Modal:
<div class="modal fade" id="addClass" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="vertical-alignment-helper">
<div class="modal-dialog vertical-align-center">
<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">Add New Product Classification</h4>
</div>
<div class="modal-body">
<form action="<?php echo base_url(); ?>products/add_class" method="post" accept-charset="utf-8" role="form">
<div class="form-group">
<div class="col-lg-6">
<label for="class_Name">Classification Name</label>
<input type="text" name="class_Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-lg-3">
<label for="is_active">Enabled</label>
<select name="is_active" class="form-control">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-success" value="ADD">
</form>
</div>
</div>
</div>
</div>
</div>
Possibly due to using class btn-success twice, You hit it when opening the modal and its triggered at the bottom of the model footer
balance your html tags
i.e. for every opened tag, there must exist a closed tag and in the same order too.
in your modal, youre opening your form tag inside a div that is closed before the form tag is closed. some browsers may automatically close this tag, and therefore the click on your submit button will propagate to the main form.
change to
<div>
<form>
</form>
</div>
open your form before .modal-dialog
change
<button> to <a> or you can tell <button type="button"> that it's button and should only do what button supposed to do which is not to submit the form
w3schools.com/tags/att_button_type.asp
– Shehary