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.
Related
I am trying to pass specific variables to the html file which has extension of .php
for e.g I am trying to load main.php by using loadHTMLFile() function in another file. but when I try to add code like this
"<?php echo "hello world"?>"
and running the website and checking it's inspect elemnt this php code is commented, do you have any idea or way around of this?
I have tried using semicolon like this
'.<?php echo "hello world" ?>.'
also
'.echo "hello world".'
but it does not works
function loadHtml($loadfile){
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile($loadfile);
echo $doc->saveHtml() ;
}
and is called in index.php like this:
loadHtml("./views/html/shop/main.php");
about errors there are no errors just that I can't call the php variable or function or anythin php related to the main.php file because anything that has php tags around it is commented. Please help me if you know solution or way around this.
You can use file_get_contents() with eval()
Example: eval(file_get_contents('file.php'));
But you will either have to remove the PHP tags from the file you want to include or do like this: eval('?>'. file_get_contents('file.php'));
Why you didn't use just require("./views/html/shop/main.php") or include("./views/html/shop/main.php")?
Using eval() is not great idea, because it call script in special context, and is it unsafe, less effective to power and little hard to debug errors in future (stack trace log is not contain source of eval files).
I have a PHP file (myFile.php) that parses XML, and assigns the data I need to a variable ($myVar). I need to echo this variable on an HTML page. I’ve tried adding <?php include ‘/myFile.php’; echo $myVar; ?> to the HTML file to no avail, however if I call the script itself (http://localhost/myFile.php) and add echo $myVar; to the end of the file the data is displayed as expected.
Am I missing something simple? I’m running PHP7 on a Linux Apache server - is there a setting somewhere that I need to change? I’ve used this syntax in the past without issue.
Thank you for the second set of eyes!
Try the following:
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/myFile.php';
echo $myVar;
?>
I'm learning PHP coding and I can't solve this problemIt's includes the variables and the endlines.
I'm new to the language.
There is no other issue, fix your xampp problem, put your project in xampp > htdocs folder, save your file with .php extension and go to browser write http://localhost/project/page.php
Update: You are calling POST values, meaning a form (similar to a login form) has to send the values of tireqty, oilqty, and sparkqty to that page.
So you generally want to start a .php file with <?php at the very top. I would recommend doing a simple example like this:
<?php
echo '<p>1</p>';
?>
<p>2</p>
Or, change your file to reflect .html instead. You should be able to still have the php run properly there like this:
<p>1</p>
<?php
echo '<p>2</p>';
?>
If this doesn't work, please update your question to provide:
Operating System
What program you're using
What browser you're using
What version of PHP you're using
Any other information about your system
php html server
I'm new to PHP and I seem to be doing something wrong. On one hand, I have a Perl script that looks like this:
use LWP::UserAgent;
my $browser = LWP::UserAgent->new;
my $url = 'https://url/index.php';
my $response = $browser->post($url, [
"command" => "test",
"data" => "123"
]);
die "Error getting $url" unless $response->is_success;
print $response->content;
On the server, the index.php file looks like this:
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
echo "Hello!\n";
}else {
echo "Error\n";
}
?>
And... that's it. If I try to execute the Perl script, however, it prints the whole index.php file, instead of Hello! or that other error message. I guess it makes sense that I'm requesting a file and that's what it's printing, however I'm quite confused about what it is I'm doing wrong. I've been looking around for examples for a while but I've found nothing so far that could point me in the right direction.
I think the problem here is that the server doesn't know that it should be treating the PHP file as PHP. Instead of parsing/interpreting it it's just returning it.
Make sure that you have PHP installed on the server and then make sure that the following line is in your Apache config:
AddType application/x-httpd-php php
Sounds like you don't have PHP set up on your server.
Here's some info on the steps required to get it up and running on Apache. Even if you have the module installed, you are likely missing some httpd.conf configuration steps.
Can you load https://url/index.php in your web browser? My first guess would be that your Webserver isn't executing PHP and is just outputting the contents of the index.php file.
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();
}