I am using this Plugin to add a marker to a image but they are in php echo formats but i want to get them as java script alert method or as a div to print using JavaScript ..
Code :
<script type="text/javascript">
$(document).ready(function() {
$('#map').dropPin('showPin',{
fixedHeight:495,
fixedWidth:700,
backgroundImage: '/images/{some-image.jpg}',
pin: '/image/{custom-pin-graphic.png}'
pinX: <?php echo {pinXcoord} ?>,
pinY: <?php echo {pinYcoord} ?>
});
});
</script>
i try this
<script type="text/javascript">
$(document).ready(function() {
$('#map').dropPin('showPin',{
fixedHeight:495,
fixedWidth:700,
backgroundImage: '/images/{some-image.jpg}',
pin: '/image/{custom-pin-graphic.png}'
pinX: alert(pinXcoord),
pinY: alert(pinYcoord)
});
});
</script>
but i can"t get the value
You can try this:
<script type="text/javascript">
$(document).ready(function() {
var x = <?php echo {pinXcoord} ?>;
var y = <?php echo {pinXcoord} ?>;
$('#map').dropPin('showPin',{
fixedHeight:495,
fixedWidth:700,
backgroundImage: '/images/{some-image.jpg}',
pin: '/image/{custom-pin-graphic.png}'
pinX: x,
pinY: y
});
alert(x+" "+y);
});
</script>
You can try this, assuming that you have the php variables($pinXcoord, $pinYcoord) defined in the same page.
<script type="text/javascript">
$(document).ready(function() {
$('#map').dropPin('showPin',{
fixedHeight:495,
fixedWidth:700,
backgroundImage: '/images/{some-image.jpg}',
pin: '/image/{custom-pin-graphic.png}'
pinX: '<?php echo $pinXcoord; ?>',
pinY: '<?php echo $pinYcoord ?>'
});
});
</script>
Related
In my PHP, I have:
$welcome = "Welcome!";
echo '<script type="text/javascript">addName();</script>';
And my HTML/script:
<a id="franBTN"></a>
<script type="text/javascript">
function addName(){
var fran = "<?php echo $welcome ?>";
$("#franBTN").text(fran);
}
</script>
When I run my PHP, everything else works but my element with ID "franBTN" does not change to say "Welcome!". Am I doing something wrong, or is this even achievable with PHP/JS?
In this first example var fran will become var fran = "Welcome!"; but when you will call addName() will not work because you declare it after..
<?php
$welcome = "Welcome!";
echo '<script type="text/javascript">addName();</script>';
?>
<a id="franBTN"></a>
<script type="text/javascript">
function addName(){
var fran = "<?php echo $welcome ?>";
$("#franBTN").text(fran);
}
</script>
In this second example var fran will become var fran = "";, addName() will work but it will return an empty string.
<a id="franBTN"></a>
<script type="text/javascript">
function addName(){
var fran = "<?php echo $welcome ?>";
$("#franBTN").text(fran);
}
</script>
<?php
$welcome = "Welcome!";
echo '<script type="text/javascript">addName();</script>';
?>
This is what you want:
<?php $welcome = "Welcome!"; ?>
<a id="franBTN"></a>
<script type="text/javascript">
function addName(){
var fran = "<?php echo $welcome ?>";
$("#franBTN").text(fran);
}
</script>
<?php echo '<script type="text/javascript">addName();</script>'; ?>
In this code you are calling function before it is declared. You should call function on DOM ready
Php Code:
$welcome = "Welcome!";
echo '<script type="text/javascript">$(function() {addName();});</script>';
Html/Script:
<a id="franBTN"></a>
<script type="text/javascript">
function addName() {
var fran = "<?php echo $welcome ?>";
$("#franBTN").text(fran);
}
</script>
I'm having a problem trying to use a PHP variable within JavaScript. I keep getting the following message.
"Invalid or unexpected token. Message: Undefined variable: example."
I'm unsure why example is being undefined, as it is defined within the php code. Here is my code:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = "<?php echo json_encode($example); ?>";
</script>
Does anyone have any suggestions? I have also tried the following javascript that results in the same problem:
<script type="text/javascript">
var php_var = "<?php echo $example; ?>";
</script>
Firstly, your original code has a syntax error: $example = '2' needs a semicolon.
Secondly, the next piece of code is just assigning the string <?php echo $example; ?> to the JavaScript variable php_var where the $example PHP variable is first substituted. The $example variable should be initiated properly first, however, for this to work.
As a separate note: JS cannot execute PHP directly -- only a PHP server can do so. What you're most likely trying to do is this:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = '<?php echo $example ;?>';
</script>
This should work, use single quotes
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = '<?php echo $example; ?>';
</script>
Tried and working both variant:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = <?php echo json_encode($example); ?>;
console.log(php_var);
</script>
<script type="text/javascript">
var php_va = "<?php echo $example; ?>";
console.log(php_va);
</script>
I want to be able to echo a PHP link inside a JavaScript string, i.e. <?php echo SITE_URL ?>. How can I do this with the code below?
<script type="text/javascript">
$.backstretch("http://www.example.com/bg.jpg");
</script>
<script type="text/javascript">
$.backstretch('<?php echo str_replace(array("\\", "'"), array("\\\\", "\\'"), SITE_URL); ?>');
</script>
I also made sure to escape ' and \ to avoid any problems.
you can do
<script>
var test= '<?php echo SITE_URL; ?>';// and use it as you please
</script>
This code works just fine in Chrome & Firefox - but fails in IE & Safari.
<script type="text/javascript">
$(document).ready(function() {
alert('1');
<?php foreach($options as $option): ?>
<?php if($option['option_choice'] == 1): ?>
var choiceid = <?php echo $option['product_option_id']; ?>;
<?php foreach ($option['option_value'] as $option_value): ?>
<?php if($option_value['whatsize'] == 2): ?>
var choicebigid = <?php echo $option_value['product_option_value_id']; ?>;
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
alert('2');
$('#'+choiceid+''+2).hide();
});
</script>
Alert 1 +2 shows in Chrome/FF, but only Alert 1 shows in IE.
---Generated Chrome JS---
<script type="text/javascript">
$(document).ready(function() {
alert('1');
var choiceid = '321'; //Produktvalg ID
var choicebigid = '1111';
$('#'+choiceid+''+2).hide();
document.getElementById(choiceid+''+2).style.display = 'none';
alert(choiceid);
});
</script>
---Generated IE JS---
<script type="text/javascript">
$(document).ready(function() {
alert('1');
var choiceid = '321'; //Produktvalg ID
var choicebigid = '1111';
$('#'+choiceid+''+2).hide();
document.getElementById(choiceid+''+2).style.display = 'none';
alert(choiceid);
});
</script>
---Update---
Adding single quotes seemed to help generating the IE JS code - before it was empty.
This also added the style="display:none;"
New problem is that hiding an option is not supported in IE and Safari. Will disable the option along hiding it as well.
---Status---
Fixed. End problem seemed to be hiding and
Try with single quote
var choiceid = '<?php echo $option['product_option_id']; ?>';
var choicebigid = '<?php echo $option_value['product_option_value_id']; ?>'
How can I echo this javascript if the php error messages is called? I have an error message setting that when a user misses his username or password it triggers an error message. The php error message is called by a php code. Here is the code:
<?php echo showmessage($msg) ?>
I have an alert message in javascript that when called it will make a javascript css pop up alert box. IF the javascript code is present it will show the alert box right away after reload. Here is code:
<script type="text/javascript">
$(document).ready(function () {
jqxAlert.alert('Alert Message');
})
</script>
How can I incorporate so that when the php echo message comes up it will trigger the javscript alert message. I was trying an if in php, so something like this code:
if ( showmessage($msg) ) {
<script type="text/javascript">
$(document).ready(function () {
jqxAlert.alert('Alert Message');
})
</script>
}
How can I echo my javascript message on the php call?
This could be written like this:
endif
endif-stackoverflow
<?php if (showmessage($msg)): ?>
<script>
$(document).ready(function (){
jqxAlert.alert('Alert Message');
});
</script>
<?php endif; ?>
Or like this:
<?php if (showmessage($msg)) { ?>
<script>
$(document).ready(function (){
jqxAlert.alert('Alert Message');
});
</script>
<?php } ?>
Or like this:
<?php
if (showmessage($msg)) {
echo
'
<script>
$(document).ready(function (){
jqxAlert.alert(\'Alert Message\');
});
</script>
';
}
?>
short tags
short hand comparison (ternary)
ternary 2
Or even this (probably not the best idea though):
<?= (showmessage($msg)) ? '<script>$(document).ready(function (){jqxAlert.alert(\'Alert Message\');});</script>' : ""; ?>
Alternatively, if you mean you would like to put the error message in that alertbox
<?php
if (showmessage($msg)) {
echo
'
<script>
$(document).ready(function (){
jqxAlert.alert('.showmessage($msg).');
});
</script>
';
}
?>
and again in the first style:
<?php if (showmessage($msg)): ?>
<script>
$(document).ready(function (){
jqxAlert.alert(<?= showmessage($msg) ?>);
});
</script>
<?php endif; ?>
something like this.. close your php tag before starting to write javascript..
<?php if ( showmessage($msg) ) { ?>
<script type="text/javascript">
alert('Alert Message');
</script>
<?php } ?>
Can you try this
<?PHP if ( showmessage($msg) ) { ?>
<script type="text/javascript">
$(document).ready(function () {
jqxAlert.alert('Alert Message');
});
</script>
<?PHP } ?>