I am trying to alert message but it's not working with my string.
echo "<script>alert('$lang['ALERT']');
window.location.href ='index.php';
</script>";
Not working... Help
You're mixing quotes. Keep it simple,
echo "<script>alert('".$lang['ALERT']."');
window.location.href ='index.php';
</script>";
try it.
<script>
var alertMsg = "<?php echo $lang['ALERT'] ?>";
alert(alertMsg);
window.location.href ='index.php';
</script>;
You're not properly quoting your variable
<?php
$lang['ALERT'] = 'mystring';
echo "<script>alert('".$lang['ALERT']."');window.location.href ='index.php';</script>";
?>
Here's the eval
Try this one
<script>
alert(<?=$lang['ALERT']; ?>);
window.location.href ='index.php';
</script>
quotes is the problem
<?php $alert=$lang['ALERT'];?>
<script>
alert('<?php echo $alert?>');
window.location.href ='index.php';
</script>;
Related
I was working on project in which I have to get some text from the database and display it in the text area. Working fine with no line breaks and tabs. but when a line breaks occurs it gives the error
Uncaught SyntaxError: Invalid or unexpected token
My java script code is as :
<script type="application/javascript">
$(document).ready(function () {
document.getElementById('job_type').value = "<?php echo set_value('job_type') ?>";
document.getElementById('job_cat').value = "<?php echo set_value('job_cat') ?>";
if("<?php if(validation_errors() == false) echo "false"; else echo "true"; ?>"=="false")
{
$('#job_title').val("<?php echo $job[0]->job_title ?>");
$('#job_type').val("<?php echo $job[0]->job_type ?>");
$('#job_cat').val("<?php echo $job[0]->job_cat ?>");
$('#salary').val("<?php echo $job[0]->salery ?>");
$('#job_descp').text("<?php echo $job[0]->job_desc ?>");//error here
}
});
});
</script>
The text it was trying to set was
Error details are shown in images
Debug info
I have tried using .html() and .val() too but nothing worked. How can I handle it.
This has already been answered here https://stackoverflow.com/a/4270709/3004335
However, you can use json_encode
<script type="application/javascript">
$(document).ready(function () {
document.getElementById('job_type').value = "<?php echo set_value('job_type') ?>";
document.getElementById('job_cat').value = "<?php echo set_value('job_cat') ?>";
if("<?php if(validation_errors() == false) echo "false"; else echo "true"; ?>"=="false")
{
$('#job_title').val(<?php echo json_encode($job[0]->job_title); ?>);
$('#job_type').val(<?php echo json_encode($job[0]->job_type); ?>);
$('#job_cat').val(<?php echo json_encode($job[0]->job_cat); ?>);
$('#salary').val(<?php echo json_encode($job[0]->salery); ?>);
$('#job_descp').text(<?php echo json_encode($job[0]->job_desc); ?>);//error here
}
});
});
</script>
You do however need to be careful about cross site scripting
See here https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet in particular rule #0
Remove line break(CR LF) in job[0]->job_desc
like this:
$('#job_descp').text("<?php echo str_replace (array("\r\n", "\n", "\r"), '<br>', $job[0]->job_desc);?>");
echo '<script type="text/javascript">
$(document).ready(function(){
$("video").easyWebPlayer({ playlist:\''.json_encode($playlist).'\'
});
});
</script>';
I am using the above code in a php file it produces following output.
'playlist:'[{"id":"0","url":"\/26beb276\/26beb276_0.mp4","thumb":"\/26beb276\/26beb276_0.jpg","title":"big buck test"}]'});});
It gives me error of Unexpected identifier .
How can I avoid this error.
Try below code:
<Script>
var playlist = '<?php echo $playlist; ?>';
var jsonplaylist = JSON.parse(playlist);
</Script>
echo '<script type="text/javascript">
$(document).ready(function(){ $("video").easyWebPlayer({ playlist:'+jsonplaylist+'});
</script>';
solved
I used playlist:"'.str_replace("\"","\'",json_encode($playlist)).'" while json_encode()
and use
var jsonobj=JSON.parse(Playlist.replace(/\'/g, "\""));
at the receiver end.
I had the same console error, the error was due to quotes
Before
<script>window.lang_arr="<?php echo json_encode($lang_arr);?></script>
Fix
<script>window.lang_arr=<?php echo json_encode($lang_arr);?></script>
It could help someone today :_)
I understand that this question has been asked a lot of of times, but the solutions posted there don't seem to work for me. I have a code as follows:
<script>
var JSvar = "<?php echo $phpVar ?>";
document.write(JSvar);
</script>
<?php
$phpVar="jajha";
?>
I actually want to pass a PHP variable to a JS function, but I wanted to try if printing the variable works first. Please help me out.
instead of
<script>
var JSvar = "<?php echo $phpVar ?>";
document.write(JSvar);
</script>
<?php
$phpVar="jajha";
?>
try
<?php
$phpVar="jajha";
?>
<script>
var JSvar = "<?php echo $phpVar ?>";
document.write(JSvar);
</script>
How can I read the session data set in PHP from Javascript. I tried the following code, but it doesn't work.
<?php
session_start();
$_SESSION['test'] ="orange";
?>
<script>
var username = '<%= Session["test"] %>';
alert(username );
</script>
Thanks for your help.
Just echo it out:
<?php
session_start();
$_SESSION['test'] ="orange";
?>
<script>
var username = '<?php echo $_SESSION["test"]; ?>';
alert(username);
</script>
BTW, you used ASP.NET inline expression tags. You can use <?= ?> for PHP if you have short tags enabled.
use:
<script>
<?php echo "var username = '".$_SESSION["test"]."';";
alert (username);
</script>
Following are your mistakes
You have written Session["test"] instead of $_SESSION['test']
you are using % symbol instead of ?
Try this:
<?php
session_start();
$_SESSION['test'] ="orange";
?>
<script>
var username = '<?= $_SESSION["test"] ?>';
alert(username );
</script>
<script type="text/javascript">
var jvalue = 'this is javascript value';
<?php
$abc = "<script>document.write(jvalue)</script>" ?>
</script>
<?php echo 'php_'.$abc; ?>
<script type="text/javascript">
var test = "<?php echo $abc; ?>";
</script>
<?php
echo '<script language="javascript">';
echo 'alert(test)';
echo '</script>';
?>
How to alert test variable, which contains PHP value?
I would like to get the PHP value in javascript, for further execution in my project.
try this code
<?php
echo '<script language="javascript">alert("'.$test.'"); </script>';
?>
this is updated code for you using javascript and PHP
<?php
$user = "rebel";
echo '<script> var name = "'.$user.'";
alert(name);</script>';
?>
This is the third code for you if this does not work then you have other problem
<?php
$abc= "Hello";
?>
<script type="text/javascript">
var test="<?php echo $abc; ?>";
</script>
<?php
echo '<script language="javascript">';
echo 'alert(test)';
echo '</script>';
?>
why are you assigning the variable to a javascript variable and then echoing that? sounds like extra work to me...
echo 'alert("' . $abc . '");';
Your code is not so clear, i think you want something like this:
<?php
$test = "hi there!";
echo '<script type="text/javascript">';
echo 'alert("'.$test.'")';
echo '</script>';
?>
var test="<?php echo $abc; ?>"
<script language="javascript">alert(test); </script>
If you want to alert the value of $abc, you need to escape the slash in the following line with a backslash, like this.
<?php $abc = "<script>document.write(jvalue)<\/script>" ?>
You also need to remove this line (or escape the < and the > in $abc). If you don't, the script tags will mess up the rendered html code.
<?php echo 'php_'.$abc; ?>