I have a page that have a button like this,
When this button is clicked, a pop out modal will show,
Once the submit button is clicked, a small note will show up,
Below are my codes,
<div class="col-md-12" role="main">
<div class="bs-docs-section">
<h1 class="page-header">Today's Activity <button class="btn btn-primary">Add new</button></h1>
</div>
<?php
$con = getDbConnect();
$day = date("l");
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM timetableschedule WHERE staffID= '" . $staff_information["staff_Id"] . "' AND day='" . $day . "' ");
while ($schedule = mysqli_fetch_array($result)) {
?>
<div class="col-md-3">
<button class="btn btn-warning" data-toggle="modal" data-target="#myModal" schedule="
<?php
echo "<br/>";
echo $schedule['academicInstitution'] . "<br />";
echo $schedule['startTime'] . "-" . $schedule['endTime'] . "hrs<br />";
echo "<br/>";
?>">
<?php
echo "<br/>";
echo $schedule['academicInstitution'] . "<br />";
echo $schedule['startTime'] . "-" . $schedule['endTime'] . "hrs<br />";
echo "<br/>";
?>
</button>
</div>
<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">Insert Today's Activity</h4>
</div>
<div class="modal-body">
<p class="activity"></p>
<p>Click on the submit button if the above infomation is correct.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button class="btn btn-primary btn-ok SUB" >Submit</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$('#myModal').on('show.bs.modal', function (e) {
$(this).find('.btn-ok').attr('schedule', $(e.relatedTarget).attr('schedule'));
$('.activity').html('You have selected: <strong>' + $(this).find('.btn-ok').attr('schedule') + '</strong>');
});
$(document).ready(function(){
$(".SUB").click(clickSUB);
})
function clickSUB(){
$("#processSUB").load("hello.php");
}
</script>
<div id="processSUB"></div>
<?php
}
mysqli_close($con);
}
?>
</div>
My codes for the hello.php,
<?php
echo 'You have submitted';
echo $schedule['timetableId'];
?>
Is there a way to get the 'timetableId' from the button?
Check 2nd Parameter of load() to pass data as parameter, you can pass timetableId or any other parameter required.
load('hello.php', { timetableId : javascriptVarWithValue });
you will receive passed data in $_POST in hello.php
In hello.php you can access like:
<?php echo $_POST['timetableId']; ?>
In loop, load the timetableId something like:
<div id="tableID" data-timetable-id="<?php $schedule['timetableId']; ?>" style="display: none;"></div>
In clickSUB() fetch the value something like this:
function clickSUB(){
// changed to data('timetableId') instead of data('timeTableId')
var timeTableId = $('#tableID').data('timetableId');
console.log(timeTableId);
$("#processSUB").load("hello.php", { timetableID : timeTableId });
}
Related
I'm trying to create a simple buddy list for a web application. I'm looping through an SQL table to retrieve the current user's buddy list. Each row in the table has a button to display a modal of the selected user's ID. The current issue I'm having is that it's only showing the modal for the last user in the list, for everyone else it doesn't pop up. I need the modal to pop up for each user I click on and display that user's ID.
Example:
Here is the code I have so far:
HTML/PHP:
<?php
// Full SQL retrieval omitted above
$buddyUid = '';
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$buddyUid = $row['buddy_uid'];
echo "<tr>";
echo "<td> <input type='checkbox'></td>";
echo "<td>" . $buddyUid . "</td>";
echo "<td> <button type='button' onclick='javascript:selectBuddy($(this)); ' data-bs-toggle='modal' data-bs-target='#modal-" . $buddyUid . "'><i class='fas fa-share-square'></i></button></td>";
echo "</tr>";
}
?>
<!-- Modal -->
<div class="modal fade" id="modal-<?=$buddyUid?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<?= $buddyUid?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Javascript:
function selectBuddy(buddy) {
$('#modal-' + buddy).modal('show');
}
$(this) refers to the element in the DOM to which the onclick attribute belongs:
in your case to get only buddy_id just change onclick event
from this
...onclick='javascript:selectBuddy($(this));'...
to this
...onclick='javascript:selectBuddy(".$buddyUid.");'...
also change your Modal id from
<div class="modal fade" id="modal-<?=$buddyUid?>" ...
to
<div class="modal fade" id="modal-buddy" ....
then javascript code like this
function selectBuddy(buddy) {
$('#modal-buddy').find('.modal-body').html(buddy);
$('#modal-buddy').modal('show');
}
I'm very noob in PHP, but i'm really stuck here trying to figure out how to delete a row printed in a while loop here :
<?php
$sql5 = "SELECT * FROM user_exp WHERE id=".$_SESSION["ID"]."";
$result3 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result3) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result3)) {
echo "<h4>" . $row["exp_title"]. "<a href='#' title=''><i class='fa fa-pencil'></i><i onclick='location.href='userprofile.php?deleteID=".$row["auto_id"]."';' class='fa fa-minus-square' data-toggle='modal' data-target='#EXPDELModal' value='delete_exp'></i></a></h4>";
echo "<p>" . $row["exp_detail"]. "</p>";
}
}
else
{
echo "<div class='textfaded2'>Add you experience here.</div>";
}
?>
Note: i want to delete the row using a font-awesome icon and i'm using
'auto_id' as the auto_increment, primary key to define the row i want
to delete.
Here is the code for the modal:
<div class="modal fade" id="EXPDELModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Experience</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
<br>
<br>
</div>
<div class="modal-footer">
<form action="userprofile.php?deleteID=<?php '.$row["auto_id"].';?>"
style="width: 100%;" method="post" value="delete_exp">
<button type="button" name="delete" class="btn btn-default" data-
dismiss="modal">YES</button>
<button type="button" class="btn btn-default" data-
dismiss="modal">NO</button>
</form>
</div>
</div>
</div>
</div>
And finally the query for delete:
<?php
if(isset($_POST['delete']))
{
$sql6="DELETE FROM `user_exp` WHERE auto_id=".$_GET['deleteID']."";
$result=mysqli_query($conn,$sql6) or die();
}
?>
I would like to thank you for taking the time to read this.
<?php
$sql5 = "SELECT * FROM user_exp WHERE id=" . $_SESSION["ID"] . "";
$result3 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result3) > 0) {
while ($row = mysqli_fetch_assoc($result3)) {
?>
<h4><?php echo $row["exp_title"]; ?>
<a href='#' title=''>
<i class='fa fa-pencil'></i>
<i onclick='setValue("<?php $row["auto_id"]; ?>");' class='fa fa-minus-square' data-toggle='modal'
data-target='#EXPDELModal'></i>
</a>
</h4>
<p><?php echo $row["exp_detail"]; ?></p>;
<?php
}
} else {
echo "<div class='textfaded2'>Add you experience here.</div>";
}
if (isset($_POST['delete'])) {
if($_POST['deleteID']) {
$sql6 = "DELETE FROM `user_exp` WHERE auto_id=" . $_POST['deleteID'] . "";
$result = mysqli_query($conn, $sql6) or die();
}
}
?>
<script>
function setValue(value) {
document.getElementById("deleteId").value = value;
}
</script>
<div class="modal fade" id="EXPDELModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Experience</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
<br>
<br>
</div>
<div class="modal-footer">
<form action="userprofile.php" style="width: 100%;" method="post" name="delete_exp">
<input type="hidden" value="0" name="deleteID" id="deleteId" readonly/>
<button type="submit" name="delete" class="btn btn-default" data-dismiss="modal">YES</button>
<button type="button" class="btn btn-default" data-dismiss="modal">NO</button>
</form>
</div>
</div>
</div>
</div>
Im trying to make the modal show in the condition is met but the problem is no modal is showing, can someone help me about this? The php code is below of the modal code.
here's the php
<?php
session_start();
include_once("connection.php");
if (isset($_POST['login']))
{
$stud_no = $_POST["stud_no"];
$password = $_POST["pword"];
$name = mysqli_real_escape_string($con, $stud_no);
$password = mysqli_real_escape_string($con, $password);
$select_user = "SELECT * FROM student_accounts WHERE stud_no ='$stud_no' AND password ='$password'";
$run_user = mysqli_query($con, $select_user);
$check_user = mysqli_num_rows($run_user);
if ($check_user > 0)
{
$_SESSION['stud_no'] = $stud_no;
header('location: home.php');
}
else
{
echo '<script>$("#myModal").modal()</script>';
}
}
?>
here's the modal
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
You just need to INCLUDE jQuery library before PHP code run, as well as bootstrap JS. And see the results, it will worked.
IF still not worked use below code to echo your script::
echo '<script type="text/javascript">'
. '$( document ).ready(function() {'
. '$("#myModal").modal("show");'
. '});'
. '</script>';
Replace below line
echo '<script>$("#myModal").modal()</script>';
with
echo '<script>$("#myModal").modal('show')</script>';
First include JQUERY CDN in the tags
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
if (condition === true) //Strict comparison
$(document).ready(function(){
$('#form').modal('show');
});
else
......
N/B #form is the ID for the modal
i am new in codeigniter
<?php foreach ($rows as $row) { ?>
<tr>
<td><?php echo $i++;?></td>
<td><?php echo $row->orderid;?></td>
<td><?php echo $row->order_date;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->price;?></td>
<td>
<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal-<?= $row->customerid?>">View Details</button>
</td>
</tr>
<div class="modal fade" id="myModal-<?= $row->customerid?>" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Order Details</h4>
</div>
<div class="modal-body">
<p><strong>Product Name</strong>: <?php echo $row->name;?></p>
<p><strong>Quantity</strong>: <?php echo $row->quantity;?></p>
<p><strong>Price</strong>: <?php echo $row->price;?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
only details of the first row is displayed in all modals
i need to get the details of each from each modal
I can help you with bootbox modal plugin(http://bootboxjs.com/). Modify your html as follows
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<!-- dialog body -->
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal">×</button>
<div></div>
</div>
</div>
</div>
</div>
then delete your button code and replace with this code
<a class="btn btn-primary your_defined_class_name_for_jquery_use" data-id="<?php echo $row['whatever_id']?>" > What ever name</a>
add the below script on the bottom of the page
<script>$(className).on('click','your_defined_class_name_for_jquery_use', function (e) {
var id=$(this).data('id');
var dialog = bootbox.dialog({
title: title,
message: $('<div></div>').load(url_to_your_controller_which_shows_the_data ),
});
});
Make sure that jquery is previously loaded on the page. I think this will help you.
Try to bellow code:
function get_orders(){
$this->db->select('*');
$this->db->from('orders o');
$this->db->join('order_detail od', 'o.serial = od.orderid', 'left');
$this->db->join('tbl_products p', 'p.prod_id = od.productid', 'left');
$query = $this->db->get();
return $query->result();
}
use this line $this->db->join('order_detail od', 'o.serial = od.orderid', 'left');
not $this->db->join('order_detail od', 'od.orderid = o.serial','left');
Have some db information that I'm trying to get into some modals.
Problem seems to be that the modal only ever grabs the variables from the last while loop. Does all the php on a page run first? Even when its not called?
So I know there is probably easier ways to do this using get_results() and fetch_array and fetch_row but those don't seem to work for me in php 5.5.
Also, I read somewhere to use AJAX. Well as I have never used ajax before, is this something I should look into?
<div class="col-md-4">
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require ($_SERVER['DOCUMENT_ROOT'].'/db-connect.php');
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo $conn->host_info . "\n";
if ($stmt = $conn->prepare("SELECT time, title, tool, descript, thumbpath, smallpath, mediumpath, largepath FROM websites ORDER BY id DESC LIMIT 1")){
//$stmt->bind_param('s',$id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($time, $title, $tool, $descript, $thumbpath, $smallpath, $mediumpath, $largepath);
while ($stmt->fetch()) {
}
$stmt->free_result();
$stmt->close();
}
$conn->close();
?>
<img class="img-responsive" title="<?php echo $tool; ?>" data-toggle="modal" data-target="#modalPort" sizes="100vw" src="<?php echo $thumbpath; ?>" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
<span class="time line-height-small"><?php echo $time; ?></span>
</div>
The variable's here work fine. The problem is that I'm running this same php script a few other times with the same bind_result variables. I don't really want to change variables for each modal.
Modal:
<!-- Website Modals-->
<div class="modal fade" id="modalPort" tabindex="-1" role="dialog" aria-labelledby="portfolioModallabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="portfolioModallabel"><?php echo $title; ?></h4>
</div>
<div class="modal-body text-center">
<img class="img-responsive center-block" src="<?php echo $thumbpath; ?>" sizes="100vw" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
<p class="line-height-small"><?php echo $descript; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
One way is to put modal inside while ($stmt->fetch()) { } assign both modal trigger button and modal itself unique id
Note the data-target="#modalPort<?php echo $id;?>" in modal trigger button and id="modalPort<?php echo $id;?>" in modal HTML
<?php while ($stmt->fetch()) { ?>
<button data-toggle="modal" data-target="#modalPort<?php echo $id;?>" class="btn btn-default">Modal</button>
<div class="modal fade" id="modalPort<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="portfolioModallabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="portfolioModallabel"><?php echo $title; ?></h4>
</div>
<div class="modal-body text-center">
<img class="img-responsive center-block" src="<?php echo $thumbpath; ?>" sizes="100vw" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
<p class="line-height-small"><?php echo $descript; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
Alternate If you want to do with ajax, put modal HTML outside the while loop, add <?php echo $id;?> as data attribute to modal trigger button data-id="<?php echo $id;?>"
<?php while ($stmt->fetch()) { ?>
<button data-toggle="modal" data-target="#modalPort" data-id="<?php echo $id;?>" class="btn btn-default">Modal</button>
<?php } ?>
Modal event listener to get the data attribute value and later use in Ajax method to get the respective row data to show in modal
$(document).ready(function(){
$('#modalPort').on('show.bs.modal', function (e) {
var dataid = $(e.relatedTarget).data('id');
var dataString = 'dataid=' + dataid;
$.ajax({
type: "POST",
url: "get_data.php",
data: dataString,
cache: false,
success: function(data){
$("#content").html(data);
}
});
});
});
get_data.php will be
<?php
//Include database connection
if($_POST['dataid']) {
//run query against `dataid`
?>
<div class="modal-header">
<h4 class="modal-title" id="portfolioModallabel"><?php echo $title; ?></h4>
</div>
<div class="modal-body text-center">
<img class="img-responsive center-block" src="<?php echo $thumbpath; ?>" sizes="100vw" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
<p class="line-height-small"><?php echo $descript; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
<?php } ?>
and Modal HTML will be (outside the loop)
<div class="modal fade" id="modalPort" tabindex="-1" role="dialog" aria-labelledby="portfolioModallabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div id="content"></div> //display data output via ajax
</div>
</div>
</div>