PHP variables to javascript, does not work [closed] - php

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a problem with PHP and Javascript variable communication. I have this code:
<?php
$php_var = 'lol';
?>
<html>
<script type="text/javascript" charset="utf-8">
var php_var = "<?php echo $php_var; ?>";
alert(php_var);
</script>
</html>
This code does not work (as intended) for some reason. I cannot get the PHP variable's value passed down to Javascript variable. For some reason, Javascript completely ignores the php tags and assigns php_var a value of "". So it alerts the literal php code I put it as.
What am I doing wrong? I have been stuck on this problem for 3 hours. Is it my server's problem? (Using web hosting, dedicated). Thank you

Use json_encode() to ensure you get a valid JavaScript expression (otherwise characters such as newlines and quotes will break things):
var php_var = <?php echo json_encode($php_var); ?>;
You also need to ensure PHP is actually enabled for the file. This is usually achieved by giving the file a .php extension.

Use .php file extension and this will work.
If not then you variable won't have a value, you can see the exact issue by using something like firebug.

You say that the file where your code is located has an .html extension, it should be .php for it to render the php code.

Well, you can rename the file to .php and it should work or you can do the following thing:
Create a .htaccess file,
Add the following code ->
RewriteEngine On
<FilesMatch "(file.html)">
SetHandler php5-script
</FilesMatch>
save the file,
then in the html file add the following php line at the beggining ->
than you can write php code inside the selected html file
Or you can add the following rule
RewriteEngine On
file.html file.php

Related

Why won't my PHP parse correctly?

I know the title is vague, but the details for this particular question would make the title absurd.
I have an index.php file that has a virtual include within it:
<!--#include virtual="/includes/header.include"-->
This header.include file is HTML and I want to add a few lines of PHP inside of it.
My .htaccess file is set up to process .include files as php. The actual line from the .htaccess file is:
AddType application/x-httpd-php .include
The PHP that I'm trying to add is:
<?php echo "test <br />"; ?>
But when I load the website, the only thing that prints is:
"; ?>
If I view the source of the index.php I can see the entire PHP code that I have above.
Some other things I've already tried:
Copy the .htaccess file into the directory that the header.include file is in
Both the accepted answer and the answer with 6 upvotes from this question
Tried <br> instead of <br />
What am I doing wrong? Why won't the PHP get parsed correctly?
My PHP version is 5.1.6

PHP placement within HTML page? [duplicate]

This question already has answers here:
PHP in HTML page, Super Quick Advice
(3 answers)
Closed 9 years ago.
I have asked a question leading up to this earlier. Thanks again for your help.
I have updated my .htaccess file in my root directory with the following:
AddType application/x-httpd-php .html
Where within my .html page should I place the php tag?,
I still cannot seem to get my .html page to load the .php script.
If you have followed this guide correctly, then according to the article, you should be able to execute php by simply opening a php tag.
From the article:
Things to watch out for:
If you have an existing .htaccess file, add this to it, do not overwrite it or other >settings may stop working! Always be very careful with your .htaccess file and ask your >host if you need help
Anything in your .html files that starts with your file for some other reason (an XML tag for example) you will need to echo these lines >to prevent errors. For example:
<?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
Where within my .html page should I place the php tag?
The answer is you can place your php content anywhere in your html page as long as it is inside <?php ?> tag.
even if you have done correct modification to .htaccess file and still your php content is not getting parsed than check whether you are connected to server or not.

Calling a function from a variable (PHP) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a website I am making as a school project, and it appears I have jumped into something a bit too deep. I'm using PHP to create a page for each array object. In each page, I want to personalize the contents to match the page. For example, I have a page that gets created named Darryl and it's going to contain all my images. Other pages will be created and the contents of those pages will be created dynamically depending on the page.
What I am trying to do, is to change the img src using PHP and use a counter to make different images pop up. This is incredibly hard for me to explain.
I have this line of html code which contains the php(Which determines which images get shown:
<td><img style="width:11em;" class="magnify" src="<?php print $imgSrc; ?>"/><br><?php print $pageName; ?></td>
The part i am having issue with is the " print $imgSrc; " part. This is the area where my php will create the src path. It uses this variable $imgSrc to get the correct path.
That variable is:
$imgSrc = "images"."/".$pageName."/".counter();
Which writes the
It looks like you're confused about what a function is or perhaps when it gets executed. If this is all the code you have on your page:
$imgSrc = "images"."/".$pageName."/".counter();
<td>
<img style="width:11em;" class="magnify" src="<?php print $imgSrc; ?>"/><br>
<?php print $pageName; ?>
</td>
Then $imgSrc is only ever written to once. So whatever counter() does, it only does once. You could wrap the whole thing in a for loop and do away withe the counter() call. You could remove the local variable of $imageSrc and make the attribute defined on the fly, like so:
<td>
<img style="width:11em;" class="magnify" src="<?php print "images"."/".$pageName."/".counter(); ?>"/><br>
<?php print $pageName; ?>
</td>
Then if the counter() function is stateful itself (as in, it returns a different value each time you call it), you're all set.
You can make a stateful function like the following:
$counter_var = 0;
function counter() {
global $counter_var;
return $counter_var++;
}
But globals are frowned upon, because the create hard to trace side effects (and other reasons). Most people would tell you to use a for loop. I only mention it because it looks closer to what you were trying to do in your original code.

Manipulating CodeIgniter-parsed text [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am using the template parser class in CodeIgniter to give the view (MVC) a designer-friendly look. I have a series of posts that I want to display, but on the front page I only want part of a post to show (first 200 characters) followed by a ... "Read More" link.
It is outputting the posts, but seems to be ignoring the PHP substr() function bc the text comes out full-length.
Inside the model class:
function __construct()
{
parent::__construct();
$this->load->library('parser');
$this->load->model('MPosts');
}
function index() // BASE_URL
{
$data = array("article_posts" => $this->MPosts->get_posts());
$this->parser->parse('VPosts', $data);
}
Inside the view:
<body>
{article_posts}
<h2>{title}</h2>
<p><?=substr("{post}", 0, 200);?>...</p>
<p>Read More</p>
<hr />
{/article_posts}
</body>
Since you are using a MySQL query to fetch the articles you can always use SUBSTRING(article_column_name,1,200) to only grab the first 200 chars, basically substr()
See: https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring
Inside the parser->parse() code:
CI first passes your template through the load->view() processor, just like a regular view (so it executes PHP code inside your template),
then it passes it through the "{pseudovar}-replacer".
At this point I'm sure you understood the problem: substr() is applied to the string "{post}"
I can think of these options for your case:
do the substr() controller-side, probably the fastest solution to fix your issue
remove the Parser layer and use plain ol' PHP code, which is so fine
use a third-party über heavy full-featured template engine
Try echo instead of ?= .
People tend to leave code to load (model) libraries in constructors of the controller in which user-defined functions will invoke calls to user-defined functions implemented in model files.

Search remote page for a string [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is it possible to search a remote page with a given URL for a given string, and indicate whether it exists, and if so, some indication of where it was found?
For instance, "Post Your Question" is included on this site https://stackoverflow.com/questions/ask.
In addition to searching just the page given by the URL, I would also like to search any JS or CSS links.
You can use the PHP Simple HTML DOM Parser to traverse through the contents of an html file.
Doing something like this:
$html = file_get_html('http://stackoverflow.com/questions/ask');
$htmlstring = $html->plaintext;
if(strstr($htmlstring, 'Post Your Question') === true)
// do your stuff here
Then to get the url to css or js:
foreach($html->find('link') as $css) {
$cssHref = $css->href;
//load the css, parse or whatever
}
foreach($html->find('script') as $script) {
$jsSrc = $script->src;
//load js, parse or whatever
}
From there you can grab the css or js source url and do what you want with it.

Categories