Bootstrap modal with checkbox to not show in future - php

I am trying to show a bootstrap modal on body load based on value from mysql database. I have included bootstrap modal in body and showing it successfully according to database value with:
$resultsPop = mysql_query("select popup from members where mid=" . $mid);
$pop = mysql_result($resultsPop, 0, "popup");
if($pop == 0) {
$popupval = "<script type='text/javascript'>$(window).load(function(){ $('#MyPopUp').modal('show'); });</script>";
} else {
$popupval = "";
}
echo $popupval;
And my modal code is:
<div class="modal fade" id="MyPopUp" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="">
<div class="modal-dialog modal-lg">
<div class="modal-content" style="">
<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">Welcome</h4>
</div>
<div class="modal-body" style="text-align: center;">
<span>
Welcome Popup
</span>
</div>
</div>
</div>
</div>
Now, I want to have a checkbox in the modal which on checked can silently pass value 1 and logged in member variable $mid value to a php page after user clicks on modal close button. So, popup value in members table is updated with 1 for that logged in member who do not want popup to be see again.
I have searched for such thing but couldn't successful and I am getting no idea on it.

Try this.
yourphpfile.php
<?php
$resultsPop = mysql_query("select popup from members where mid=" . $mid);
$pop = mysql_result($resultsPop, 0, "popup");
if($pop == 0) {
$popupval = "<script type='text/javascript'>$(window).load(function(){ $('#MyPopUp').modal('show'); });</script>";
} else {
$popupval = "";
}
echo $popupval;
$ppop = $_POST['pops'];
$username = $_POST['mids'];
//Here you can update your database with 1 where username is mids.
//On success
$output = json_encode(array('type' => 'pop'));
die($output);
//Else
$output = json_encode(array('type' => 'error'));
die($output);
?>
<script>
$(document).ready(function(){
$("#closemodal").click(function(event){
event.preventDefault();
var popups = $('#popups').val();
var user_id = $('#mids').val();
$.ajax({
type: 'post',
url: yourphpfile.php,
dataType: 'json',
data: $('#yourform').serialize(),
success: function(data)
{
if(data.type == 'pop')
{
$('#MyPopUp').modal('hide');
}
}
});
});
});
</script>
<div class="modal fade" id="MyPopUp" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="">
<div class="modal-dialog modal-lg">
<div class="modal-content" style="">
<div class="modal-header">
<button type="button" class="close" id="closemodal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Welcome</h4>
</div>
<div class="modal-body" style="text-align: center;">
<span>
Welcome Popup
</span>
<form action="" id="yourform" method="POST" role="form">
<div class="checkbox">
<label><input type="checkbox" name="pops" id="popups" value="1">Pops</label>
</div>
<input type="hidden" id="mids" name="mids" value="<?php echo $_SESSION['mids']; ?>">
</form>
</div>
</div>
</div>
</div>

Related

Bootstrap 4 Modal is not working Inside Slick carousel div

After searching a lot, doing lots of debugging, and I figured out that inside slick carousel div, the bootstrap modal is not displaying. Only showing a gray dropback.
Here is code: In this code, we are getting details from the database and with slick showing images and when someone clicks on those images it will open a modal for that image. But everything is fine except this. How to call the modal inside slick div.
Thank you in advance
<?php
$ush = $mysqli->prepare("SELECT company_id, img, name from list");
$ush->execute();
$ush->store_result();
$ush->bind_result($u_bid, $u_bimg,$u_bname);
?>
<div class="company-logo">
<?php
while($ush->fetch()){
?>
<div>
<a href="#<?php echo $u_bid;?>" data-toggle="modal" data-target="#<?php echo $u_bid;?>"><img
src="data:image/png;base64,<?php echo base64_encode($u_bimg); ?>" height="80px" width="80px"></a>
<!-- Modal -->
<div class="modal" id="<?php echo $u_bid;?>" tabindex="-1" role="dialog" aria-labelledby="<?php echo $u_bid;?>" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-md" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="container text-center">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><h3><b>X</b></h3></span>
</button>
<h3><?php echo $u_bname;?></h3>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
$ush->close();
?>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.company-logo').slick({
dots:true,
infinite:false,
speed:300,
slidesToShow:14,
slidesToScroll:14,
arrows:false,
responsive:[{
breakpoint:768,
settings:{
slidesToShow:4,
slidesToScroll:4
}
}]
})
});
</script>
I think you should move the modal code parts outside of your carousel parts like this:
<?php
$ush = $mysqli->prepare("SELECT company_id, img, name from list");
$ush->execute();
$ush->store_result();
$ush->bind_result($u_bid, $u_bimg,$u_bname);
$carousel_content = '';
$modal_content = '';
while($ush->fetch()){
$carousel_content .= '<div>
<a href="#'.$u_bid.'" data-toggle="modal" data-target="#'.$u_bid.'"><img
src="data:image/png;base64,'.base64_encode($u_bimg).'" height="80px" width="80px"></a>
</div>';
$modal_content .= '<!-- Modal -->
<div class="modal" id="'.$u_bid.'" tabindex="-1" role="dialog" aria-labelledby="'.$u_bid.'" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-md" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="container text-center">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><h3><b>X</b></h3></span>
</button>
<h3>'.$u_bname.'</h3>
</div>
</div>
</div>
</div>
</div>';
?>
<?php
}
$ush->close();
?>
<div class="company-logo"><?=$carousel_content?></div>
<div id="modal_contents"><?=$modal_content?></div>
<script type="text/javascript">
$(document).ready(function(){
$('.company-logo').slick({
dots:true,
infinite:false,
speed:300,
slidesToShow:14,
slidesToScroll:14,
arrows:false,
responsive:[{
breakpoint:768,
settings:{
slidesToShow:4,
slidesToScroll:4
}
}]
})
});
</script>

how to get the correct value from json (php)?

update_modal(bootstrap)
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit:</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="body">
<input type="text" class="form-control" placeholder="product_title" id="title">
</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<a class="btn btn-primary" href="login.php">Update</a>
</div>
</div>
</div>
</div>
trigger the update_modal(bootstrap)
<td>
<a data-toggle='modal' data-target='#updateModal'>
<button class='btn btn-success' id='edit' data-id='$product_no'>Edit</button>
</a>
</td>
js
<script>
$(document).ready(function() {
$(document).on('click','#edit',function () {
let id=$(this).attr('data-id');
// console.log(id);
$.ajax({
url:'get_record.php',
method:'post',
data:{
p_no:id
},
dataType:'JSON',
success:function (data) {
// $('#body').html(data);
console.log(data[0]);
console.log(data[1]);
console.log(data[2]);
}
});
});
})
</script>
get_record.php
<?php
include ("db.php");
global $conn;
if (isset($_POST['p_no'])){
$p_no=$_POST['p_no'];
$query="select * from products where p_no='$p_no'";
$result=mysqli_query($conn,$query);
while($row=mysqli_fetch_array($result)){
$user_data=' ';
$user_data [0]=$row['product_title'];
$user_data [1]=$row['date'];
$user_data [2]=$row['product_price'];
// echo $pro_title;
}
echo json_encode($user_data);
}
My problem is when I click the edit button, the update_modal displays successfully, but it doesn't get correct values from my database. I click F12 on my chrome and it tested like this photo
You are initializing $user_data in get_record.php as a string: $user_data = ' ';
Try initializing that variable as an array instead: $user_data = [];
Because you initialize $user_data as a string, setting $user_data[0] only sets the first character of the string to the first character of the value $row['product_title'].
EDIT: As noted in the comments, you are also opening yourself up to SQL injection attacks by inserting posted data directly into your SQL here:
$p_no=$_POST['p_no'];
$query="select * from products where p_no='$p_no'"
You should escape all user inputs before using them in a query. One way to do that for MySQL in PHP:
$p_no=mysqli_real_escape_string($_POST['p_no']);
$query="select * from products where p_no='$p_no'"
Using prepared statements would be ideal.

Modal not loading using javascript in php(CodeIgniter)

I want to open model on successful submission of form but although form is submitting modal is not popped up.
public function insert($data) {
// Inserting into your table
// Calling model
$done = $this->db->insert('sign_up', $data);
if($done) {
echo "<script>$('#thankyouModal').modal('show')</script>";
echo '<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
<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">Thank you for pre-registering!</h4>
</div>
<div class="modal-body">
<p>Thanks for getting in touch!</p>
</div>
</div>
</div>
</div>';
}
}
I am trying to open the model in controller using
$data = array(
'first_name' => $fname,
'last_name' => $lname,
'email' => $email,
'password' => $password
);
$this->load->model('modal');
$this->modal->insert($data);
Try below code; this will work
public function insert($data) {
// Inserting into your table
// Calling model
$done = $this->db->insert('sign_up', $data);
// You can do something else here
if($done) {
?>
<script>$(document).ready(function(){$('#thankyouModal').modal('show')});</script>
<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
<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">Thank you for pre-registering!</h4>
</div>
<div class="modal-body">
<p>Thanks for getting in touch!</p>
</div>
</div>
</div>
</div>
<?php
}
}
Put the code of modal in a view file so that we can easily call the modal with javascript in the view file.
And you need to set a variable in flash data so that you can check the condition that the form has been succesfully inserted or not.
Here is the code , I hope it helps you:
Controller file :
public function insert($data) {
// Inserting into your table
// Calling model
$done = $this->db->insert('sign_up', $data);
// You can do something else here
if($done) {
//You can set the message and variable name as per your need.
$this->session->set_flashdata('inserted','Yes');
//Redirect to the desired view file
redirect("controller/anotherfunction_where_view_file_is_loaded");
}
View File:
<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
<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">Thank you for pre-registering!</h4>
</div>
<div class="modal-body">
<p>Thanks for getting in touch!</p>
</div>
</div>
</div>
</div>
<?php if((isset($this->session->flashdata('inserted'))) && $this->session->flashdata('inserted') != "") { ?>
<script type="text/javascript">
$(document).ready(function(){
$('#thankyouModal').modal('show');
});
</script>
<?php } ?>
I got the answer
CONTROLLER
public function insert() {
$signup_email = $this->input->post('signup_email');
$signup_password = $this->input->post('signup_password');
// Checking if everything is there
if ($fname && $signup_email && $signup_password) {
$data = array(
'email' => $signup_email,
'password' => $signup_password
);
if($this->modal->insert($data)) {
echo "SOME DATA TO BE DISPLAYED IN MODAL";
}
}
}
VIEW
<body>
<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
<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">Thank you for pre-registering!</h4>
</div>
<div class="modal-body">
<p>Thanks for getting in touch!</p>
</div>
</div>
</div>
</div>
<script>
var signup_email = $('#signUp').val();
var signup_password = $('#signUpPassword').val() ;
$.ajax({
type: "POST",
url: "<?php echo site_url('form/insert'); ?>",
data: { signup_email: signup_email, signup_password: signup_password },
dataType: "html",
success: function(data) {
if(data) {
$('#thankyouModal').modal('show');
}
}, error: function() {
alert("ERROR!");
}
});
</script>
</body>

how can i pass variable to php after open modal

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.

Foreach video create modal

I'm trying to loop through an array of videos and create a button/modal combo for each. I'm having an issue with the need for id="myModal". Each video's button links to the same video. How would I go about keeping these unique? I've tried changing id to class, but that wasn't working.
<?php foreach ($thisVideos as $video):?>
<button class="btn btn-primary link">Video</button>
<div id="myModal" class="modal fade" 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>
</div>
<div class="modal-body">
<iframe width="400" height="300" frameborder="0" allowfullscreen=""></iframe>
</div>
</div>
</div>
</div>
<script>
$('.link').click(function () {
var src = 'http://www.youtube.com/v/<?=$video["entry_data"]["video-id"]?>';
$('#myModal').modal('show');
$('#myModal iframe').attr('src', src);
});
$('#myModal button').click(function () {
$('#myModal iframe').removeAttr('src');
});
</script>
<?php endforeach; ?>
Your approach is wrong, you only need one modal element and one script (and one event handler...) so you put these outside of the loop. You can add the url in a data attribute in your video list and then you can do everything you need to do.
A simple example with your code:
<?php foreach ($thisVideos as $video): ?>
<button class="btn btn-primary link"
data-url="http://www.youtube.com/v/<?=$video["entry_data"]["video-id"]?>">Video</button>
<?php endforeach; ?>
<div id="myModal" class="modal fade" 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>
</div>
<div class="modal-body">
<iframe width="400" height="300" frameborder="0" allowfullscreen=""></iframe>
</div>
</div>
</div>
</div>
<script>
$('.link').click(function () {
// get the url from the data attribute
var src = $(this).data('url');
$('#myModal').modal('show');
$('#myModal iframe').attr('src', src);
});
$('#myModal button').click(function () {
$('#myModal iframe').removeAttr('src');
});
</script>
May be this help you
<?php foreach ($thisVideos as $key=>$video):?>
..............
<div id="myModal_<?php echo $key; ?>" class="modal fade"
....................
..............
<script>
$('.link').click(function () {
var src = 'http://www.youtube.com/v/<?=$video["entry_data"]["video-id"]?>';
$('#myModal_<?php echo $key; ?>').modal('show');
$('#myModal_<?php echo $key; ?> iframe').attr('src', src);
});
$('#myModal_<?php echo $key; ?> button').click(function () {
$('#myModal_<?php echo $key; ?> iframe').removeAttr('src');
});
</script>
<?php endforeach; ?>
Hope you understand and it helps you.
Note Its not a good way.It just solves the way you want.
The trick is to have an unique id for #myModal and this can by done in so many different ways.
You can easily set a counter in your loop, for example,
$counter = $counter++;
Append
<div id="myModal_<?=$counter?>"
and in the js you should have
$('#myModal_<?=$counter?>')
This will give the video button link unique ids and all the video links will work.

Categories