i have a project in php codeigniter where data is fetched from the DB, and with a foreach loop it creates rows in a table, each row has an edit button that opens the fancybox modal, in that modal i have 2 input fields, first name and last name that i try to populate them with the current values from the database, in the value section i tried value="<?php echo $row->first_name ?>, the problem is that all the rows get the same value, the modal does not get the correct data for each row.
Relevant code:
<a data-fancybox data-touch="false" href="#modal" class="btn btn-secondary" style=" margin-bottom:5px;" id="<?php echo $row->id; ?>" type="button" name="button">Edit</a>
<div id="modal">
<div class="content">
<div class="container-fluid">
<div class="row">
<form method="post" action="<?php echo base_url()?>index.php/welcome/formValidation">
<div class="form-row">
<div class="col-12 mb-3">
<h3 class="mb-3">Modifica Rapid:</h3>
<label for="nume">Nume:</label>
<input type="text" class="form-control" id="nume" placeholder="Nume" name="nume" value="<?php echo $row->last_name ?>"required>
</div>
<div class="col-12 mb-3">
<label for="prenume">Prenume</label>
<input type="text" class="form-control" id="prenume" placeholder="Prenume" name="prenume" value="<?php echo $row->first_name ?>" required>
</div>
<div class="col-12 mb-3 text-center">
<button style="width:150px;height:50px;" class="btn btn-warning" type="submit">Modifica</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
What you need to do is, attach the $row id to the modal css id so each modal is unique.
<a data-fancybox data-touch="false" href="#modal<?=$row->id;?>" class="btn btn-secondary" style=" margin-bottom:5px;" id="<?php echo $row->id; ?>" type="button" name="button">Edit</a>
<div id="modal<?=$row->id;?>">
<div class="content">
<div class="container-fluid">
<div class="row">
<form method="post" action="<?php echo base_url()?>index.php/welcome/formValidation">
<div class="form-row">
<div class="col-12 mb-3">
<h3 class="mb-3">Modifica Rapid:</h3>
<label for="nume">Nume:</label>
<input type="text" class="form-control" id="nume" placeholder="Nume" name="nume" value="<?php echo $row->last_name ?>"required>
</div>
<div class="col-12 mb-3">
<label for="prenume">Prenume</label>
<input type="text" class="form-control" id="prenume" placeholder="Prenume" name="prenume" value="<?php echo $row->first_name ?>" required>
</div>
<div class="col-12 mb-3 text-center">
<button style="width:150px;height:50px;" class="btn btn-warning" type="submit">Modifica</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
So change
<a data-fancybox data-touch="false" href="#modal" class="btn btn-secondary" style=" margin-bottom:5px;" id="<?php echo $row->id; ?>" type="button" name="button">Edit</a>
to
<a data-fancybox data-touch="false" href="#modal<?=$row->id;?>" class="btn btn-secondary" style=" margin-bottom:5px;" id="<?php echo $row->id; ?>" type="button" name="button">Edit</a>
by adding the row id to the css to get unique modal id if not all link will call only the last modal.
The same change too for the modal change
<div id="modal">
to
<div id="modal<?=$row->id;?>">
One of the workaround is you could assign data attributes containing the last_name and first_name data, and add an edit-btn class (as the trigger) to the edit button link :
<a data-fancybox data-touch="false" href="#modal" class="btn btn-secondary edit-btn" data-lastName="<?php echo $row->last_name ?>" data-firstName="<?php echo $row->first_name ?>" style=" margin-bottom:5px;" id="<?php echo $row->id; ?>" type="button" name="button">Edit</a>
<div id="modal">
<div class="content">
<div class="container-fluid">
<div class="row">
<form method="post" action="<?php echo base_url()?>index.php/welcome/formValidation">
<div class="form-row">
<div class="col-12 mb-3">
<h3 class="mb-3">Modifica Rapid:</h3>
<label for="nume">Nume:</label>
<input type="text" class="form-control" id="nume" placeholder="Nume" name="nume" required>
</div>
<div class="col-12 mb-3">
<label for="prenume">Prenume</label>
<input type="text" class="form-control" id="prenume" placeholder="Prenume" name="prenume" required>
</div>
<div class="col-12 mb-3 text-center">
<button style="width:150px;height:50px;" class="btn btn-warning" type="submit">Modifica</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Then set the modal input value using each of the data value :
$(document).on("click", ".edit-btn", function () {
let lastname = $(this).attr("data-lastname");
let firstname = $(this).attr("data-firstname");
$('#nume').val(lastname); // set the lastname input inside the modal using the data attribute
$('#prenume').val(firstname); // set the firstname input inside the modal using the data attribute
});
Related
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>
Without Modal, user_timeout.php does not have a problem but When I use modal, as you can see at the picture below the problem is. But they have same logic codes so I dont know what the problem is. I want to use modal for some quality design so help me guys thank you
logs.php
<a rel="tooltip" title="Timeout" id="<?php echo $idlogs; ?>" href="user_timeout.php<?php echo '?id='.$idlogs; ?>" class="btn btn-info"><i class="icon-minus icon-large"></i> Manual Time-Out</a>
And for Modal
<a rel="tooltip" title="timeout" id="<?php echo $idlogs; ?>" href="#timeout<?php echo $idlogs; ?>" data-toggle="modal" class="btn btn-success"><i class="icon-pencil icon-large"></i> Manual Time-Out</a>
<?php include('modal_time_user.php'); ?>
user_timeout.php
<?php $get_id= $_GET['id']; ?>
<div class="container">
<div class="margin-top">
<div class="row">
<div class="span12">
<?php
$query=mysqli_query($dbcon,"select * from user_logs_tbl where id ='$get_id'")or die(mysqli_error());
$row=mysqli_fetch_assoc($query);
$iduser=$row['user_id'];
$idlogs=$row['id'];
?>
<div class="alert alert-info"><i class="icon-pencil></i> Manual Time-out</div>
<p><a class="btn btn-info" href="logs.php"><i class="icon-arrow-left icon-large"></i> Back</a></p>
<div class="addstudent">
<div class="details">Please Enter Details Below</div>
<form class="form-horizontal" method="POST" enctype="multipart/form-data">
<div class="control-group">
<br>
<div class="control-group">
<label class="control-label" style="margin-left:10%;" for="inputEmail">Time-Out:</label>
<div class="controls">
<input type="time" id="inputEmail" name="timeout" placeholder="Time">
</div>
</div>
<br>
<div class="control-group">
<div class="controls">
<button name="timeouts" type="submit" style="margin-left:16%;" class="btn btn-success"><i class="icon-save icon-large"></i> Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php
if (isset($_POST['timeouts'])){
$timeout=$_POST['timeout'];
$timeout = date('Y-m-d H:i:s', strtotime(str_replace('-', '/',
$timeout)));
mysqli_query($dbcon,"UPDATE user_logs_tbl set time_out = '$timeout' where id='$idlogs'")or die(mysqli_error($dbcon));
$yourURL="logs.php";
echo ("<script>location.href='$yourURL'</script>");
}
?>
But If I change it into modal form, it's working but all data will change if I change one
modal_time_user.php
<div id="timeout<?php echo $idlogs; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-body">
<div class="alert alert-info"><strong>Time</strong></div>
<form class="form-horizontal" method="post">
<div class="control-group">
<label class="control-label" for="inputEmail">Time-Out :</label>
<div class="controls">
<input type="time" id="inputEmail" name="time_out" pattern="[a-zA-Z0-9]{5,}" required>
</div>
</div>
<div class="control-group">
<div class="controls">
<button name="enter" type="submit" class="btn btn-success"><i class="icon-save icon-large"></i> Update</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i> Close</button>
</div>
</div>
<?php
if (isset($_POST['enter'])){
$timeout=$_POST['time_out'];
$timeout = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $timeout)));
mysqli_query($dbcon,"UPDATE user_logs_tbl SET time_out='$timeout' where id = '$idlogs'")or die(mysqli_error($dbcon));
$yourURL="logs.php";
echo ("<script>location.href='$yourURL'</script>");
}
?>
Problem
After Inserting a Time Out In USER ID 45
Firstable, hello everyone. I have issue about the $_POST method, when I write this code block;
if (isset($_POST['menuduzenle'])) {
$menu_id=$_POST['menu_id']; //It didn't turn with value in mysql
$ayarguncelle=mysql_query("update menuler set menu_ad='".$_POST['menu_ad']."',menu_link='".$_POST['menu_link']."'where menu_id='$menu_id'");
if(mysql_affected_rows())
{
header("Location:../menuleriduzenle.php?guncelleme=basarili&menu_id=$menu_id");
}else{
header("Location:../menuleriduzenle.php?guncelleme=basarisiz&menu_id=$menu_id");}
}
menu_id turns emty, what I need to do for solve this?
and here it's html code
form
<form action="network/islem.php" method="POST">
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Adı: </label>
<input class="form-control" name="menu_ad" value="<?php echo $menucek['menu_ad']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Link: </label>
<input class="form-control" name="menu_link" value="<?php echo $menucek['menu_link']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-6">
<input class="btn btn-primary col-md-3" type="submit" name="menuduzenle" value="Edit Menu">
</div>
</form>
and
<td><button class="btn btn-primary col-md-12">Edit</button></td>
You not send any one input with name 'menu_id' on your from.
<form action="network/islem.php" method="POST">
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Adı: </label>
<input class="form-control" name="menu_ad" value="<?php echo $menucek['menu_ad']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Link: </label>
<input class="form-control" name="menu_link" value="<?php echo $menucek['menu_link']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-6">
<input class="btn btn-primary col-md-3" type="submit" name="menuduzenle" value="Edit Menu">
</div>
<input type="hidden" name="menu_id" value="<?php echo $_GET['menu_id'] ?>" />
</form>
You input name "menu_ad" would be "menu_id" or fault other input with name "menu_id" on you sample.
<div class="modal fade validate" id="modal-6">
<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">Edit Advertisement</h4>
<label class="idhere"><?php echo $id; ?></label>
</div>
<?php $sel = $d->select('advertisements', 'ad_id=12');
while ($result = mysqli_fetch_assoc($sel)) {
?>
<div class="modal-body">
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Image Upload</label>
<div>
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 200px; height: 150px;" data-trigger="fileinput">
<img src="<?php echo $result['photo']; ?>">
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px"></div>
<div>
<span class="btn btn-white btn-file">
<span class="fileinput-new">Select image</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="advertise_photo" accept="image/*">
</span>
Remove
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info">Send</button>
<button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
</div>
<?php } ?>
</div>
</div>
</div>
I am unable to set the value of input type=file from database,
and if the image is exits then change button appear instead of select image.
when i am choosing new file it works properly,and change and remove button appear instead of select file.
Please help me to solve it.
Note: Above question is continuous of this question so the following answer is also continuous of this answer
You can skip the <label class="idhere"><?php echo $id;?></label>
in JS what yo need is ajax call method to fetch data from database against <?php $id;?> and display in modal
$(document).ready(function() {
$('#modal-6').on('shown.bs.modal', function(e) {
var id = $(e.relatedTarget).data('id');
$.ajax({
type : 'post',
url : 'file.php', //Here you will fetch records or images
data : 'id='+ id, //Pass id
success : function(data){
$('#fetched-data').html(data);//Show fetched data from database
}
});
});
});
Modal HTML will be
<div class="modal fade validate" id="modal-6">
<div class="modal-dialog">
<div class="modal-content">
<div id="fetched-data"></div>
</div>
</div>
</div>
and file.php will be
<?php
//database connection
if($_POST['id']) {
$id = $_POST['id']; //escape the string
//run query
//fetch data from database
//poplutae the HTML with values
?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Edit Advertisement</h4>
</div>
<?php $sel = $d->select('advertisements','ad_id=12');
while($result = mysqli_fetch_assoc($sel)){
?>
<div class="modal-body">
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Title</label>
<input class="form-control" value="<?php echo $result['title'];?>" name="advertise_title" data-validate="required" data-message-required="Please Enter Title" placeholder="Enter Title" type="text">
</div>
<div class="form-group col-md-6">
<label class="control-label">URL</label>
<input class="form-control" name="advertise_url" value="<?php echo $result['url'];?>" data-validate="required" data-message-required="Please Enter URL" placeholder="Enter URL" type="text">
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group no-margin">
<label for="description" class="control-label">Description</label>
<textarea class="form-control autogrow" id="description" name="advertise_desc" placeholder="Describe Description Regarding Query">
<?php echo trim($result['description']); ?>
</textarea>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Image Upload</label>
<div>
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 200px; height: 150px;" data-trigger="fileinput">
<img src="<?php echo $result['photo']; ?>">
<!-- <img src="images/thumbnail.jpeg" alt="..."> -->
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px"></div>
<div>
<span class="btn btn-white btn-file">
<span class="fileinput-new">Select image</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="advertise_photo" accept="image/*" >
</span>
Remove
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Active Flag</label>
<input type="checkbox">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info">Send</button>
<button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
</div>
<?php } } ?>
As suggested by #devpro you need <form></form> to edit / update the populated data inside modal.
I have a list of product on mypage each product is having a enquiry button on it.
By clicking enquiry button bootstrap pop up opens.
I want that form to work for every product with product id of each product
I have achieved this by putting bootstrap pop up model in loop
my Question is. Instead of repeation blok of bootstrap pop up model , instead of keeping popup html in loop,is it possible to achieve this with function and keeping popup html outside loop ?
<?php if(isset($ser_gal)){
$gal_count =1;
foreach($ser_gal as $s){?>
<div class="element">
<div class="tz-inner" >
<div class="tz-image-item"> <img alt="" src="<?php echo URL;?>gallery/thumbs/thumb_<?php echo $s->IMAGE_PATH ; ?>"> </div>
<h6><?php echo $s->interior_cat;?></h6>
<hr />
<div class="cl"></div>
<?php echo $s->title;?>
<div align="center" >Enquiry</div>
</div>
</div>
<!--form-->
<div class="bs-example">
<div id="myModal<?php echo $gal_count;?>" class="modal fade">
<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">Enquiry</h4>
</div>
<div class="modal-body">
<form role="form">
<div class="form-group">
<label for="recipient-name" class="control-label">Name</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Email Id</label>
<input type="text" class="form-control texttrans_none" id="recipient-name">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Mobile No</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Product</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="control-label">Message</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!---->
<?php $gal_count++;} }?>