How I can show a specific modal depending on a variable in php which is modified in the url.
I'm using as responsive framework Bootstrap.
I have a page containing several modals, each corresponding to a product. I need to make a link from other parts of the site to go to page "products.php" and automatically display the required product.
E.g:
href="products.php?prod=1"
So far, I have only succeed in showing the modal when loading the page, but if I add or change the variable, it has no effect, not working at all.
This is what I have:
At the beginning
<?php
//Variable to choose which modal to show, default = 1
$prod = 1;
?>
Then the modal which has an id for identification.
<div class="modal fade" id="product1" tabindex="-1" role="dialog" aria-hidden="true">
<!-- Content of the modal -->
</div>
Then the script that show the modal when the page loads.
<script type="text/javascript">
$(document).ready(function(){
<?php if ($prod == 1) {
echo "$(document).ready(function () {var a = 'PD1'; alert(a); });";
echo "$('#product1').modal('show');";
}elseif ($prod == 2) {
echo "$(document).ready(function () {var a = 'PD2'; alert(a); });";
echo "$('#product2').modal('show');";
};
?>
});
</script>
Any idea?
You not gathering the value of the product variable from the URI. To accomplish this at the top of the page where you have $prod=1; you can put the following line of code :
$prod = (isset($_GET['prod']))?$_GET['prod']:"1";
This tells the system to get the value of the prod= in your url if it can't find it then use "1" as a default.
This is not the ideal answer, there are quite a few reasons not to access $_GET/$_POST variables in this way without proper filtering. I leave that for you to research.
You are doing opposite, should be
<?php if ($prod == "1") { ?>
<script type="text/javascript">
$(document).ready(function(){
var a = 'PD1';
alert(a);
$('#product1').modal('show');
});
</script>
<?php } elseif ($prod == "2") { ?>
<script type="text/javascript">
$(document).ready(function(){
var a = 'PD2';
alert(a);
$('#product2').modal('show');
});
</script>
<?php } ?>
Related
I'm trying to show a bootstrap modal after submitting a form and getting the result from the database. The page has to reload and show the extra modal so that the user can try to log in again.
I've tried to reload the page and then echo the script
Here is what I have for now
<? php
if(condition)
echo "<script>$('#modal').modal('show')</script>";
?>
...
<div class="modal fade" id="modal" tabindex="-1" role="dialog"</div>
The page is either reloading without the modal, or a blank page. I expect it to load with the added modal.
Have you tried tying the modal to a JS method, then calling the method from PHP?
<?php
if($condition)
echo "<script>showModal()</script>";
?>
function showModal() {
$('#modal').modal('show');
}
Edit:
You could try something like this:
<script>
$(function(){
if (<?php echo $condition ?> === true) {
$('#modal').modal('show');
}
});
</script>
I am new to this client variable to server variable transfer process.
Essentially I have a Google maps api function and have a marker on a map to represent a username in a users table (MySQL) of my PHP web app. When I double click on that map marker my Jquery function is popping up a modal and I am able to get the username in an id/name element (chosenUser) in a . However, I now need to be able to access this value in a PHP variable ($chosenUser) without any "submit" or page refresh, so that I can invoke my PHP function - getUserChannelID($chosenUser), and continue with the rest of the logic.
My disconnect is the ability to be able to access that value coming through on the id/name as #chosenUser and transfer it to a PHP variable $chosenUser without a "submit" or page refresh. All the code below is on the same page.
I am reading about the use of AJAX to do this in SF posts but since I quite the infant with AJAX I am getting confused on how exactly this can be done on the same page without a form "submit" or page refresh.
Can you please help correcting the code below so that I can get that value in #chosenUser to the PHP variable $chosenUser?
Here's my code all in the same PHP file.
<?php
require_once("classes/autoload.php");
$db = new Database();
....
?>
<html>
<head>
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<link rel="stylesheet" href="css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js" ></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid" style="margin: 0 auto;">
<div style="height: 100%; position: relative; top:30px;">
<div id="map" style="height: 100%;"></div>
</div>
<div class="modal fade" id="showChannel" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Channel Preview</h4>
</div>
<div class="modal-body">
<div>
<input type="hidden" id="chosenUser" name="chosenUser" value="">
<!-- This is where I need help. How do I transfer that value I am getting for "chosenUser" above into the PHP variable $chosenUser - ideally the code in the next line would need to be executed -->
<?php $channelId = $db->getUserChannelID($chosenUser); ?>
.....
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.20&sensor=false"></script>
<script src="multiple_marker/oms.js"></script>
<script type="text/javascript">
var mapMarkers = [];
var infowindow = null;
var infowindowOpen;
var map, oms;
var gm = google.maps;
var markerIcons = new Array();
var gmarkers = [];
function getCoords() {
$.ajax({
url: "markers.php", // this markers.php provides the value of "Locations" array used below
type: "POST",
data: {
foo: "bar"
},
dataType: "json",
success: function (returnedData) {
console.log(returnedData);
if(!spiderify)
moveMarkerMap(returnedData);
//window.setInterval(getCoords, 10000);
window.setTimeout(getCoords, 2000);
}
});
}
function MarkerMap(json) {
var iconBase;
oms.addListener('click', function (marker) {
console.log('clicked');
});
if ((json.Locations.length > 0)) {
for (i = 0; i < json.Locations.length; i++) {
var location = json.Locations[i];
var username = location.username; // I am getting the value of username here
var myLatlng = new gm.LatLng(location.Lat, location.Long);
var marker = new gm.Marker({
position: myLatlng,
map: map,
username: username,
});
var infowindow = new gm.InfoWindow();
var div = document.createElement('DIV');
gm.event.addListener(marker, 'dblclick', (function (marker, div, infowindow) {
return function () {
$('#showChannel').modal('show');
$('#chosenUser').val(marker.username); //passing the value of marker.username to the modal input id/name="chosenUser"
};
})(marker, div, infowindow));
gmarkers[id] = marker;
}
}
window.map = map;
window.oms = omg;
}
gm.event.addDomListener(window, 'load', initialize);
</script>
Well, you can't exactly communicate between PHP and JS in the way you're thinking of. PHP is executed while the page is loading, before any content is displayed, before any javascript is even sent to the client, so it's impossible to do it as you wish.
What you can do is call a PHP script once the page is finished loading, using javascript. You can do this whenever you like, using a delay loop, when clicking a button, any way you can call your javascript function. That way you can send information from javascript to PHP, and when the PHP script finishes running it can report back information to javascript.
You want a variable from js in PHP, there are multiple ways of doing this, such as POST or GET requests. I'll post an example using POST, which is the same thing you'd use to send a form. You'll need to include jQuery for this to work.
JS
var myVar = "my info";
var myVar2 = "my other info";
$.post("myscript.php", {
info1: myVar,
info2: myVar2
}, function(answer) {
// answer will contain whatever the PHP script printed
// in this example answer will be "success"
alert(answer);
} );
PHP
$var1 = $_POST['info1'];
$var2 = $_POST['info2'];
if(!empty($var1) && !empty($var2))
{
echo "success";
}
It's up to you to implement it now.
I have a foreach loop in php that searches through a directory, finds any other directories, and then using hide/show in javascript, the subdirectories names are made into links that drop down to reveal the files inside of that specific subdirectory. I hope that makes sense. The problem I have is that because I am using a loop to find any present subdirectories, I can’t give each of the subdirectories a different id. As a result, all of the links have the id of the first link and when any of them are clicked, the first link always drops down. Do I need to use JQuery for this?
<!--Code for the javascript part:-->
<?php
<script language="javascript">
function showOrHide(){
var div = document.getElementById("showOrHideDiv");
if (div.style.display == "block"){
div.style.display = "none";
}
else {
div.style.display = "block";
}
}
</script>
?>
<!-- A subdirectory has been found and is called $subDir -->
<!-- Below is the show/hide part of my html/php code -->
<?php echo $subDir;?>
<div id="showOrHideDiv" style="display: none">
<!-- The rest of the code that prints the files from the subdirectory -->
</div>
One approach would be to use a counter and use that to vary the IDs:
<?php echo $subDir;?>
<div id="showOrHideDiv_<?php echo $counter;?>" style="display: none">
Then your javascript changes:
<script language="javascript">
function showOrHide(num){
var div = document.getElementById("showOrHideDiv_" + num);
if (div.style.display == "block"){
div.style.display = "none";
}
else {
div.style.display = "block";
}
}
</script>
I'm trying to create a 'print' button to open a new window and display a PHP Variable in it.
The code below is looped through the PHP script as many times as there are tickets, however I can't seem to get the correct number to display when the window opens (the number that displays in the print link is correct - but when the new window is opened it's incorrect).
<script type='text/javascript'>
jQuery(function($) {
$('a.new-window').click(function(){
var recipe = window.open('','PrintWindow','width=600,height=600');
var html = '<html><head><title>Print Your Ticket</title></head><body><div id="myprintticket">' + $('<div />').append($('#ticket').clone()).html() + '</div></body></html>';
recipe.document.open();
recipe.document.write(html);
recipe.document.close();
return false;
});
});
</script>
Print <?php echo $EM_Booking->get_spaces() ?>
<div style="display:none;">
<div id="ticket"><?php echo $EM_Booking->get_spaces() ?>
</div>
</div>
why not try something like this:
<a href="#" class="new-window">Print <?php echo $EM_Booking->get_spaces() ?>
<div class="ticket" style="display:none">
<?php echo $EM_Booking->get_spaces() ?>
</div>
</a>
<script>
jQuery(function ($) {
$('a.new-window').click(function () {
var btn = $(this),
ticket = btn.find('.ticket').html(),
recipe = window.open('','PrintWindow','width=600,height=600'),
html = '<html><head><title>Print Your Ticket</title></head><body><div id="myprintticket">' + ticket + '</div></body></html>';
recipe.document.open();
recipe.document.write(html);
recipe.document.close();
return false;
});
});
</script>
But much better solution would be to give a button a unique ID, and onclick open an existing (php-generated) page from the server passing that ID, e.g. /getBooking.php?id=123 and that page would output whatever's needed.
hi i am building a php mysql database pagination page, so i have a list of records 2 rows long at the bottom of the record i want a div which opens up when the span above it is clicked, how do i set up the jquery to make it so that it takes the id of the <p> and expands it in jquery
<span id="button1">Toggle</span>
<p id="p1">
hello<br/>hello
</p>
<script>
$("#button1").click(function () {
$("#p1").slideToggle("slow");
});
</script>
when i output it in php mysql the button will all have a different id and the p will have different ids as well
//Jquery Code which calls toggle_visibility function
$(".pOne").click(function(){
toggle_visibility('partsIdThree');
})
.toggle( function() {
$(this).children("span").text("[-]");
}, function() {
$(this).children("span").text("[+]");
});
//HTML Code
<div id="parts_toogle_one">
<p class="pOne">Add Records <span>[+]</span></p>
<div id="msg_body_one">
<tr><td></td></tr>
</div>
</div>
// Javascript code
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == "inline") {
e.style.display = 'none';
}
else if(e.style.display == "none") {
e.style.display = "inline";
}
}
Something like this..
You can use a dynamic id retrieving, so you don't have to worry about the number of results..
For example:
<?php $i = 0;?>
<?php foreach($resultset as $result) : ;?>
<span class="activator" id="result<?php echo $i;?>">CLICK HERE</span>
<div id="panel_result<?php echo $i;?>">SLIDER DIV</div>
<!-- Do the rest of you processing of $result here; it just doesn't matter, for here you only need some identification, I used a number for convenience. -->
<?php ++$i;?>
<?php endforeach;?>
Your jquery:
$('.activator').click(function(){
var the_id = $(this).attr('id');
var the_panel = $('#panel_' + the_id);
$(the_panel).slideToggle('slow');
});
So you don't have to write a jQuery command for each line you print in php, use just this snippet and it will work out by itself which panel to slide, according to which span is clicked.