i have a problem. I have some php script that create list of post in my database.
this is the code
<td><?php echo "$r[date_create]"; ?></td>
<td><?php echo "$r[date_update]"; ?></td>
<td><?php echo "$r[hitcount]"; ?></td>
<td>
<i class="icon-pencil"></i>
<a href="#myModal" role="button" data-toggle="modal" id="<?php $r[id]; ?>">
<i class="icon-remove"></i></a>
</td>
This syntax is calling a modal called MyModal
as you can see, the anchor (
<a href="#myModal"
) getting some id based on database
But i can't by pass the value generated from the php script to my modal window
here's the modal window syntax
<div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Delete Confirmation</h3>
</div>
<div class="modal-body">
<p class="error-text">Are you sure you want to delete the post?</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
<a class="btn btn-danger" data-dismiss="modal" href="delete.php?id=THE ID SHOULD BE HERE ">Delete</a>
</div>
</div>
I want to the ID from anchor above is place on the delete.php?id=THE ID SHOULD BE HERE
how to do that?
thank you in advance
You will have to manually open the modal box, instead of relying on the data attributes to do the job. A pseudo-code could be like this:
$(function() {
// Attach a click handler to your link
$('#modal_opener').click(function() {
// Open the modal box
$('#myModal').modal('show');
// Set the id to the hidden field
$('#id_on_your_modal').val("<?php print $r[id] ?>");
});
});
Remember that the javascript code must be in the same page of your html code, so you can have access to your php variables.
Related
I have a problem with a bootstrap modal. I'm using PHP.
In an admin panel there is a table with the list of the users and the possibility to edit or delete a user's profile. For the delete I want to create a modal for the confirm of the delete but, when I click on "confirm" button inside the modal, the modal gets by default the user ID of the first user in the table and not the user ID of the selected user.
Here is the code:
<?php foreach ($utenti as $utente) { ?>
<tr>
<th scope="row"> <?php echo $utente['idUser']?> </th>
<td><?php echo $utente['nome']." ".$utente['cognome']?></td>
<?php if($_SESSION['role'] == 1) {?>
<td><?php echo $utente['az']?></td>
<?php } ?>
<td><?php echo $utente['email']?></td>
<td class="text-warning"><a
href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
class="fas fa-edit text-warning"></i></a></td>
<!-- <td class="text-warning"><i class="fas fa-trash-alt text-danger"></i></td> -->
<td class="text-danger">
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete"><i
class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
</button>
</td>
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Attenzione <?php var_dump($utente['idUser']); ?></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Continuando eliminerai l'utente in maniera irreversibile</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger "
><a
class="text-white btn-modal-confirm"
href="<?php echo '?action=delete&user='.$utente['idUser']?>"
>Elimina</a>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Indietro
</button>
</div>
</div>
</div>
</div>
</tr>
<?php }?>
If I make a var_dump of $utente['idUser'] before the modal, it gets the right user ID. If I make it inside the modal it gets by default the first ID, as I said.
Notice that every modal trigger button has a data-target attribute to define which modal will be opened.
In your case, the button of every row you used to triggered the modal have the same data-target, which is #confirmDelete. These modals behind also has the same id called #confirmDelete, so every time you hit the modal trigger button (all had the same data-target) then eventually it will shows up the very first modal element.
For a better understanding, compare my code to yours and see the differences.
<?php foreach ($utenti as $utente) { ?>
<tr>
<th scope="row"> <?php echo $utente['idUser']?> </th>
<td><?php echo $utente['nome']." ".$utente['cognome']?></td>
<?php if($_SESSION['role'] == 1) {?>
<td><?php echo $utente['az']?></td>
<?php } ?>
<td><?php echo $utente['email']?></td>
<td class="text-warning"><a
href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
class="fas fa-edit text-warning"></i></a></td>
<!-- <td class="text-warning"><i class="fas fa-trash-alt text-danger"></i></td> -->
<td class="text-danger">
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete_<?php echo $utente['idUser']; ?>"><i
class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
</button>
</td>
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Attenzione <?php echo $utente['idUser']; ?></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Continuando eliminerai l'utente in maniera irreversibile</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger "
><a
class="text-white btn-modal-confirm"
href="<?php echo '?action=delete&user='.$utente['idUser']?>"
>Elimina</a>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Indietro
</button>
</div>
</div>
</div>
</div>
</tr>
<?php }?>
In the above code, I gave every pair of modal elements (modal trigger button and modal ID) a unique data-target value and a unique element id.
...
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete_<?php echo $utente['idUser']; ?>">
...
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
...
Now each pair of modal elements have their own ids and they should be working the way you wanted.
I am looping a dynamic PHP value and also a button that is linked with a modal form.
The problem is that im currently setting the data-target to one of the dynamic values being looped but it never gets to work.
This is the PHP+HTML code being looped:
while($row = mysqli_fetch_assoc($seleccionar_requerimientos)){
$req_id = $row['req_id'];
?>
<tr>
<td><?php echo "{$req_id}" ?></td>
<!-- Here we place the data-target to dynamic value -->
<td><p data-placement="top" data-toggle="tooltip" title="Editar"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" **data-target="#<?php echo $req_id; ?>" ><span class="fas fa-edit"></span></button></p></td>
<!-- Here we set the dynamic ID linked to previous trigger -->
<div class="modal fade" id="<?php echo $req_id; ?>" tabindex="-1" role="dialog" aria-labelledby="edit" 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"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">Editar Requerimiento</h4>
</div>
<div class="modal-body">
<div class="form-group">
<textarea rows="2" class="form-control" placeholder="<?php echo $req_desc_corta; ?>"></textarea>
</div>
</div>
<div class="modal-footer ">
<button type="button" class="btn btn-warning btn-lg" style="width: 100%;"><span class="fas fa-wrench"></span> Actualizar</button>
</div>
</div>
</div>
</div>
<?php } ?>
Where am I wrong? I can see on the site source code that both values are currently properly set but modal still not showing.
If values on both data-target and ID are dynamic, they are properly displayed on site source code but the modal never triggers.
If values are manually writen, the modal pops up correctly which confirms modal code is working, however since its not a dynamic value it opens up the same form for every looped item, which is the reason to write a dynamic value on them.
This is the HTML output I get on the source code whiledisplaying dynamic values:
<!-- Echo en bucle de los botones de editar el requerimiento -->
<td><p data-placement="top" data-toggle="tooltip" title="Editar"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#9" ><span class="fas fa-edit"></span></button></p></td>
<!-- Pop Up con el form de editar el requerimiento -->
<div class="modal fade" id="9" tabindex="-1" role="dialog" aria-labelledby="edit" 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"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">Editar Requerimiento</h4>
</div>
<div class="modal-body">
<div class="form-group">
<textarea rows="2" class="form-control" placeholder="dsfgdfg"></textarea>
</div>
</div>
<div class="modal-footer ">
<button type="button" class="btn btn-warning btn-lg" style="width: 100%;"><span class="fas fa-wrench"></span> Actualizar</button>
</div>
</div>
</div>
</div>
Weirdly, if I change
data-target="#<?php echo $req_id; ?>"
To
data-target="#9"
And I also change
<div class="modal fade" id="<?php echo $req_id; ?>" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
To
<div class="modal fade" id="9" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
Then it works (with static values)
I think using only numbers for the id attribute is a bad idea. Please, use some string before the number. From https://www.w3schools.com/tags/att_global_id.asp :
Must contain at least one character
Is it possible that your code is generating duplicate values for $req_id?
If the code works when you manually put the IDs in place then the problem is likely the Ids and there aren't many ways this could be wrong. Either there are duplicate IDs or there's some other issue with the way they are being output. Can you add the source code of the whole page? Does you console warn you about duplicated IDs?
I have a Modal Window. It is working good with javascript. I tried something myself but I can't show any information of Customer in Modal Window. When clicking on the info button, I want to show the Customer's information. How can I show any customer information in Modal Window?
Controller:
public function index()
{
$viewData['countries'] = $this->db->get("country")->row();
$viewData['customers'] = $this->customer_model->get_all();
$this->lang->load('content', $this->session->userdata('people_lang'));
$this->load->view('customers', $viewData);
}
public function getInfo(){
$cusId = $this->input->post('cusId');
$viewData['customers'] = $this->db->where("cusId", $cusId)->get("customer")->row();
$this->load->view('customers/getInfo', $viewData);
}
This Ajax Code:
<script type="text/javascript">
function showCustomers(str){
$.ajax({
type: "POST",
data: { cusId: str },
url: "<?=base_url('customers/getInfo');?>",
success: function(data) {
$('#divInfo').html(data);
}
});
}
</script>
This Modal:
<!-- Modal -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal Tittle</h4>
</div>
<div class="modal-body">
<div id="divInfo"></div>
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
<button class="btn btn-warning" type="button"> Confirm</button>
</div>
</div>
</div>
</div>
<!-- modal -->
This View:
<a data-toggle="modal" href="#myModal3"><button class="btn btn-primary btn-xs" onclick="showCustomers(this.id);" id="<?php echo $customers->cusId; ?>"><i class="fa fa-eye"></i></button>
But This is not working. Where is I do mistake?
I think you can do this by adding a couple of lines.
Note* Code is not tested, i can practically painting the idea to solve this.
In the index function of your controller
public function index(){
$viewData['countries'] = $this->db->get("country")->row();
$viewData['customers'] = $this->customer_model->get_all();
//**new line
// Assuming you already have method in your Model to get customer by id
// Check if there's a GET request of 'customerID' then assign to the $viewDataByID array the data from the model, else nothing.
$viewDataByID['customersById'] = (isset($_GET['cusomterID'])) ? $this->customer_model->get_by_id($id) : '';
//Then you'll have to update the variable sent to the view by storing both in one array
$allData['viewData']= $viewData;
$allData['viewDataByID'] = $viewDataByID;
$this->lang->load('content', $this->session->userdata('people_lang'));
$this->load->view('customers', $allData);// $allData get sent to the view
}
In your modal you can sent the request
Please update the variables in the view. You can do a print_r($allData) or var_dump($allData) to clearly see the data tree and how you can make use of them.
Alternatively if you do not want to use your index for this, you can create another function within your controller to handle the customer by id, json_encode the result and then use jQuery to get the data.
Hope this helps.
Here's one way you could do it:
<!--modify button to include an onclick element, pass a value to it-->
<button class="btn btn-primary btn-xs" id="info_modal" onclick="show_modal('<?=$customer_name;?>')"><i class="fa fa-eye"></i></button>
You could modify your modal to have some id's in anticipation of your data:
<div class="modal fade" id="info_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
....
<div class="modal-body">
View File List According to Customer ID.
<div id="customer_name"></div>
</div>
Then in your script (if using jquery):
//add passed string to the modal and then display it
function show_modal(customer_name){
$("#customer_name").html(customer_name);
$("#info_modal").modal('show');
}
I answered myself this question.
<td data-title="Edit" align="center"><button class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i></button> <a data-toggle="modal" href="#<?php echo $customer->cusId; ?>"><button class="btn btn-primary btn-xs"><i class="fa fa-eye"></i></button></a></td>
I edit a href like that href="#<?php echo $customer->cusId; ?>
Afterthat I just write this code on modal top:
<?php foreach ($customers as $get) { ?>
<div class="modal fade" id="<?php echo $get->cusId; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<?php } ?>
I edit id like that id="#<?php echo $get->cusId; ?>
It works better now!
I have a list of database records on my page. Each record have there own edit button. When i click on the edit button a modal shows up and i can do my process there in a form. But i can't figure out how i can get the selected record to my update query.
For now i have 3 records with an project name. Lets say i want to edit the second record in the list, I always get the values from the first record but i want the second record. So i would like to know how i can get the values from a selected record.
<!-- Get all projects in the database -->
<?php $result = mysqli_query($connection, "SELECT project_name, project_completion FROM project");
while ($project = mysqli_fetch_assoc($result)){ ?>
<tr>
<td><span class="glyphicon glyphicon-ok"></span></td>
<td><?php echo $project["project_name"]; ?></td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;"></div>
</div>
</td>
<td><?php echo $project["project_completion"]?>%</td>
<!-- edit button on click to modal -->
<td><a data-toggle="modal" data-target="#myModalEdit" class="btn-sm btn btn-warning"><span class="glyphicon glyphicon-pencil"></span></a>
<!-- remove button -->
<span class="glyphicon glyphicon-remove"></span></td>
<!-- Modal -->
<div class="modal fade" id="myModalEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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"> Edit project</h4>
</div>
<div class="modal-body">
<!-- do process with selected record -->
</div>
</div>
<div class="modal-footer">
<input type="submit" name="saveProject" value="Add" class="btn btn-success"/>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</tr>
<?php } ?><!-- end while -->
Assign different id to every popup.
<?php $i++; ?>
<a data-toggle="modal" data-target="#myModalEdit-<?php echo $i;?>" class="btn-sm btn btn-warning"><span class="glyphicon glyphicon-pencil"></span></a>
Assign $i=0; before while loop start.
Then also change modal popup id like below:
<div class="modal fade" id="myModalEdit-<?php echo $i;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
Update above edit and modal popup line. Then try.
You have to use some type of identification. For example lets say you has the following code for a button
<button type="button" class="btn btn-primary editor" data-target=<?php echo $project['id']; ?>>Edit Something</button>
<div id="returnbuffer" style="display: none;"></div>
Then in javascript later on you have the following code
<script>
$(document).ready(function() {
$('editor').click(function() {
$('#returnbuffer').load('editRecord.php?id=' + $(this).attr('data-target'));
});
});
</script>
Then you just have a php file in the same directory that usses get protocol and changes the database.
Just to explain what is actually happening here, when the button is clicked javascript runs a php file and sends information on which record (with what ID) should be changed.
There are different ways for this to do. like :-
When creating the list of records in your php loop apply a "onclick" event to your edit button which will have records primary key as parameter. When ever edit button is clicked this function will be called and through ajax you can fetch data of this record and display. And at the time of saving the data again call ajax and save the data. In this you have to call two ajax method i.e for saving and for fetching the data.
Or you can use same page with different Post variables. Like when edit button is cliked reload the same page with id appended to it url. Check the url if its appended with any records id variable, if yes then show form to edit, make changes and submit or else show only records.
Try this:
Step 1: In your select query, get auto increment value also (for example - any id)
step 2: Then set one hidden field which contains your increment id
for example:
<input type="hidden" name="test" value="<?php echo your id value?>">
when submit your form you will get which row will be updated
Some changes in your code:
<?php $result = mysqli_query($connection, "SELECT project_id,project_name, project_completion FROM project"); ?>
..........................
..........................
<div class="modal-footer">
<form>
<input type="hidden" name="projectid" value="<?php $project['project_id'] ?>">
<input type="submit" name="saveProject" value="Add" class="btn btn-success"/>
</form>
</div>
I have a list created dinamically using php
while($row = $result->fetch_assoc()){ //$row is a row fetched from my database
<tr>
<td><?php echo $row['id']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['surname']?></td>
<td><a href="#Modal" <?php echo "id= " . $row['username'] ?> class="btn btn-small btn-info" data-toggle="modal" > Vedi</a></td>
</tr>
}
Modal is something like this:
<div id="Modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-header">
<a class="close" data-dismiss="modal" aria-hidden="true">×</a>
<h3 id="myModalLabel">user information</h3>
</div>
<div class="modal-body" id="loadInfo">
<div class="row-fluid">
<div class="span12">
<!-- my php code here -->
</div>
</div>
</div>
</div>
My php code must be referred to a precise user. So I need that the Modal can receive this the id of the in order to execute the proper php code.
How can I obtain this result?
assign the values of the user info to javascript variables. Add a data attribute to the modal toggle buttons like data-user_id=<?php echo $row['id']; ?> and make a jquery click handler to update the modal's content to match the button you clicked.