How to use php-function-output as input for javascript? - javascript

I have a php-function trans to translate text. How to input trans-output in javascript?
<script>
function fregister_submit(f) {
if (!f.agree.checked) {
alert("<?=trans('회원가입약관의 내용에 동의하셔야 회원가입 하실 수 있습니다.')?>");
f.agree.focus();
return false;
}
if (!f.agree2.checked) {
alert("개인정보처리방침안내의 내용에 동의하셔야 회원가입 하실 수 있습니다.");
f.agree2.focus();
return false;
}
return true;
}

Try this:
alert("<? echo trans('회원가입약관의 내용에 동의하셔야 회원가입 하실 수 있습니다.'); ?>");

Converting php variables to be readable for javascript can be done like this:
<?php echo json_encode($var); ?>;
Everything else a simple echo does the job

Related

Echo PHP code inside Javascript

I am having problem getting this to work. I am trying to echo some php code to html if the page is in an iframe. Below is a short example of the php code i try to echo (the real code i try to echo is much longer)...
<?php $pic='https://cdn.pixabay.com/photo/2015/06/19/17/58/sample-815141_960_720.jpg'; ?>
<div id="frameid"></div>
<script type="text/javascript">
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
if (inIframe()) {
var node = document.getElementById('frameid');
node.innerHTML('<?php echo '<img width="100%" src="'.$pic.'"></img>'; ?>');
}
</script>
Seems there was some quotes issue. You can try the below code:
<div id="frameid"></div>
<script type="text/javascript">
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
if (inIframe()) {
var node = document.getElementById('frameid');
node.innerHTML(<?php echo "...something involving html and php variables...."; ?>);
}
</script>
Can you try this?
var pic = ;
innerHTML("");
I think is more clear to separate js from php, and recomanded
You are having troubles with single quote, you may consider this function (taken from WP https://developer.wordpress.org/reference/functions/_wp_specialchars/) to escape quote (single and double)
node.innerHTML('<?php echo esc_attr('<img width="100%" src="'.$pic.'"></img>'); ?>');
where you can define
function esc_attr( $text ) {
return _wp_specialchars( $safe_text, ENT_QUOTES );
}

Best way to separate PHP and Javascript code

Is there a best way to separate javascript and php code. for example:
//php code
if(condition1)
{
?>
// javascript code
$card_typej = $(this).val().trim();
if(condition2)
{
$("#id1").html('<?php echo($var1); ?>');
}
else
{
$("#id1").html('<?php echo($var2); ?>');
}
<?php
}
?>
If yes then, how to separate the above code?
First store required PHP variables in Javascript vars and then manipulate it.
<script>
var var2 = <?php echo json_encode($var2); ?>;
var var1 = <?php echo json_encode($var1); ?>;
if(condition1)
{
?>
// javascript code
$card_typej = $(this).val().trim();
if(condition2)
{
$("#id1").html(var1);
}
else
{
$("#id1").html(var2);
}
<?php
}
?>
</script>
You can see, complicated php+js code mixture is not there now.

PHP isset variable than jquery and javascript should work

I want to know if my php variable does not exists, then I want to execute my javascript.
For example-
<?php
if(!isset($_REQUEST['myVar'])){
?>
<script>
alert('variable not exist');
</script>
<?php
}
?>
Is this right way to use javascript code in php extension file
I know all other answers solve your issue but i prefer it to do this way
<script>
<?php
$isset = !isset($_POST['myVar']) ? 'true' : 'false';
echo "var isset = $isset;";
?>
if(isset) {
alert('variable not exist');
}
</script>
when php render your page it will give this output
<script>
var isset = true;
if(isset) {
alert('variable not exist');
}
</script>
Do it like this and it will work:
if (!isset($_REQUEST['myVar'])) {
echo "<script>alert('variable not exist');</script>";
}
you can try writing this piece of code where you want the script to be placed:
<?php
if (!isset($_REQUEST['myVar'])) {
echo '<script>
alert("variable not exist");
</script>';
}
?>

How to know if a PHP variable is being retrieved by JavaScript?

I'm having some trouble in JS function for combobox. It functions well if there is a PHP variable being passed, but if there's nothing, the whole block of code doesn't work. I've tried using if(typeof(<?php echo json_encode($information['day']) ?>)!='undefined') but it still doesn't work. Is there another way on how to determine if the PHP variable is set or passed?
There are more ways to do this than I can think of. Here is one.
<script>
<?php if( isset($information) && isset($information['day']) ) { ?>
var myJson = <?php echo json_encode($information); ?>;
<?php } else { ?>
var myJson = null;
<?php } ?>
if(myJson != null) {
// do something
}
</script>
Change this line of code
if(typeof(<?php echo json_encode($information['day']) ?>)!='undefined')
to this
if(typeof("<?php echo json_encode($information['day']); ?>") != 'undefined')

Two onclick events with a PHP if and else statement - in one button

I have the above below already functional on my website. It works alright but right now, I want to create a kind of promo and if the display meets the criteria of the promo, I want it to display onclick $htx
<?php
echo "<a href='javascript:;' onclick='pkgsPopup('http://'.$hLnk');' rel='nofollow'>";
?>
I have $htx pre defined to open a link $dealpath and if it does not meet that condition, I want it to open the default link - '$hLnk'
I have tried the code below and I had an error _ I mean the page will not load at all
if ($htx) { echo "onclick=\'miaPopup('http://$dealPth');\'' } else { echo 'onclick=\'pkgsPopup('http://$hLnk');\'' }";
I will really appreciate if someone can let me know how to do this without error using the PHP if/else statement.
Here is your second block of code a little cleaner. You were missing semi-colons in the PHP as well as matching escape characters ( a mix of apostrophes and quotes)
if ($htx)
{
echo "onclick=\'miaPopup('http://$dealPth');\'" ;
}
else
{
echo "onclick=\'pkgsPopup('http://$hLnk');\'" ;
}
The problem is that you have the echo pumping out a long string that makes no sense:
if ($htx) { echo "onclick=\'miaPopup('http://$dealPth');\'' } else { echo 'onclick=\'pkgsPopup('http://$hLnk');\'' }";
should read:
if ($htx) {
echo "onclick='miaPopup(\'http://$dealPth\');'";
} else {
echo "onclick='pkgsPopup(\'http://$hLnk\');'";
}
You could simplify by saying
if ($htx) {
$url = $dealPth;
} else {
$url = $hLnk;
}
echo "onclick='pkgsPopup(\'http://$url\');'";
Something like this should work (this code is not tested):
$onclick = ($htx ? 'miaPopup("http://'.$dealPth.'");'
: 'pkgsPopup("http://'.$hLnk.'");');
echo "<a href='javascript:;' onclick='$onclick' rel='nofollow'>";
try this
if ($htx){
$onclick="miaPopup('http://$dealPth');";
}
else{
$onclick="pkgsPopup('http://$hLnk');";
}";
echo "<a href='javascript:;' onclick='$onclick' rel='nofollow'>";
Why not use jQuery
<a class='one <?=($cond?"two" : "")?>'>Link</link>
<script>
$(function(){
$(".one").click(function(){
//Class of one
});
$(".two").click(function(){
//class of two
})
})
</script>

Categories