Below is my jQuery Code:
<script>
$(document).on('click','.quickview', function(){
var **image** = $(this).attr('image');
var testimage = document.getElementById('quickimage');
testimage.src= "<?= base_url().'assets/product_images/'.image.".png"?>";
});
</script>
I want to use this image value to the src attr of img which is in a modal, what is the best way can please help? I will be very thankful to you.
You can't use JavaScript variables in PHP, since PHP runs on the server and finishes before the page is sent to the browser.
But you don't need to use the JS variable in PHP. You can do the concatenation in JavaScript.
testimage.src= <?= json_encode(base_url()) ?> + `assets/product_images/${image}.png`;
Related
So I declared a variable empty in external JavaScript file which I am sourcing in my main HTML page and because I want to load that variable from PHP I am doing this but it doesn't seem to work.
<script src="./assets/js/script.js"></script>
<script type="text/javascript">var APIKey = <?php echo $API; ?></script>
script.js is the one having empty global variable like this:
var APIKey = "";
I have already declared $API in PHP and I know it's working because I tried echoing it as text and it works but for some reason it doesn't in script. Please help.
Thanks!
So it took a bit time to figure out but the mistake was pretty simple.
All I had to do was REMOVE this from script.js
var APIKey = "";
The problem was that I was defining it 2 times and I don't think that's supported for some reason but that worked for me so if anyone else has the same issue, it'll work for you as well. Have a good day and keep on coding.
I want to take the session data in .js file.Here i want the data in a function
Now i'm directly put the value.How to take it from session.
.js file
function callAllfnt(){
get_data(1) ;
}
I want the 1 as session data.How to take it
If you want it on page load the try this:
function callAllfnt() {
var data = "<?php echo $_SESSION['givenName']; ?>";
}
In later part in the life cycle of the application, use AJAX
1)
View File
<script type="text/javascript">
var Sessionvalue = "<?php echo $this->session->userdata('session_name')";
<script>
After load your js file
<script src="<?php echo base_url(); ?>js/file.js" type="text/javascript"></script>
JS FILE
function callAllfnt() {
var data = SessionValue;
}
(OR)
2) Using Ajax call and Get the Session value
You need to create php script that will return Session data and call that with ajax like this:
php
if (isset($_GET['var'])) {
echo json_encode($_SESSION[$_GET['var']]);
}
js with jQuery:
function get_data(variable, callback) {
$.get('session.php', {'var':variable}, function(data) {
callback(data);
}, 'json');
}
and you can call that function using:
get_data(1, function(value) {
alert(value);
});
just use php tag with in single quotes if you java script is written in html page like
function callAllfnt() {
var value = '<?php echo $_SESSION["varName"]; ?>';
get_data(value) ;
}
But if your javascript code is written seperate js file then use ajax.
Third option is if your javascript file included in php file then create a javascript variable on top of js file including code and use that variable in js file.
This is my employeeshift.js code and I can't figure out how to do a PHP code in a JavaScript file
This is my code inside the employeeshift.js but my var inorno is null
$(document).ready(function(){
var inorno= "<?php echo json_encode($inOrOut ); ?>";
if(inorno=="0"){
$('.Sales1').hide();
}else{
$('.Sales1').show();
}
});
Rename your js file to php ....or past js code to php file then do like below
<?php
//some of your main php file code ..........
?>
<script type="text//javascript">
$(document).ready(function(){
var inorno= "<?php echo json_encode($inOrOut ); ?>";
if(inorno=="0"){
$('.Sales1').hide();
}else{
$('.Sales1').show();
}
});
</script>
Your JS file is probably NOT parsed as PHP script, unless you changed some server config. One way would be to rename it to employeeshift.js.php
if this is your JS file than var value will always be null, you can assign value to a var only when the file is php and you are adding this in script tag
You can't (and probably shouldn't, even if you could) write PHP into a JavaScript file. PHP on your server doesn't "know" that it should execute any PHP code you've placed there. An alternative would be to put the JavaScript, surrounded by <script></script> tags, in a PHP file, and then your could should work.
I'm working on a link disclaimer page for internal use and on of the issues I'm having is getting the outgoing URL into the link on the page. The code below is what I'm using to grab the GET value I need and put it in the link's href attribut after decoding. The other half of the code uses the built-in Javascript encodeURIComponent() function, so unless I can use the PHP decode function, or if there's a dead simple way to grab the GET value I need using Javascript, I'm going to use PHP to place it in the Javascript on the server side. I know it works and it only takes a few lines of PHP code to grab and validate what I need.
That said, the code is below and I'll also provide a link to the page so you can see what's going on.
jQuery(".btn-continue").attr("href") = "\"" + decodeURIComponent(
<?php
if(isset($_GET["outlink"])){
echo $_GET["outlink"];
}else{
echo "/";
}
?>
);
This is the page that has the code in action: http://radboxstudio.com/you-are-now-leaving-radboxstudio-com.html?outlink=http://google.com/
Clicking the first link to Google on this page should take you to the page above: http://radboxstudio.com/
There's a bit of Javascript that is supposed to encode the link's href and pass it on using get. I've included the relevant code below in case I screwed up there. currentDomain is defined in an earlier bit of code and I know that works as it is supposed to.
$('a').each(function() {
var $a = jQuery(this);
if ($a.get(0).hostname && getDomain($a.get(0).hostname) != currentDomain) {
$a.click(function(event) {
if (!confirmed) {
event.preventDefault();
event.stopPropagation();
var url = encodeURIComponent($a.get(0));
window.location = "http://radboxstudio.com/index.php?option=com_content&view=article&id=10&outlink=" + url;
}
});
}
});
To set the value of href by using the attr() function of jquery you should use it like this:
$("selector").attr("href", "value_you_want");
Checkout jquery api: http://api.jquery.com/attr/
Turns out the PHP was doing the work for me. The code below works.
<?php if(isset($_GET["outlink"])){
$outlink = $_GET["outlink"];
}else{
$outlink = "/";
}
?>
Linky
Sorry for the trouble. :D
I am trying to execute a js action that automatically sends informations to the URL for future use with php. To do so, I added a "onload" event on the window object and modified the URL in the listener. This is what it looks like so far:
window.addEventListener("load", function() {
window.location = "?test=test";
}, false);
When I load the page, the URL changes, but this is repeated over and over until the browser crashes. I was just wondering if there was a way to only execute it once.
If you don't want to use AJAX, and don't want to write JS functions to parse query strings, why not a simple:
<?php if(!isset($_GET['test'])): ?>
<script>
window.addEventListener("load", function() {
window.location = "?test=test";
}, false);
</script>
<?php else: ?>
<!--No JS written on the page, so no redirect -->
<?php endif;?>
But you really should look into AJAX, try using some JS framework like jQuery to help you in the process: http://api.jquery.com/jquery.ajax/
Check to see if you're already ON that page, before redirecting.
Add a simple flag on the URL like so: window.location = "?test=test&second=true";
As suggested in How can I get query string values in JavaScript?
Write a function to check get the URL params in JS GetQueryStringParams()
Then as suggested by others pass a send param in while reload
window.location = "?test=test&second=true";
Use the function in JS to check if you have a URL query string second and if its not there then reload the page
if you want to pass variables do this:
<?php
if(!isset($_GET['page'])){ //this line check if the variable field is available
$a = "Blank";
}else{
$a = $_GET['page'];
}
?>
that is it.
You could just erase the onLoad method once it's run, like so:
<script>
function doLoadStuff()
{
doSomeStuff();
//Clear onLoad contents
body.onLoad = function() {};
}
</script>
<body onLoad="doLoadStuff();">MyContent</body>