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
Related
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
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>';
In my HTML-page I have a form with action="register.php" and a <p id="slogan">. I want the PHP-code in register.php to change the value of the <p> by echoing some Javascript.
However, the paragraph is not changing in my HTML-page and I do not understand why.
This is what my simplified HTML-page contains:
<body>
<p id="slogan"> hello </p>
<form action="../scripts/register.php" method="post">
...
</form>
</body>
This is what my simplified register.php contains:
<?php
...
if (mysqli_query($conn, $sql)) {
header("Location: http://www.google.com");
echo "<script>
document.getElementById('slogan').innerHTML = 'Character successfully introduced.';
</script>";
sleep(3);
}
?>
The echoed JavaScript is supposed to change "hello" to "Character successfully created.".
The reason behind sleep(3) is to wait three seconds so that you have time to notice the updated paragraph before getting redirected to Google.
This is a bad implementation in a couple of ways (hear me out).
Firstly I'd suggest not injecting javascript to the page. Why? Because in order to get that javascript to show, you're relying on two factors:
Javascript to be enabled on the client's browser.
PHP to sleep while that occurs.
It might seem like two tiny points but every time you send PHP to sleep that is effectively a blocker - while that happens, nothing else.
Also, slight flaw in your code if I've picked up your theory correctly - it seems you want to inject a success message in the "main script" page rather than the intermediary register.php page. If that's the case, it'll never get executed. If I've picked you up wrongly, it's worth adding more of your code to the question to clarify what exactly is going on.
Alternative approach
My suggestion would be to do something like the following:
register.php
if($your_var == 1) {
header('Location: youroriginalscript.php');
$_SESSION['yoursessionvar'] = 'Character successfully created.';
}
youroriginalscript.php
... (beside your slogan HTML entity) ...
<div id="slogan">
<?php
if(isset($_SESSION['yoursessionvar'])){
echo $_SESSION;
}
?>
</div>
This is by no means perfect but it should give you an idea to get started.
Your original script also assumes that the character creation is always successful. This might not be the case and should be double checked before giving clients misleading feedback. Check it, make sure it's correct, and never assume it stays the same between page hops!
Tip: If you ever try and get PHP to sleep or do some crazy stuff, it'll always, always, always create a bottleneck.
Edit
Okay, from your edited question it seems you're getting PHP/Javascript mixed up a little bit. Here's another answer I wrote a while back explaining the difference but there are literally millions of others out there:
With
header("Location: someURL");
you are redirecting to another page (and very likely throwing an error because "someURL" might not exist) before the script is actually sent to the browser.
The echo statement is being buffered. You have to configure your script to avoid buffering. Take a look at this: How to disable output buffering in PHP
Try this:
header("Location: someURL");
#ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++)
ob_end_flush();
ob_implicit_flush(1);
echo "<script>
document.getElementById('slogan').innerHTML = 'Character successfully created.';
</script>";
sleep(3);
The problem is that since PHP is back-end and JavaScript is front-end, the whole PHP-script has to finish before any JavaScript is executed. This means that the sleep(3) happens before the execution of the JavaScript and totally kills its purpose. All it does is to waste three seconds of your time.
If you want to output a message and also redirect, you need to come up with other ways of doing it. However, that is another topic.
i'm trying to refresh page every 3 second, the url page change with $_GET variable.
i'm trying to save $_GET var into session and cookie, but get error header has already sent.
how to change url after page reload ?
here my script :
Index.php
<?php
session_start();
$skill =$_SESSION['skill'];
?>
<script type="text/javascript">
var auto_refresh = setInterval(function () {
$('#src2').load('monitor.php?skill=<?php echo $skill;?>').fadeIn("slow");
}, 3000);
</script>
monitor.php
<?php
include "conn.php";
session_start();
$_SESSION['skill'] = $_GET['skill'];
if ($_SESSION['skill']=='')
{
$a ="bro";
$_SESSION['skill']=4;}
elseif ($_SESSION['skill']==4){
$a = "yo";
$_SESSION['skill']='5';
}
elseif ($_SESSION['skill']==5){
$a = "soo";
}
?>
First off, "headers already sent" means that whichever file is triggering that error (read the rest of the error message) has some output. The most common culprit is a space at the start of the file, before the <?php tag, but check for echo and other output keywords. Headers (including setting cookies) must be sent before any output.
From here on, this answer covers how you can implement the "refresh the page" part of the question. The code you provided doesn't really show how you do it right now, so this is all just how I'd recommend going about it.
Secondly, for refreshing the page, you will need to echo something at the end of monitor.php which your JS checks for. The easy way is to just echo a JS refresh:
echo '<script>window.location.reload();</script>';
but it's better to output some JSON which your index.php then checks for:
// monitor.php
echo json_encode(array('reload' => true));
// index.php
$('#src2').load('monitor.php?skill=<?php echo $skill;?>', function(response) {
if (response.reload) window.location.reload();
}).fadeIn('slow');
One last note: you may find that response is just plain text inside the JS callback function - you may need to do this:
// index.php
$('#src2').load('monitor.php?skill=<?php echo $skill;?>', function(response) {
response = $.parseJSON( response ); // convert response to a JS object
if (response.reload) window.location.reload();
}).fadeIn('slow');
try putting
ob_start()
before
session_start()
on each page. This will solve your problem.
Without looking at the code where you are setting the session, I do think your problem is there. You need to start the session before sending any data out to the browser.
Take a look at: http://php.net/session_start
EDIT:
Sorry, a bit quick, could it be that you send some data to the browser in the 'conn.php' file? Like a new line at the end of the file?
I'm using CodeIgniter form helper, and this is what it does:
Uncaught SyntaxError: Unexpected token ILLEGAL
The code:
jQuery('#window-1').append('<?= form_dropdown('hfdata', $hf_arr, set_value('hfdata'), 'class="span3"') ?>');
As you can see, I'm using a PHP inside of a JS, when I do <?= 'Test' ?> it works.
So it seems like its related with the CodeIgniter function.
As far as i know this error message can be caused by unknown/wrong characters in the code, and from what I saw in the firebug, this CI function is generating text with tabs and line breaks... and that is my problem I guess.
I may be wrong, so please correct me if so.
I will appreciate any solution for this problem.
Chances are you're screwing up your quotes and you need to change the way you're passing that last parameter to the dropdown.
$class = 'class="span3"';
jQuery('#window-1').append('<?= form_dropdown("hfdata", $hf_arr, set_value("hfdata"), $class) ?>');
To explain how PHP and Javascript works: Php is executed on the server, this give html output. You browser will get that output and can do anything with it. From this point javascript can do his job.
You are echoing the php function call to the users browser. Javascript can't call the php function. If you want to do this, then you need to make a php file call that returnes the result you want. But I guess there is an other problem. ( if you want to do this, escape the ' characters)
You need to execute the PHP code on the server. This will give a result. You send this result to the client. On the clients PC javascript will execute your javascript code.
If you want to generate with PHP the javascript code, then you can do something like this ( in PHP on the server!):
jQuery('#window-1').append('<?= form_dropdown('hfdata', $hf_arr, set_value('hfdata')); ?> ');
If this isn't what you want, then you are working on a design problem. Then it's a kind of dead end.
I found the answer myself... As I said, it was caused by the HTML output that was returned from the from_dropdown.
The solution is simple, remove all unwanted characters like line breaks:
<?php
$prepare = preg_replace('/^\s+|\n|\r|\s+$/m', '', form_dropdown('hfdata', $hf_arr, set_value('hfdata'), 'class="span3"'));
?>
jQuery('#window-1').append('<?= $prepare ?>');