Executing Javascript with php and echo some vars - javascript

I am trying to execute a simple javascript file with if and else in php. So far it is working but i have an issue.. See the example bellow.
<?
$varone = "Popup One";
$vartwo = "Popup Two";
if ($_GET['myrequest'] == "")
{
echo <<<html
<script>
alert($varone);
</script>
html;
}else{
echo <<<html
<script>
alert($vartwo);
</script>
html;
}
?>
So i want to pass my php variables inside each alert! Or is there any other simply way to execute my javascript and pass my php variables in javascript?
Thank you!!!

Try
<?php
$varone = "Popup One";
$vartwo = "Popup Two";
if ($_GET['myrequest'] == "")
{
echo " <script>
alert('".$varone."');
</script>";
}else{
echo "<script>
alert('".$vartwo."');
</script> ";
}
?>

To Keep separate your JavaScript and PHP try this
<?php
$varone = "Popup One";
$vartwo = "Popup Two";
if ($_GET ['myrequest'] == "") {
?>
<script>
alert('<?php echo $varone; ?>');
</script>
<?php
} else {
?>
<script>
alert('<?php echo $vartwo; ?>');
</script>
<?php
}
?>

I think you are just missing quotes around your variables inside the alerts.
Try this:
<?
$varone = "Popup One";
$vartwo = "Popup Two";
if ($_GET['myrequest'] == "")
{
echo <<<html
<script>
alert('$varone');
</script>
html;
} else {
echo <<<html
<script>
alert('$vartwo');
</script>
html;
}
?>
You may have spotted this javascript error if you had looked in your browser's javascript console, which is very helpful for debugging. Google for how to open the javascript console in whichever web browser you use.

i don't know what you're trying to achieve but i think that if you want to pass some var from php to js the best approach would be use $.ajax , but this depend on what your trying to do or in this case maybe something like this should do the tricks.
<?php
if($_GET['myrequest'] == ""){
$alertVar = "popupone";
}
else{
$alertVar = "popuptwo";
}
?>
<script>
alert('<?php echo $alertVar ?>');
</script>

You have few choices as seen in the answers.
<?php
$varone = "Popup One";
$vartwo = "Popup Two";
?>
<script>
<?php if ($_GET['myrequest'] == ""):?>
alert('<?php echo $varone; ?>');
<?php else: ?>
alert('<?php echo $vartwo; ?>');
<?php endif; ?>
</script>

Related

Echoing a mulitple line message in alert function

I need to echo multiple line error in JS alert and I am using below code
Working code
$str = "This is a error1\\n";
$alert = $str."This is error2";
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
but I need to work on an old written code so this has addslashes function before echo alert like below:
$str = "This is a error1\\n";
$alert = $str."This is error2";
$alert = addslashes($alert);
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
Output : This is a error1\nThis is error2
I need this to print in two lines I have also tried with only \n but it is not working then.
I'd make javascript do the hard work.
<script type="text/javascript">
var alertArr = ["<?php echo $string1; ?>", "<?php echo $string2; ?>"];
alert(alertArr.join("\n"));
</script>
https://jsfiddle.net/0uwmmm3n/
(Magnificent random url by the way)
EDIT: less messy version, maybe more legitimate
<?php $alertArr = [$string1, $string2]; ?>
<script type="text/javascript">
var alertArr = <?php echo json_encode($alertArr); ?>;
alert(alertArr.join("\n"));
</script>
RE-EDIT: nah, too much work for a job so simple, two conversions is way too much
$str = "This is a error1\\n";
$alert = $str."This is error2";
$alert = addslashes($alert);
if(!empty($alert)){
?>
<script type="text/javascript">
alert('<?php echo $alert;?>');
</script>
<?php
}
I have used this code and it working fine according to your need
if still issue continues then clear your cache

Reading session var in Javascript

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>

PHP variable with a value of string is not equal in PHP variable with a value of html id from javascript?

This is my example code
<script>
var item = "yooow";
jQuery("#view").html(item);
</script>
<?php $str_html = '<p id="view"></p>';
echo $str_html; //it will output "yooow"
?>
but...
when i'm trying to compare this into another php variable
<?php $str_php ="yooow";
if($str_html == $str_php)
{
echo "true";
}
else
{
echo "false";
}
?>
but it returns to false
how can they be an equal?
PHP is run serverside which means it's not aware of any changes you make in the DOM using Javascript.
<script>
var item = "yooow";
Value set only in clinet side, PHP don't know about changes here
jQuery("#view").html(item);
</script>
<?php $str_html = '<p id="view"></p>';
WRONG, it will output "<p id="view"></p>"
echo $str_html; //it will output "yooow" ?>
<?php $str_php ="yooow";
Comparing "<p id="view"></p>" and "yoow" and that is not equal!
if($str_html == $str_php)
Better, you give the item value in cookie/session. And, compare the cookie value with $str_php.
<?php session_start(); ?>
<script>
$(document).ready(function(){
var item = "<?php echo $_SESSION['html']="yooow"; ?>";
$("#view").html(item);
});
</script>
<?php
$str_html = '<p id="view"></p>';
$strHtml=$_SESSION['html'];
$str_php ="yooow";
if($strHtml == $str_php)
{
echo "true";
}
else
{
echo "false";
}
?>

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 alert PHP value in JavaScript?

<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; ?>

Categories