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!
Related
Heys guys
I am having trouble passing an ID variable to be used in a pop-up window in HTML
this is what I am trying to do
<?php
$sql = "select * from locations";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()){
$locationID = $row['id'];
<button class=\"btn btn-success btn-lg\" data-toggle=\"modal\" data-target=\"#myModal\"> Show Listed Properties </button>
// here i want to pass the locationID variable so that when the user clicks on the
// button a pop up window appears displaying information based on the location id.
}
?>
my pop up window looks like this:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
Hi there, I am a Modal Example for Dashio Admin Panel.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
inside the popup code, I will be performing an SQL query based on the locationID variable and then populate the popup window with the query results.
alternatively, I could just create a new page and pass the id as a get request but I feel like that does not look as professional and is more time-consuming.
could someone point me in the right direction?
add new class in the button and call it open-modal, and use data-id like so:
<button type="button" class="btn btn-info btn-lg open-modal" data-toggle="modal" data-id="<?= $id; ?>" data-target="#myModal">Show Listed Properties</button>
in the end of the file you can call it like:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).on("click", ".open-modal", function () {
var id = $(this).data('id');
alert(id)
/*
proceed with rest of modal using the id variable as necessary
*/
});
</script>
hope I understand your issue well.
I've a problem with my code. I want to pass a PHP variable to bootstrap modal.
Here's my code :
My link to open modal :
<td><a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#modal-user" href="userDetails.php?id_user=<?= $rel['id_user'] ;?>"><?= $rel['id_user'] ;?></a></td>
My modal code :
<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="modal-user" aria-hidden="true" id="modal-user">
<div class="modal-dialog modal-lg">
<?php
include('userDetails.php');
?>
</div>
</div>
And the userDetails.php code :
<?php
require 'connect.php';
if (isset($_GET['id_user']) && !empty($_GET['id_user'])) {
$idUser = $_GET['id_user'];
}
$sql = "SELECT * FROM users WHERE id_user=" .$bdd->quote($idUser);
$query = $bdd->query($sql);
$userDetails = $query->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Infos</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php
foreach($userDetails as $details) {
?>
<p><?= $details['name'] ?></p>
<p><?= $details['firstname'] ?></p>
<?php
}
?>
</div>
</div>
Also, I've this jQuery code :
<script>
$( document ).ready(function() {
$('#modal-user').on('hidden.bs.modal', function () {
$(this).removeData('bs.modal');
});
});
</script>
I've looking other posts and normaly, everything must be ok but I've an error and I still don't know why...
Notice: Undefined variable: idUser in /Applications/MAMP/htdocs/directory/userDetails.php on line 10
Can you help me please ?
Bootstrap modal href is not calling your page when you click. Your page is already loaded when you include userDetails.php. So you can define the get before the include or pass the user id through GET variable.
So:
<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="modal-user" aria-hidden="true" id="modal-user">
<div class="modal-dialog modal-lg">
<?php
$_GET['id_user'] = $rel['id_user'];
include('userDetails.php');
?>
</div>
</div>
In a LOOP
If you have a loop and want to do it dynamically through the jQuery you can do the following:
Remove the bootstrap calls from your link, and add a trigger class modal-btn and make it like:
<a
class="btn btn-primary modal-btn btn-xs"
href="userDetails.php?id_user=<?= $rel['id_user'] ;?>"><?= $rel['id_user'] ;?>
</a>
Update your modal structure and remove the include():
<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="modal-user" aria-hidden="true" id="modal-user">
<div class="modal-dialog modal-lg">
<div class="modal-content"></div>
</div>
</div>
The jQuery should get the href property of the link and call the bootstrap modal function load and also force the modal to show
$(".modal-btn").on('click',function(e){ //trigger when link clicked
e.preventDefault();
$('#modal-user').modal('show'); //force modal to show
$('.modal-content').load( $(this).attr('href')); //load content from link's href
});
So your page userDetails.php will be loaded inside the modal, and you will need to remove the <div class="modal-content">...</div> from it to avoid duplication...
When I was trying to delete one data from the database but it would either delete the whole data in the database; it also the same problem for edit.
this code is for delete a user that will show a prompt when deleting a user:
<div id="delete_user<?php echo $id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-body">
<div class="alert alert-danger">Are you Sure yuo want to Delete this Data?</div>
</div>
<div class="modal-footer">
<a class="btn btn-danger" href="delete_user.php<?php echo '?id='.$id; ?>"><i class="icon-check"></i> Yes</a>
<button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i> Close</button>
</div>
</div>
and this code here below is the actual delete from the database
<?php
include('dbcon.php');
$id=$_GET['id'];
mysql_query("delete from users where user_id='$id'") or die(mysql_error());
header('location:users.php');
?>
First check if value is set in $_GET['id'] then perform your action.
<?php
include('dbcon.php');
if(isset($_GET['id'])){
$id=mysql_real_escape_string($_GET['id']);
mysql_query("delete from users where user_id='$id'") or die(mysql_error());
}
header('location:users.php');
?>
your code seem to be okay, just check the debug the id value.
But still i strongly believe for such action don't pass id's in GET method. Use POST in ajax.
Below is the code with the ajax using POST method to pass the id to delete_user.php page
<div id="delete_user<?php echo $id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-body">
<div class="alert alert-danger">Are you Sure yuo want to Delete this Data?</div>
</div>
<div class="modal-footer">
<a class="btn btn-danger" onclick="deleteUser(<?php echo $id; ?>)" href="javascript:void(0)"> <i class="icon-check"></i> Yes</a>
<button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i> Close</button>
</div>
</div>
Below the jquery ajax part
function deletUser(id) {
$.ajax({
url: 'delete_user.php',
method: 'POST',
data: {"id" : id},
success: function(data) {
// on success alter the div structure.....
}
});
}
below is code for your delete_user.php page
include('dbcon.php');
if(isset($_POST['id']) && $_POST['id'] != ''){
$id = (int)$_POST['id'];
mysql_query("delete from users where user_id=$id") or die(mysql_error());
}
header('location:users.php');
Hi I want to open modal after I click on button. But I want to display different content on the basis on which button I click.
So in html I have:
<h1>article1</h1>
<div class="btnComment">
<button class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg">Comment button 1</button>
</div>
<h1>article2</h1>
<div class="btnComment">
<button class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg">Comment button 2</button>
</div>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" 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-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="gridSystemModalLabel">Comments</h4>
</div>
<div class="modal-body">
<?php
mysql_query("set names 'utf8'");
$selectConf = mysql_query("SELECT * FROM e_comments WHERE article='".$article."'");
$count = 0;
if($selectConf){
while ($row = mysql_fetch_array($selectConf)) {
$text = $row['text'];
echo $text;
}
}
?>
</div>
</div>
</div>
</div>
So how can I know in modal which button was clicked? And how I can pass some variable throw it? I want to show comments of article. So I need to pass id of article and select all comments where is id of article same.
There's a direct example on the bootstrap page at http://getbootstrap.com/javascript/#modals-related-target. Here is the equivalent code for your dialog
<script>
$(document).ready(function () {
$('.bs-example-modal-lg').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
alert(button.html())
})
})
</script>
edit : you'd have to change the alert() part to whatever if else logic you want.
as i am a complete newbie to Zend Framework this might be a total beginner question:
I did some tutorials and now i have a Zend-Framework skeleton-application with ZfcUser (and ZfcBase) up and running.
Everything works fine so far, but what i want to accomplish is that the Login and Registration opens up in a Bootstrap-Modal-Dialog.
So now i see that in ./vendor/ZfcUser/config/module.config.php you can define the routes, but i have no idea what to do, when i want the whole dialogs being "served" with the index of my main-application (i guess i will need this to let the login dialog open up from the main menu from anywhere in the application).
So can someone help me with getting this to work? I really have no idea how to start at all and any help is highly appreciated :)
best regards
If you want to use Bootstrap Model for ZfcUser login, You need to change approach of login a bit.
You should use ZfcUser API Calls to validate login instead of post data on /user/login page.
You need to override the ZfcUser login.html to change form action button from submit to simple button/link binded with ajax request.
Call ZfcUserLoginWidget into Bootstrap Model's body
Form/ajax action set to your custom auth page
Where ZfcUser API Calls validate & return json response with success/failure.
ZfcUser validation no more work automatically, You need to apply through jQuery, Javascript from Json Response.
So First copy vendor/ZfcUser/view/user/login.html to module/[YourModule]/view/zfc-user/user/login.phtml
Now replace submit button with normal button like that :
<input type="button" value="Sign in" onClick="javascript:verifyLogin(this.form);" />
--
--
<script type="text/javascript">
function verifyLogin(frm)
{
var data = $(frm).serialize();
$.ajax({
type: "POST",
url: "<?php echo $this->url('authurl') ?>",
data: data,
success: function(resp){
alert(resp.status);
},
error: function(resp){
},
dataType: 'json'
});
}
</script>
You should add a route for authurl for YourController/authAction
Add your html for Bootstrap model on parent template view :
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Login Box
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" 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">Modal title</h4>
</div>
<div class="modal-body">
<?php echo $this->zfcUserLoginWidget(); ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Now YourController/authAction code should work like that :
$request = $this->getRequest();
$data = $request->getPost();
$this->getRequest()->getPost()->set('identity', $data['identity']);
$this->getRequest()->getPost()->set('credential', $data['credential']);
$this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
$this->zfcUserAuthentication()->getAuthService()->clearIdentity();
$adapter = $this->zfcUserAuthentication()->getAuthAdapter();
$adapter->prepareForAuthentication($this->getRequest());
$auth = $this->zfcUserAuthentication()->getAuthService()->authenticate($adapter);
if (!$auth->isValid()) {
//$this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage($this->failedLoginMessage);
$adapter->resetAdapters();
$response_data = array(
'status' => 'Failure'
) ;
}
else
{
$response_data = array(
'status' => 'OK'
) ;
}
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent(json_encode($response_data));
return $response;
The Login Widget viewhelper is probably what you are looking for. Within your desired .phtml template just add the viewhelper to render the login form.
<!-- Modal -->
<div class="modal fade" id="myModal" 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">Modal title</h4>
</div>
<div class="modal-body">
<?php //Add the Widget to the Modal Body here!?>
<?php echo $this->zfcUserLoginWidget(); ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
If you want to alter the template of the widget you'll need to edit/add the zfcuser.global.php in your ../config/autoload/ folder and change/uncomment this the following to your desired login-widget template.
'user_login_widget_view_template' => 'your_module/whatever/login.phtml',