In /var/www/html/msg.txt is just one word "Test".
Can someone please tell me why this doesn't work?
echo "<script>alert('$tes');</script>";
Complet php code:
<?php
$ma="Test";
$tes = file_get_contents("/var/www/html/msg.txt");
echo "$tes"; //works
echo "<script>alert('$ma'); //works
</script>";
//but if this
echo "<script>alert('$tes'); // doesn't work!!!! Why?
</script>";
?>
how can I do it?
Most likely you have a line break in that file, so the resulting code is:
<script>alert('Test
');
</script>;
Which is wont work, you can confirm this by looking at the source, and/or it will be erroring out in the browser console.
The problem is with your file_get_contents. Probably you are setting an incorerect path, or the file that you are trying to access doesn't have the right permissions
Related
**This is my code **
I am sending copying data from a php variable to a JavaScript variable here
<?php
include('includes/config.php');
include('includes/classes/Artist.php');
include('includes/classes/Album.php');
include('includes/classes/Song.php');
if(isset($_SESSION['userLoggedIn'])){
$userLoggedIn = $_SESSION['userLoggedIn'];
echo "<script>userLoggedIn = '$userLoggedIn';</script>";
// echo "<script>console.log('$userLoggedIn')</script>";
}else{
header('location:register.php');
And if i use the echo statement in the if statement here
it cause this. see the left side navigation bar;
Error image
And if i don;t use the echo statement the page is fine looks like this
Without error
I can't access the picture you uploaded right now, but I saw an error
echo "<script> var userLoggedIn = '" . $userLoggedIn . "'; alert(userLoggedIn); </script>";
Change the part we print on the screen in this way, data will be displayed on the screen.
I used this code inside the body of the html part and it works fine
<?php
if($userLoggedIn){
echo "<script>userLoggedIn = '$userLoggedIn';</script>";
}
?>
I want to pass the php variable inside the window.location javascript this is my php code and i am unable to do that.
echo '<script>location.href = "reportConsumption.php?creategenReport="'.$genid.'"&sDate="'.$startdate.'"&eDate="'.$enddate;</script>';
try to set quotation marks
echo '<script>location.href = "reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate.'"</script>';
You are closing your quote in JS
echo '<script>location.href = "reportConsumption.php?creategenReport="'.$genid.'"&sDate="'.$startdate.'"&eDate="'.$enddate;</script>';
Should be
echo '<script>location.href = "reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate.'</script>';
This will cause an error in JS on the client side, you could see this by pressing f12 and looking in the console log in the browser debugger. Your source code will look like this
<script>location.href = "reportConsumption.php?creategenReport="35"&sDate="...
//where this is a quoted block
"reportConsumption.php?creategenReport="
//and this is just chilling in space
35
//and then a new quoted block, etc.
"&sDate="
And you had this other (php syntax error) issue I took the liberty of fixing.
.$enddate;</script>';
Just in PHP you can redirect with
header("Location: $url");
But you have to be sure of 2 things:
You do not output "Anything" not even a line return before calling header
You call exit(); immediately after it. If you don't PHP will continue to execute the current script after it executes the redirect. Which is probably not desirable.
You are closing the double quotes too early. It should be close at the end of the URL. So you have a syntax error in your JavaScript:
echo '<script>location.href = "reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate.'";</script>';
Or separate using a variable to be more clear:
$url = 'reportConsumption.php?creategenReport='.$genid.'&sDate='.$startdate.'&eDate='.$enddate;
echo '<script>location.href = "'.$url.'";</script>';
You should not use double quote around the values for GET param
echo '<script>location.href = "reportConsumption.php?creategenReport='.$genid.
'&sDate='.$startdate.'&eDate='. $enddate .'"';</script>';
I have the following code in a PHP page in ATOM:
<html>
<head>
<?php $testvar = "Test"; ?>
<script>
var test = <?php echo $testvar; ?>;
</script>
</head>
<body>
<p> This is a test. </p>
</body>
</html>
The page is doing exactly as planned on the PHP page but in Atom, everything after the </script> line is highlighting in red as in an error. What's going on here?
Here is a screenshot of the actual code I'm using. The above is an example but also has the same problem.
I have opened a Issue claim on the Atom support but I would like to try here as well to see if it's anything within the code.
Thanks!
Image
This is a bug in your code, not in Atom. Your echo statement produces an unenclosed string:
<script>
var test = Test;
</script>
This results in a JavaScript error ("Uncaught ReferenceError: Test is not defined") because you are telling the interpreter to use a variable named Test, but none exists.
You need to produce a properly enclosed string, which you can do with json_encode():
<script>
var test = <?php echo json_encode($testvar); ?>;
</script>
Based on your comment, you're actually trying to build an array, not just echo a string. You have this code:
var main_categories_array = [<?php echo '"'.implode('","', $main_categories_array ).'"' ?>];
If your array contains any quotation marks, like the simple "Test" example, you will get errors. So, your code should be:
var main_categories_array = <?php echo json_encode($main_categories_array); ?>;
That was an actual bug fixed in 1.13, released yesterday : 10th of January.
Sources :
https://github.com/atom/atom/issues/13532
http://blog.atom.io/2017/01/10/atom-1-13.html
This is a bug with Atom.
Posted here: https://github.com/atom/atom/issues/13532
I was able to close the window. But with the timeout, it does not seem to work.
This test.php was called by the submit button's action on another window. If I remarked out all window closing script lines, then this "Sending ... This window will close itself after sending." will show up.
This echo "<script>window.close();</script>"; will close this window without showing any echo. The other 3 lines, all I see is a blank window and not being closed at all. Only on the Chrome I got a Server 500 error. I tried on Firefox, Safari, and Chrome.
Any suggestions?
test.php contains:
<?php
echo "Sending ... This window will close itself after sending.";
echo "<script>window.close();</script>"; // this line works
// echo "<script>setTimeout("window.close()", 5000);</script>";
// <script type="text/javascript">setTimeout("window.close();", 3000);</script>
// echo "<script type="text/javascript">setTimeout( function() { window.close(); }, 3000);</script>"
?>
<script type="text/javascript">setTimeout("window.close();", 2000);</script>
Tell me if this works, of course change 2000 to what you want ;)
You should pass a function to setTimeout().
Try this:
<script>setTimeout(function(){ window.close();}, 5000);</script>
UPDATE on your update:
Error 500 means "Internal server error", there is something wrong with your script, not the resulting page.
I assume that you are not posting the real script, but there is something else wrong in it besides what you output.
Did you really wrote double quotes inside PHP string?
Because what you are really doing is writing a compile error, something like that is illegal PHP code:
echo "something "quoted" something";
I still suggest you to write a proper function and not a string, but if you must, at least escape the double quote, or use the single quote to start PHP constant string if you don't have to parse variables in it.
echo 'something "quoted" something';//I prefer this
or
echo "something \"quoted\" something";
//but this still works, altough parser will try to find variable names
I am getting the constant error that it is expecting an object.
I call a function onchange:
<select name =<?php echo $dagen[$i]."1"; ?> id=<?php echo $dagen[$i]."1"; ?> onchange="weekchange(this, <?php echo $i; ?>)" />
Here is my javascript function:
function weekchange(selected, weekkeuze)
{
var samen;
var id = weekkeuze;
var week = selected.value;
samen = "id="+id+"&week="+week;
alert(samen);
submit_javascript("GET","capaciteitberekening.php?"+samen,"","true"); //row 13
}
In this function I receive the data from the onchange correctly since it shows the correct values in the alert(samen), with the last sentence I should be sending this data to capaciteitberekening.php. here I am not sure what is going on. On capaciteitberekening.php I use this code to catch the value:
$filt['week'] = $_GET['week'];
$filt['id'] = $_GET['id'];
echo $id, $week;
But as you might expect I do not receive anything, instead i get an error on my browser saying that it is expecting a object in row 13.
I hope you can help me, since I have no clue how to solve my issue.
Thanks in advance.
There could be some issue with your php code at line 13.
To check if the values are posted correctly,
try this at the beginning of capaciteitberekening.php.
<?php
var_dump($_GET);
exit;
?>
If It works, then its sure that there could be some other issue with your php code in capaciteitberekening.php, but request is working fine.
thanks for reminding me about the function, I am working om someone else his code so I didn't look properly at that function. and I had to add a ID and not a class, sorry for making such a basic mistake, and thanks