PHP variable to javascript + modal - javascript

I want to create modal button for delete a user gets from a foreach in the html body.
The modal opens an iframe with a form. The iframe url seems like
<iframe src=<?php echo base_url('index/page/');?> witdth....
I want to pass to the iframe src the php variable contains an userid, for example:
<iframe src=<?php echo base_url('/index/page/'+username);?> witdth....
First code works fine and it shows the view but no variable passed with the second code.
I use a javascript for open the modal and get the ID variable.
The link:
<a data-toggle="modal" data-target="#myModal" data-detail-id=<?php echo $users->username; ?>>
And the js:
<script>
$(".myModal").on("click", function(e) {
var username;
e.preventDefault();
username = $(this).data("detail-id");
});
</script>
How to get the detailId to the iframe src to pass the variable to the index/page?
Thanks a lot.
EDIT---
I will post full code, the ovjective is to pass a PHP variable got in a foreach to a modal windows in that calls a iframce to other view to use the variable:
<a data-toggle="modal" data-target="#myModal" data-detail-id=<?php echo $users->username; ?>></a>
<!-- 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"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<script>
$(".myModal").on("click", function(e) {
var username;
e.preventDefault();
username= $(this).data("detail-id");
});
</script>
<center><iframe src=<?php echo base_url('/index/page/'+username);?> width="500" height="380" frameborder="0" allowtransparency="true"></iframe></center>
</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>
Thanks!!

You are mixing JS and PHP together:
<iframe src=<?php echo base_url('/index/page/'+username);?> width.... ( note the + )
<iframe src=<?php echo base_url('/index/page/'.$username);?> width.... ( append with . operator )
In PHP, you append with . operator

Also if you are looking for easy solution to delete smoething (ex. user) you can use something like this in your view:
<script>
function doconfirm()
{
job=confirm("Are you sure, to delete?");
if(job!=true)
{
return false;
}
}
</script>
<?php foreach ($users as $user_detail): ?>
Delete user<br>
<?php endforeach ?>
and from your user model try somethig like:
public function delete($user_id)
{
$query = $this->db->where('user_id', $user_id);
return $query = $this->db->delete('users');
}
Hope this helps.

Related

Woocommerce : add a text pop up on add to cart conditionally

I'm new at PHP and Woocommerce and I'm trying to add a pop up containing HTML when a client adds a product in the cart conditionally, here's what I've tried so far :
add_action('wp_footer','custom_jquery_add_to_cart_script');
function custom_jquery_add_to_cart_script(){
if ( is_product() ):
?>
<script type="text/javascript">
// Ready state
(function($){
$(document.body).on("added_to_cart", function (event, fragments, cart_hash, $button) {
var myRadio = $("input[name=payment_type]");
var checkedValue = myRadio.filter(":checked").val();
if(checkedValue == "deposit")
{
alert("Alert !");
}
});
})(jQuery); // "jQuery" Working with WP (added the $ alias as argument)
</script>
<?php
endif;
}
It works very well with an alert, but I would like to add html content instead and I'm unsure of how to do that with PHP, JS and Jquery ... I'm new to the WordPress workflow in general
Here's another syntax I tried :
add_action('wp_footer','custom_jquery_add_to_cart_script');
function custom_jquery_add_to_cart_script(){
if ( is_product() ):
?>
<script type="text/javascript">
// Ready state
(function($){
$(document.body).on("added_to_cart", function () {
var myRadio = $("input[name=payment_type]");
var checkedValue = myRadio.filter(":checked").val();
if(checkedValue == "deposit")
{
$('#myModal').modal('show');
}
});
})(jQuery); // "jQuery" Working with WP (added the $ alias as argument)
</script>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Data</h4>
</div>
<div class="modal-body">
<div class="fetched-data"><?php $message ?></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php
endif;
}
But unfortunatelt it just keeps loading, nothing happens after the "Add to cart" action.
Thank you for your help !

how to open bootstrap modal with different PHP value

i have a php app with 3 table, each one have multiple row with button for each row to get a form for the affected data
the idea is to give a value for the modal to have as much column as in the affected table.
until now, i tried to have one form with button fo each table, with different value, and testing it in php to use the correct table for for the column as below in the code (it's a test webpage so there is just one button, i'm just trying to open a modal manually after testing a php value ) :
<body>
<form action="" method="post">
<button type="submit" class="btn btn-primary" data-bs-toggle="modal" data-bs-
target="#CreateInfra" name="der">
Launch demo modal
</button>
</form>
<script>
function modal(){
$("#myModal").modal('show');
}
</script>
<div class="bs-example">
<!-- Modal HTML -->
<div id="myModal" class="modal fade" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal Title</h5>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<p>This is a simple Bootstrap modal. Click the "Cancel button", "cross icon" or
"dark gray area" to close or hide the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data
dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
</div>
<?php
if (isset($_POST['der'])){
$value = 1;
var_dump($value);
}
else{
$value=2;
var_dump($value);
}
if ($value == 1){
echo "<script>modal()</script>";
}
?>
</body>
the thing is , when i hit the button, i see my page loading something for maybe 0.4 second but nothing happen.
is there any way to do this or i should use something else ?
You can do something like this as long as you want it to decide on the page load
<script>
function modal(val){
let options = [];
var myModal = new bootstrap.Modal(document.getElementById('exampleModal'))
if (val == 0){
myModal.show()
}
}
</script>
<?php $val = 0; ?>
<?php echo "<script>modal(".$val.")</script>" ?>
Other than that you would have to do it through a fetch request or AJAX because php and JS can't directly talk to each other

Calling a modal form after setting a session variable on a page

I want to call a modal form after setting a session variable on a page.
I need some advice. I am stuck on calling the modal after setting a session variable on a page. Please note that I want to call the modal on an if statement behavior, not on a button click.
I've tried testing my modal forms if there was an error on them on a button click and it was working fine and test the session variable if it is really set using an alert command and session variable is really passing a value but the calling of modal is not triggering. I dunno if I'm missing something, sorry in advance.
Here are my code samples:
index.php:-
<?php require('includes/header.php');?>
<?php require('includes/sidebar.php');?>
//content or body page
<?php include('Incoming_Home.php');?>
<?php require('includes/modals.php')?>
<?php require('includes/Footer.php')?>
Incoming_Home.php
//some long codes and inside a table is here
<td><a class="btn btn-warning btn-sm" href="processUpdate.php?id=<?php echo $myrow["inc_id"];?>">Update</a>
</td>
<?php
if(isset($_SESSION["Update_ud"])){
echo "<script>
$(document).ready(function(){
$('#modal-update-incoming').modal('show');
});
</script>";
//test if session is set then alert
//echo "<script>alert('".$_SESSION["Update_ud"]."');</script>";
//test if session is set then alert
unset($_SESSION["Update_ud"]);
}
?>
processUpdate.php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
//echo $_GET["id"];
$_SESSION["Update_ud"] = $_GET["id"];
?>
<script type="text/javascript">
window.location='index.php';
</script>
Modals Code
<div class="modal fade" id="modal-update-incoming">
<div class="modal-dialog modal-lg">
//some contents
</div>
</div>
You could do something like that:-
<?php if(isset($_SESSION['Update_ud'])){ ?>
<div class="modal hide fade" id="myModal">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Success</h3>
</div>
<div class="modal-body">
<p><?php echo $_SESSION['Update_ud']; ?></p>
</div>
<div class="modal-footer">
<a class="close btn" data-dismiss="modal">Close</a>
</div>
</div>
<script type="text/javascript">
$(window).load(function () {
$('#myModal').modal('show');
});
</script>
<?php } ?>

Bootstrap modal POST issues with AJAX

I've been trying to create a confirm delete popup in a modal via bootstrap. I was initially having issues with posting via plain php, but now I don't know whether my AJAX code is working or not. If I create an alert on error and success, both of them alert.
Here is the following code:
Modal:
<div id="delete_data_Modal" 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">Delete Member</h4>
</div>
<div class="modal-body">
<form method="post" id="deleteMember">
<input type="hidden" value="<?php echo $deleteMemberID ?>">
<p>Are you sure you want to delete this member?</p>
<button class="btn btn-danger"
id="deleteConfirm">Delete
</button>
</form>
</div>
</div>
</div>
</div>
PHP code when confirm delete is clicked:
<?php
if (isset($_POST['deleteConfirm'])) {
$deleteMemberID = $_POST['MemberID'];
$sql = "DELETE FROM tbl_Members WHERE tbl_Members.MemberID = %{$deleteMemberID}";
$update = mysqli_query($DB, $sql);
echo "<script>alert('DELETED')</script>";
}
?>
and finally, my AJAX code.
<script>
$(document).ready(function () {
$('#deleteMember').submit(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
data: $(this).serialize(),
success: alert("yay"),
});
});
});
</script>
I'm relatively new to PHP, and extremely new to AJAX (Have never used it before this), so I don't know how obvious the issue could be. Any comments would be appreciated. TIA.

dynamic php id JQuery ajax output will not change

After a button is selected on a fresh page, all information are there, but whenever another is clicked, the first info is still there and wont change until the page is refreshed.
Help me pass this will ya?
button
<?php while($b = mysqli_fetch_assoc($equery)): ?>
<button type="button" onclick="modal(<?=$b['id'];?>)" class="w3-btn w3-border" style="width: 260px; display: inline-block; margin: 9px">
<img src="">
<h3><?php echo $b['product'];?></h3>
<h5>Descritopm</h5>
<h6></h6>
</button>
<?php endwhile;?>
js:
function modal(id){
var data = {"id" : id};
jQuery.ajax({
url : <?=BASEURL;?>+'includes/modal.php',
method : "post",
data : data,
success: function(data){
jQuery('body').append(data);
jQuery('#mod').modal('show');
},
error: function(){
alert("IDK")
}
});
}
modal:
<?php
require_once '../core/init.php';
$id = $_POST['id'];
$id =(int)$id;
$sql = "SELECT * FROM aisle WHERE id = '$id'";
$result = $db->query($sql);
$product = mysqli_fetch_assoc($result);
?>
<!-- Modal -->
<?php ob_start();?>
<div id="mod" class="modal fade" role="dialog" tabindex="-1">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title w3-center"><?=$product['product'];?></h3>
</div>
<div class="modal-body">
<p><?=$product['aisle'];?></p>
<p>$<?=$product['price'];?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php echo ob_get_clean();?>
The problem is here
jQuery('body').append(data);
This line will append more modal and many modals will appear on your page. You should follow this way. Create a div tag called contents_from_ajax inside body tag and then use this.
jQuery('#contents_from_ajax').html(data);
This code will remove all old modals and create a new one instead of append, and then it will work fine.
Hope this help

Categories

Resources