javascript inside php code - php

i have place javascript inside php code, like this
<?php if(condition) { ?><script>do some scripting here</script><?php } ?>
it works perfect when i run it in local.
but when i upload it to the server it doesnt run..
the javascript doesnt work.
Can anyone please help.
thanks,
Devan

First, I'm pretty sure you have to do either:
<?php if (condition) { ?> ** script stuff ** <?php } ?>
or
<?php if (condition) : ?> ** script stuff ** <?php endif; ?>
(Your script has no scope on the if statement, although that could be a copy/paste error).
Second, it could be your script (perhaps one of the Javascript files such as jQuery isn't on your server).

Your problem is pretty vague...
Here's all the things I would review first :
are you sure php runs on your distant server ?
do you have anything on screen when running your script ?
when you view the source code from the generated page : do you see your javascript code ?
did you check for javascript error messages ?
Another batch of questions after the comments :)
Did you try to change your JS code with a "basic" one, such as "alert('my code works');" ? (in order to see if your javascript code is triggered)
If the basic alert code does not produce a popup, then you should see your browser's setup with javascript. It may not be enabled
if it works (and you don't have any JS error messages) : try to place some more "alert" popups along your own code, in order to find where your code stops to work)
This will not provide you a direct answer to your problem, but will help you to find more details on your problem, and will help us find an answer

Related

php read webpage after all processing are done

I dont know how to explain my need, and neither which key words to use to find a solution on google, so i'll give an url to be more clear:
check an IP (click on: Check your current IP address)
I'ld like, by using this website for example, getting somes informations after all the processus are terminated.
I tried with "file_get_contents" and with "cURL functions" but i did not find a way to do it, i always get the original source code.
Any idea ?
EDIT:
<body onLoad="setTimeout('get_my_blacklist()', 60000)">
...
...
<?php
echo '<iframe id="my_iframe" src="http://multirbl.valli.org/lookup/'.$ip.'.html">';
?>
...
...
<script>
function get_my_blacklist()
{
//function to get the content after somes secondes.
}
</script>
Here is the new code i tried thank to #Ludovic for is iframe idea.
Still working on it, i'll tell you if its working or not to solve my issue.
Edit2: Whatever how i try, i didnt find a way to get the containt of my frame window.. And even if i'ld succeed, i dont know how i can update my database if do it with JQuery/Javascript
First the page should have been construct by server script like PHP, at this step you have all IP requested then the page is modified by JQuery script who seems to query each IP.
The second step is an asynchronous script so you can't know when the page is effectively finished to construct.

php line of code not executing

Really puzzled on this, probably simple but it is the day after New Years.
I have one line of php 'inline' code that fails to execute on the server before the server sends the page to the browser.
Here's the code:
<div>
Server says this: <?php "DOOSH"; ?>
</div>
That's all I have in the body of my html test page. I'm expecting the inline php to put 'DOOSH' there into the html then send the html over the client. Not happening.
All I see in the browser is "Server says this:" and nothing else.
The reason this has me confused is -- I KNOW the php executes on the server BEFORE the html above is sent over, so why is the DOOSH not being sent? I have tried 'DOOSH', "DOOSH", I even tried writing a php function in the file to return "DOOSH" and nothing works -- do I have a syntax problem here?
(NOTE: Doesn't have much to do with my problem here, but I can successfully click on the "Server says this =>" link and I see thePhpFile.php successfully load, as expected.)
You need to echo it.
<?php echo "DOOSH"; ?>
Or the shorthand way,
<?="DOOSH";?>
I think the syntax you may be looking for is
<?="DOOSH";?>
Right now, you just have a statement which contains the string "DOOSH" but doesn't do anything with it. You either have to use the syntax above or echo it.
<? echo "DOOSH"; ?>
Or maybe I"m totally misunderstanding...
<?php echo "DOOSH"; ?>
Or
<?= "DOOSH"; ?>

PHP Exits code from =>

I have a chunk of PHP given to me to put into a web page... the code looks like this:
<?php
$SVQuerystring = "";
foreach (($_GET) as $SVGetKey => $SVGetValue) {
....
?>
Problem is, everything after the '>' in the 3rd line gets printed as html text, so the php is being exited there. I can't find a similar issue anywhere, and I have never worked with PHP before. Any suggestions on what is wrong, or where to look? Any help would be greatly appreciated :)
I would seriously question whether the PHP is getting executed in the first place.
Try this code to make sure it is executing without any of your code.
<?php
phpinfo();
?>
If you see a bunch of output pertaining to PHP and your server then there is some other problem. If you don't see any new output, then PHP is not correctly installed on your server.

PHP files not loading properly

I have a PHP file consisting of the following structure:
<html>... headers, scripts & styling
... some html here
<?php
if($_GET['v'] == 1)
{
?>
... html code here ...
<?php
}
else
{
?>
... html code here ...
<?php
}
?>
</html>
Sometimes the file just loads half, for example if v=1 what would load onto the screen (if I check with View Source also) is something like this: (relative to what I exampled above)
<html>... headers, scripts & styling
... some html here
... html cod
As you can see, the code just cuts off randomly. The is nothing obvious casing this such as a loop or anything. It happens in the middle of HTML code and not inside the <?php ?> tags.
It looks as if the server just decides to stop communicating right there-and-then for no reason. It also happens at a different and random place each time and sometimes loads perfectly fine.
It also only happens on my shared hosting account and not on my localhost.
Is there anything simples that might be causing this?
Did anyone experience this before?
Your code produces a warning (apparently silent) and fails here:
if($_GET['v'] == 1)
if no v parameter was given in the query string.
Do it like this:
if(isset($_GET['v']) && $_GET['v'] == 1)
If you're running an old version of PHP you'll have to make two separate if statements for each of the two conditions.
Make sure you have display_errors turned on.
ini_set('display_errors',1);
Just to make sure there's nothing going horribly wrong.

PHP echo in Javascript

I have this code:
document.getElementById('root').style.left = '<?php echo $page_position[$info["os"]][$info["browser"]][0]; ?>';
document.getElementById('root').style.top = '<?php echo $page_position[$info["os"]][$info["browser"]][1]; ?>';
Why won't this work like this?
Can anybody point me in the right direction?
EDIT:
<?php echo $page_position[$info["os"]][$info["browser"]][1]; ?>
echoed "top:300px;"
Sorry guys, very stupid error of mine :/
If you put it in a .js file, it won't work because it must be in a page that is parsed for PHP (.php).
Things to check:
Check to make sure those variables are available. print_r($info) will help you out.
Make sure that the code is actually being filled with those variables (symptom of #1). View source and check that they are there.
Ensure you don't have any JavaScript errors preceding your code. Open your console (like Firebug) and check to make sure you don't.
Ensure that a root element exists. Again, use your console.
My money goes on #3, you probably have a JavaScript error somewhere that is stopping execution of the script.

Categories