javascript function not behaving correctly - php

I have this little function
function makewindows(){
child1 = window.open ("about:blank");
child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");
child1.document.close();
}
Which whatever I try, simply outputs the php code as the html source, and not the result of the php code. This was previously working fine, and I am not sure what I have changed to result in this behavior.
I have pasted all the code now. An error is generated by a link that calls updateByQuery, preventing makewindows from being parsed correctly..I think. I am not sure what is wrong with updateByQuery however:
function updateByQuery(layer, query) {
url = "get_records.php?cmd=GetRecordSet&query="+query+"&sid="+Math.random();
update(layer, url);
}

Have you recently moved this file out of a PHP parsed file (i.e. .phtml/.php) and into a .js file? Note that any PHP you expect to be executed must be parsed by the PHP parser before delivery to the client. If it was originally in a .php file, then it would have been parsed/ executed, and worked fine.
However, .js files are not, by default, parsed by PHP. Perhaps they were, at one point, but your server administrator has recently upgraded something, and lost this behaviour? You may be able to use a local configuration file (in Apache, .htaccess) to re-enable it.

This code must be in a file that is parsed by PHP before being sent to the browser. Make sure it has a ".php" extension (or that Apache/(or other) is configured to put whatever extension it is using through PHP). Also, make sure PHP is installed correctly and working.

I assume you still have it in a file that is parsed by PHP, like the others already have said. Then it is probably something above this code snippet that confuses the php-parser so it don't recognize the php-tag.
To test that, try to output something else before this function, maybe just a comment or something.
Also, use "var" before client1, or else client1 will be in the global scope.
update 1
Since you tried to insert a piece of php-code and it broke, then the problem is that the server don't parse the file as it should.
To test if the server really parses your .js files (its not the default setting I believe), create a new file: test.js
<?php echo "This is a test"; ?>
Open the test.js file in your browser and look at the page source. If it has the php tags your server don't parse .js files.
update 2
If the php works in .js files, try to rewrite the function like this (sorry I have not tested it because I don't have access to a php-server right now)
<?php
echo "function makewindows(){var child1 = window.open (\"about:blank\"); " .
"child1.document.write(\"" . htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES) . "\");" . "child1.document.close(); }";
?>

Make sure you are running the page from the webserver like such: http://localhost/yourpage.php and not directly from the file itself like such: file://yourpage.php

I'm not sure if this will help, but best practices dictate that whenever you write to a new window using JavaScript, you should open and close the document. Can you try this?
function makewindows(){
var child1 = window.open ("about:blank");
child1.document.open();
child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");
child1.document.close();
}

Related

jQuery load php file as text?

Is it possible to load a php file as text with jquery?
$('#loader').load('somefile.php', function(e){
console.log(e);
});
This always interprets/execute the php file but I'm looking for a way to only load it as text, without to resort to renaming my php file as .txt
Is it possible?
Cheers
It is not possible without making any server side modification. The web server will always interpret the php file and return the output. However does not matter what solution you find it'll be very dangereous since you'll be dumping content of your php file to public.
Possible solutions with server side modifications:
Create a PHP file that dumps the content of a file, which name is specified by a url argument
Rename the file (I know the op does not want this, just included since it's an option)
As #nicholas-young suggested, get rid of the PHP tags.
I'm not sure why you need this type of need but I want to emphasize that this might not be a good idea in most of the cases since you'll be make a working PHP file available to public. If you can explain more why you need this we might offer better solutions.
Update:
Create a dumper.php that requires authorization and call this file from the javascript side with passing the filename that you want to be dumped as a parameter (dumper.php?file=index.php)
echo file_get_contents($_GET['file']);
It is of course not possibile.
.load will make an HTTP request to yourwebsite.com/somefile.php hence you will obtain the result of your script not the PHP code inside it.
If you really need the raw code inside your javascript as a string you should output it from the php itself:
<script>
var yourCode = <?=json_encode(file_get_contents('somefile.php')) ?>;
</script>
NO! Would be a major security problem if possible. The header will not matter. If making request towards php file, it will execute prior to delivery.
Use some parameter to print out contents from file instead. But do it in the file itself.

PHP code not outputting properly

I'm working with PHP and HTML, but I have an issue popping up whenever I write some PHP code. An example of this is as follows:
<?php
echo "<h2>Hello?</h2>";
$var = 5;
echo "You have $var minutes to go.";
?>
What this ends up outputting on screen is:
Hello?"; $var = 5; echo "You have $var minutes to go."; ?>
But what I want to happen is this:
Hello? You have 5 minutes to go.
Is there something I'm forgetting to do? It doesn't seem to matter whether or not I add the HTML preamble, or if I put a tag like around the second echo line. Does anyone have any advice?
EDIT: Apparently I have failed to parse PHP correctly. This computer is new and I have XAMPP installed on it, but nothing else. Did I miss something I needed in order to use PHP?
That sounds like if the php code wasn't interpreted.
Make sure to have the code in a file with a filename ending with .php and that PHP is installed/enabled on your server.
The PHP isn't being parsed properly.
Make sure you are saving your files as .php
If you are running this locally through WAMP the make sure to use localhost in your URL because if your URL looks like this file:///C:/wamp/www/index.php then that is incorrect.
I think CakePHP uses .ctp files so that could also be an issue
You can setup Apache to interpret any file extension as PHP
make php and html different.so things become much easier.
try like this:
<h2>Hello?</h2>
<?php
$var = 5;
?>
You have <?php echo $var;?> minutes to go.

Executing a script

I need to execute a script within php, i tried almost everything..
http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?username=user&password=******&sendername=Vfup&mobileno= '.$f.' &message='.$message1.''
I tried java script, but i $f and $message1 are variables, so i cant use echo twice
Is there any function in php, which automatically opens the above link in a new window and i need the remaining part of the php code to execute normally without being affected. Will provide more details if required..
Regards,
PHP executes on the server, so is not capable of opening windows. If you want this to happen on the client side you will have to echo it out as JavaScript that the browser can execute.
If you're trying to get this alternate process to fire, then you might want to just call file_get_contents from your current script:
file_get_contents('http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?'.
'username=user&password=******&sendername=Vfup&mobileno= '.$f.
'&message='.$message1.'');
When file_get_contents is given a URL, it will load all of the data from that URL into a local string. This means, in this case, that the server will do what you are trying to get the browser to do for you.
If that is not sufficient, then you will need to use the js function window.open and pass it that URL:
<script>window.open("<?php echo $url; ?>");</script>

Include external class php

How can include a external class in a php file?
example:
//Test.class.php
<?php
class Test{
function print($param){
echo $param;
}
}
?>
//######################################################
//test.php
<?php
include('http://www.test.com/Test.class.php');
$obj = new Test();
echo $obj->print("hola");
?>
The class is on another server. I have enabled the allow_url_include and allow_url_fopen.
Why can't I call the function print?
The remote file must output the php source code, not execute it.
To output the PHP code instead of executing you could simply remove the .php extension from the file.
PS: Are you really, really, really sure you need remote inclusion? It's a BIG security risk!
What you're including from the other server isn't the code behind the PHP but the output from it (if you visit that page in a browser you aren't seeing the PHP code if you view source right?)
You either need to reconfigure the other server not to execute the code but display it (not a good idea if it's in any way shared or needs to execute it's own code), or rename the other file to something that isn't first interpretted (try otherfile.php.txt)
Have a look at the documentation:
(...) If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
Probably the server, you are trying to get the file from, executes the PHP file and only the result (which is empty) is included. You would have to configure the server in such a way that it outputs the PHP code. But this is not a good idea if it is sensible code.

Problem with AJAX and PHP

I have a small problem, I want to load data from a PHP file and put them on a DIV.
Here's the Jquery code
// Store the username in a variable
var jq_username = $("#txt_checkuser").val();
// Prepare the link variable
var link = 'user.php?action=check&username=' + jq_username;
$('div #checkuser_hint').load(link);
So it works! but instead of loading the result (compiled PHP) it loads the PHP code.
If I write the long URL "http://localhost/project..." it doesn't load anything!
Any idea how to do that?
I think you might be accessing your javascript file as a file on your local filesystem, a request to the same directory would go through the filesystem and not through your webserver, processing the PHP into the desired output. This also explains why http://localhost/project for the AJAX call doesn't work: Javascript might be enforcing the same-origin policy on you.
Verify that you're actually accessing this javascript file through http://localhost/ (as opposed to something like file://C:/My PHP Files/ ).
Does the page return anything when you use your browser?
Are you sure it should not be 'div#checkuser_hint' instead of 'div #checkuser_hint' ?
And this looks like the correct way according to the documentation.
var link = 'user.php';
$('div#checkuser_hint').load(link, {'action':'check', 'username':jq_username});
Are you able to access the script manually on your own? (try accessing it via your browser: htp://localhost/...) It may be the case that you're missing your opening <?php and/or closing ?> in the script-file itself.

Categories