Pass msg_id with jQuery into Twitter Bootstrap modal - php

I have an jquery modal. So now I want to pass value of msg_id to modal.
HTML code:
<a data-toggle="modal" href="#com" id="<?php echo $msg_id; ?>">Comment</a>
--
<div id="com" class="modal hide fade in" style="display: none; ">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3><?php echo $msg_id; ?></h3>
</div>
<div class="modal-body">
<h4>Text in a modal</h4>
<p>You can add some text here.</p>
</div>
<div class="modal-footer">
Call to action
Close
</div>
This is the screenshoot.
If I click comment in table 1, it show and I can see the pass value.
But if I click comment in table 2, it show same with the table 1. Should be different because each have an different msg_id.
So how can I pass the msg_id value into modal with the case of my code ?
Appreciate with your helps.
Thanks

You must add some class attribute to your a tag, for example "modal-toggler"
<a class="modal-toggler" data-toggle="modal" href="#com"
id="<?php echo $msg_id; ?>">
Comment
</a>
And call some jQuery script like this
<script type="text/javascript">
$(".modal-toggler").click(function(){
var _id = $(this).attr("id");
$("#com h3").text(_id);
$("#com").show();
return false;
});
</script>

Related

Why does my jquery submit form popup disable any button clicking or page response?

I'm a bit new to jquery.
Currently, I have a contact 7 form on wp, and have created the function below in order to have a modal popup with a successful submission. However, while it works as intended, when the popup modal appears, the screen goes a tad darker, and neither the 'back to home' or close button will function - or anything else on the page for that matter.
The website is Deltadesigns.co -- Feel free to submit a form -- the inspect shows no errors in the console .
The code is as follows:
<div class="col-md-9 col-sm-10">
<div class="contact-form" id="contact-id">
<?php echo do_shortcode("[contact-form-7 id=\"86\" title=\"Contact form 1\" html_class=\"form-group\"]"); ?>
</div>
</div>
<div class="modal-fade" id="submitModal" tabindex="-1" role="dialog" aria-labelledby="submitModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="submitModalLabel">Success!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Thanks for reaching out! We'll reply within 1-2 business days.
</div>
<div class="modal-footer">
<a button type="button" href="https://deltadesigns.co/" class="btn btn-primary">Back to Home</button></a>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.slim.js" integrity="sha256-HwWONEZrpuoh951cQD1ov2HUK5zA5DwJ1DNUXaM6FsY=" crossorigin="anonymous"></script>
<?php add_action( 'wp_footer', 'mycustom_wp_footer' );
function mycustom_wp_footer() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( '86' == event.detail.contactFormId ) { // Change 123 to the ID of the form
jQuery('#submitModal').modal('show'); //this is the bootstrap modal popup id
}
}, false );
</script>
<?php } ?>
<script type="text/javascript">
document.querySelector('.close').addEventListener('click',
function(){
document.querySelector('.modal-fade').style.display='none';
});
</script>
<?php
get_footer();
?>
The problem was that the jquery function, bootstrap, or wordpress perhaps, added in a modal-backdrop element which I hadn't thought to look for, and the Z-index of the class was set higher than all other elements.
Once I changed it to be below that of the popup, all was well.
Here is the error in the modal-footer:
<div class="modal-footer">
<a button type="button" href="https://deltadesigns.co/" class="btn btn-primary">Back to Home</button></a>
</div>
The <a> and <button> tags are merged in your code.
Try this:
<div class="modal-footer">
<button type="button" class="btn btn-primary">Back to Home</button>
</div>

Edit a selected record from a list with records

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>

Cannot load data from PHP + JQuery to Bootstrap 3 Modal

Briefly speaking, my site has a Bootstrap modal, which is used for display a Youtube video, like following
<!-- Video dialog-->
<div class="modal fade" id="vid-modal">
<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 class="modal-title">
<h4 id="modal-title">Modal title</h4>
</div>
</div>
<div class="modal-body">
<div class="modal-video">
<iframe src=""></iframe> <!-- Code for video here -->`
</div>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- End video dialog-->
This modal is displayed each time user clicks on a link. Those links are loaded from database using PHP with this code:
<div class="panel-group" id="accordion">
<?php
$v_units = $courses->get_distinct_unit($id);
foreach ($v_units as $v_unit) { ?>
<div class="panel-info">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collpase<?php echo $v_unit['unit_id'];?>"><?php echo $v_unit['unit_id'] . ' - ' . $v_unit['unit_name']; ?></a>
</h4>
</div> <!-- End of .panel-heading-->
<div id="collpase<?php echo $v_unit['unit_id'];?>" class="panel-collapse collapse in">
<div class="panel-body">
<?php $v_vids = $courses->get_unit_video($v_unit['unit_id']); ?>
<?php foreach ($v_vids as $v_vid): ?>
<a class="show-vid-modal" href="<?php echo $v_vid['vid_link']; ?>"><?php echo $v_vid['vid_title'] ?></a>
<?php endforeach ?>
</div> <!-- End of .panel-body -->
</div> <!-- End of .panel-collapse -->
</div> <!-- End of .panel-default-->
<?php }?>
</div> <!-- End of #accordion-->
The a class="show-vid-modal" ... triggers the modal.Modal's title and video link is fetched through PHP for each item.
I have added the following javascript code.
<script>
$('.show-vid-modal').click(function(e) {
e.preventDefault();
$("#vid-modal").on('show.bs.modal', function(e) {
$("h4 #modal-title").text("<?php echo $v_vid['vid_title'];"?>);
$("div .modal-video").innerHTML = "<iframe src="<?php echo $v_vid['vid_link'];?>" "
});
$("#vid-modal").modal();
});
</script>
However, it only shows the title and video of the last item for every item (as the foreach loop ends). If I put the javascript inside foreach loop, then it will not work.
Can anyone give me an idea on how I should do that.
In your modal, put an id for the iframe, so it'll be:
<iframe id="video-frame" src=""></iframe>
In your link, create a custom attribute, let's say video-src, so it'll be:
<a class="show-vid-modal" href="#" video-src="<?php echo $v_vid['vid_link']; ?>"><?php echo $v_vid['vid_title'] ?></a>
Once you have it, put in your button's click function:
$('.show-vid-modal').click(function(e) {
//here the attribute video-src is helpfull
var vid-src = $(this).attr('video-src');
var title = $(this).html();
e.preventDefault();
$("#vid-modal").on('show.bs.modal', function(e) {
$("h4 #modal-title").text(title);
//here the id for the iframe is helpfull
$('#video-frame').attr('src', vid-src);
});
$("#vid-modal").modal();
});
It might work.

Placing Values from Anchor to Modal Bootstrap

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.

Put data in #myModal popup Twitter Bootstrap

Its very simple but i don't see the solution.
How can I put a php var in this twitter bootstrap popup link?
Read More
And how can i get and print the value of this link in the popup field?
If short tags are enabled:
<a
href="#myModal"
title="Lees meer"
value="12345"
data-toggle="modal"
data-phpvar="<?=$myVariable?>"
>
Read More
</a>
else:
<a
href="#myModal"
title="Lees meer"
value="12345"
data-toggle="modal"
data-phpvar="<?php echo $myVariable; ?>"
>
Read More
</a>
both indented for readability - don't do this in actual HTML, it looks messy.
And to get it in jQuery - .data("variable") automatically gets attributes that start with data-:
$("a[href=#myModal]").click(function() {
alert($(this).data("phpvar"));
});
or use the .attr equivalent:
$("a[href=#myModal]").click(function() {
alert($(this).attr("data-phpvar"));
});
and to output in the modal:
$("a[href=#myModal]").click(function() {
var phpVariable = $(this).attr("data-phpvar");
var modalObject = $($(this).attr("href"));
modalObject.find(".modal-body").text(phpVariable); //You might also use .html()
});
JSFiddle demo of the code working
Thanks a lot h2ooooooo
I have the short tags enabled and I will build the link ass follow:
<a href="#myModal"
title="Bewerken"
role="button"
data-toggle="modal"
data-phpvar="<?php echo $value['i_id_pk'] . '||' . $value['c_user'] . '||'. $value['i_user_type_id_fk'];?>">
<i class="icon-edit"></i>
</a>
I send the values of data-phpvar with JQuery to the #myModal Popup. I use a OnClick event to get the values out of the a href data-phpvar. Like this:
<script>
$("a[href=#myModal]").click(function() {
var str = $(this).attr("data-phpvar");
var substr = str.split('||');
$("[name=user_id]").val(substr[0]);
$("[name=c_chance_user_name]").val(substr[1]);
$("[name=validation_c_user]").val(substr[1]);
$("[name=c_chance_user_type]").val(substr[2]);
$("span.userName").text(substr[1]);
});
</script>
It will work but, can I do this piece of JQuery make shorter or in a better way?
Eventually I put the values ​​out of the a href data-phpvar in the textfields inside the popup. The textfield must have the same name like the JQuery names:
$("[name=user_id]").val(substr[0]);
or
$("[name=c_chance_user_name]").val(substr1);
The html textfield in the popup is like this:
<div id="myModal" class="modal hide fade" 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">Wijzig gebruiker "<span class="userName"></span>"</h3>
</div>
<div class="modal-body">
<input class="span3" id="appendedPrependedInput" type="text" name="c_chance_user_name" value="" minlength="3" required />
<input class="span3" type="text" data-validation-match-match="c_chance_user_name" name="validation_c_user" minlength="3" required />
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Sluiten</button>
<button class="btn btn-success" name="chanceBtn"><li class="icon-ok-circle icon-white"></li> Opslaan</button>
</div>
</div>
You can find this popup plug-in on www.twitter.github.com/

Categories