Javascript wont run in PHP - php

I have this piece of code:
if(isset($_POST['btnSubmit']) && $_POST['btnSubmit'])
{
require_once($_SERVER['DOCUMENT_ROOT'] . 'database.php');
$derpCard = $card;
$derpAccessGroup = $_POST['tbAccessGroup'];
$derpComments = $_POST['tbComments'];
if(isset($_POST['cbActivated']))
$derpActive = $_POST['cbActivated'];
else
$derpActive = "DEACTIVATED";
$x = editCard($derpCard,$derpAccessGroup, $derpComments, $derpActive);
if($x)
{
$_SESSION['editcard'] = $derpCard;
$_SESSION['editgroup'] = $derpAccessGroup;
$_SESSION['editcomments'] = $derpComments;
$_SESSION['editstatus'] = $derpActive;
echo "<script>";
echo "alert(\"Done!\");";
echo "</script>";
}
echo "<script>location.reload(true);</script>";
}
Basically, editCard runs an SQL "UPDATE ... where..." to edit the content within the db. If this is sucessful, I want it to display an alert telling the user it's been updated, as well as refresh the page.
Both the alert and reload code do not run, and i've been trying any and all alternatives! If someone has any idea as to simply refresh the page (thats the minimum i need!) It would be greatly appreciated!

I have to apologize if this answer is too short but the question is too broad or is missing more info. I noticed that one of your lines is wrong.
require_once($_SERVER['DOCUMENT_ROOT'] . 'database.php');
should be:
require_once($_SERVER['DOCUMENT_ROOT'] . '/database.php');
There should be / in it since $_SERVER['DOCUMENT_ROOT'] returns something like this:
"C:/xampp/htdocs"
So if you are to concatenate that with "database.php", you'll be having
"C:/xampp/htdocsdatabase.php" instead of "C:/xampp/htdocs/database.php"
In any case, you should try using firebug or similar browser add-on to help you debug those javascript errors(if there are any).
I hope this helps.

Try to format the script echo like this:
echo "\n<script>\n<!--\n";
echo "alert(\"Done!\");";
echo "\n-->\n</script>\n";
and
echo "\n<script>\n<!--\nlocation.reload(true);\n-->\n</script>\n";
Note the new lines added.

You appear to be missing the type attribute for script.
If you want to specify javascript, you need to include the type.
echo "<script type=\"text/javascript\">";
echo "alert(\"Done!\")";
echo "</script>";
Same goes with the other line
echo "<script type=\"text/javascript\">location.reload(true);</script>";
If the above does not help in the slightest, the problem could be with your logic statements, or that your javascript may just not be outputting what you want.
There are tools to help you figure out these issues, such as the apache logs and firebug plugin
EDIT: Forgot missing semicolon

Related

check the value returned by array_rand?

I'm trying to get some PHP to check if a specific value is pulled out of an array, but I'm having trouble getting it to work. It actually causes the site to go black.
<?php
$message_array = file("http://www.example.com/wp-content/themes/mytheme/Subtitles.css");
$message = array_rand($message_array);
echo "$message_array[$message]";
$GfCheck = "<audio id='audio' src='example.com/wp-content/uploads/2016/01/example.wav'; preload='auto' ></audio><a onclick='GFFUNC()'><img src='example.com/wp-content/uploads/2016/01/Gf.png'; height='90px' alt='gf' title='gf'/></a>";
if ($message_array[$message] == $GfCheck) { $Gf = "1" } else { }
?>
the $Gf would then in turn add a secret section to a menu later on.
Can anyone help me figure out what's going wrong?
Thanks in advance for your help!
Answer largely came from Rizier123's comments on the main post.
While the file tag did not work, the code did in the end. I am left with an issue of global variables now, but the hard part is done!

How would I get an alarm using sessions when variable change? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Im trying to build a single page that gets content using "file_get_contents" function, I have managed this part. The content ends up in a variable called $content. The idea is to get an alert (by sound) when the content changes.
What I have faild at is the part where I compare the old value for $content with the new one. Somehow I must keep the old value and compare it with the new one and when I reload the page I would like to get an alert if $content has changed. I was thinking I should use sessions for this comparison.
This is what I got:
<?php
session_start();
$_SESSION['red'] = $red;
if($red != $content) {echo "ALERT" . $content = $_SESSION['red'];}
if($red == $content) {echo "Same";}
It is not working, probably looks like crap since I have no clue what im doing really :)
Below is the code for setting the $content variable:
$page = file_get_contents('data.htm');
if ( preg_match ( '/<a tabindex="50" class="item_link".*?a>/s', $page, $matches ) )
{
foreach ( $matches as $key => $content )
{
echo $key . ' => ' . $content . '<br /><br />';
}
}
else
{
echo 'No match';
}
?>
For the Alert sound I found a script;
<script>
var audio = document.createElement('audio');
document.body.appendChild(audio);
audio.src = 'http://rpg.hamsterrepublic.com/wiki-images/2/21/Collision8-Bit.ogg';
setInterval(function(){audio.play();}, 5000);
</script>
Need help activating this script if the $content has changed, also to make it only play once.
I commend your self-efacing attitude.:)
One problem, I think, is the way you've written your if statement(s):
if($red != $content) {echo "ALERT" . $content = $_SESSION['red'];}
if($red == $content) {echo "Same";}
Firstly, the '.' after echo "ALERT" should, I think be a ';'.
Secondly, use an else rather than the 2 if statements. Why write more code than you need to and (more importantly) the assignment in your first if makes your second if true!!
Also, I think you want to be making $red = $_SESSION['red'] at the start, then make $_SESSION['red'] = $content.
Finally, use strcmp() instead of == or != (strcmp() is more reliable for comparing strings - remember it returns false when the strings match though).
So, you end up with:
<?php
session_start();
$red=(array_key_exists('red',$_SESSION)?$_SESSION['red']:'');
... set up $content
if(strcmp("$red","$content") )
{ // what you've previously read doesn't match the new content
echo "ALERT";
// escape back to the html to do the sound stuff
?>
<script>
var audio = document.createElement('audio');
document.body.appendChild(audio);
audio.src = 'http://rpg.hamsterrepublic.com/wiki-images/2/21/Collision8-Bit.ogg';
setInterval(function(){audio.play();}, 5000);
</script>
<?php
$_SESSION['red'] = $content;
// so now they both match the new content
// - so will give "same" next time round
} else {
echo "Same";
}
...
Try this out.
First, you must consider that to re-run your PHP code, your page needs to refresh.
To achieve this, you may use the classic html refresh through meta tag (I suppose you want just get things done, otherwise you need some tutorials to understand better what you are doing).
Second, you can store the content of data.htm in a file on the server (it's not clever how data.htm is supposed to change, I imagine through FTP or something similar, if it should change through browser, notice that it's a completely different story and you have to follow some tutorials).
After you have done this, you can just compare file_get_contents result with the stored file and check if they are not the same. If they are not, print out your javascript code to have your page ring and replace the content of the file with the new fetched contents (otherwise will keep ringing).
No sessions in this way.

PHP counter file and page forwarding

So i'm writing this code so that you either get forwarded to a certain page if you're the first one to hit the link, or you are sent back to the original page after being displayed a message if you're not what beginner mistake am i making?
<?php
$count = file_get_contents('counter.txt');
$count = trim($count);
if ($count="0")
{
$count = $count + 1;
$fl = fopen("counter.txt","w+");
fwrite($fl,$count);
fclose($fl);
header("Location: newpage.html");
}
else
{
fclose($fl);
echo "Sorry but the item has already been sold out";
header("Location: oldpage.html");
}
?>
As for the delay, you can accomplish it two different ways. The first is to use PHP header (like you are currently doing), but change it to look like this:
<?php
header("refresh:5;url=oldpage.html");
echo "Sorry but the item has already been sold out";
?>
The other way is to echo out a piece of HTML code, the meta-refresh:
<?php
echo '<meta http-equiv="refresh" content="2;url=oldpage.html">';
echo "Sorry but the item has already been sold out";
?>
In both examples, 5 is the amount of seconds until the refresh. Experiment with each one to see if it will fit your needs.
This might be some sort of syntax that I'm not familiar with, but none of my scripts have ever had the
<? code
I simply use
<?
Also since you did not delay our header tag the user will not see the previously echoed statement above it. It will automatically redirect before the page has time to output fully.

PHP Session Function Debug Assist

I know how to include external PHP pages and how to start sessions etc, but I think there is something messed up with my logic on what I am working on. Hoping someone could take a look...
I have an html page that is a form that pulls up a PHP view page with the info it sends to it. I wanted to put my function in an external page, along with using sessions, but I keep getting a syntax error.
When I send my form it goes to the following:
<?php
session_start();
include 'functs.php';
if ($_POST && !empty($_POST['name'])) {
$_SESSION['name'] = $_POST['name'];
$_SESSION['time'] = $_POST['time'];
confirmed();
}
else {
print unconfirmed();
}
?>
My external page with the functions is this:
<?php
function confirmed() {
echo "<head>";
echo "<title>Confirmation Page</title>";
echo '</head>";
echo "<body>";
PRINT <<<HERE
if (isset($_SESSION['name'])) {
echo 'Thank you, '.$_SESSION['name']. ' your reservation is confirmed for ' . $_SESSION['time'] ;
}
else {
echo 'There seems to have been an error processing your reservation. Please ensure that you have cookies enabled and try your request again' ;
}
HERE;
echo "</body></html>";
?>
The error I am getting is Parse error: syntax error, unexpected 'name' (T_STRING), expecting ',' or ';' in E:\Program Files\xampp\htdocs\cis\w2\functs.php on line 10. If I insert the function internally, it works, so I know its something with how I am formatting the include page.
It's pretty obvious via the syntax highlighting what is wrong here:
echo '</head>";
//-----------^
This line has the incorrect quote mark, thus you never terminate the string, and it keeps going.
Edit:
But that isn't the only problem. You also never close your function with a right curly bracket: }.
The main problem is that you have mismatched quotes on this line:
echo '</head>";
However, I have to say I'm confused as to why you have the HEREDOC. Surely you just need the if statement alone?

php , read file code problem

I was using this piece of php code for a site.
Now its old and I recently had a few attacks. Script was used for to include another file from someplace else and send spam. Obviously this makes my script as spam sender.
for the content
$htm = ".htm";
$pid = "$details$htm";
function show_details($pid)
{
if (!preg_match("/http/", $pid)) {
require($pid);
} else {
die;
}
}
and for the title, desc , keywords etc..
$txt = ".txt";
$title = "$details$txt";
function show_title($title)
{
if (!preg_match("/http/", $title)) {
if (file_exists($title)) {
require($title);
} else {
die;
}
}
}
and a display.php file with
print '
<!-- CONTENT -->
';
show_details("$pid");
print '
by this code ı was able to call any content by "/display.php?details=mycontentpage"
mycontentpage.htm
mycontentpage.txt
.............
Now this code has to be re-coded .. I can not change the construction as the site is just too big.
So I guess I just have to stick to this..
Can anyone help ? Any comments ?
To make scripts like this more secure, you have to ensure register_globals is set to OFF. This means you'll have to add a line like:
php_flag register_globals off
...To .htaccess. Then, declare all your user variables the first time you use them like:
$details = $_GET['details']
...Which assigns the data from the URI piece "details" to the PHP variable $details.
I can very much see how your attackers were able to get in via your code and register_globals set to on -- they'd need to merely create a .htm file with PHP code in it that reassigns other variables, include it, then viola.
For more info, see:
http://us2.php.net/manual/en/security.globals.php
Hope this helps!

Categories