I have a php file that's being used as a dynamic stylesheet. So its a very large file with css/values generated by php. Here's an example.
<?php
header("Content-Type: text/css");
?>
.top {
top: <?= $topValue ?>px;
}
More css...
On a separate file, i'm trying to get the generated content from the php file and store it on the page somewhere so I can get it later with javascript. So the method i'm trying is storing the contents in a hidden input. Like so.
<?php
$file = file_get_contents("FILE_URL.php");
?>
<input type="hidden" name="hidden-css" value="<?= $file ?>" />
However, when I do this, the value contains the raw php from the file and its also commenting out the opening/closing php tags. So for instance, its doing things like '< !--?php' instead of "< ?php" and "?-->" instead of "?>". This messes up the general html on the page and doesn't store the value correctly in the input.
So I ask you, how can I fix the current situation or better store the contents of the stylesheet on the page so I can later grab it and use it with javascript?
EDIT: There may also be an issue with quotes in the generated css causing issues. I'm not sure how to address this issue either.
EDIT: This is how it should be stored if everything worked perfectly.
<input type="hidden" name="hidden-css" value="
.top {
top: 54px;
}
" />
You can't use the file_get_contents() function for this purpose - it simply gets the file contents (i.e. it won't run PHP on that file first).
Instead, as user574632 suggests, you must use include and so your code would look something like this:
<input type="hidden" name="hidden-css" value="<?php include 'FILE_URL.php'; ?>" />
Hopefully that solves your problem.
(Another quick note: Given your main file is HTML not CSS, you will probably need to declare that content header again after the input like this:
<?php header('Content-Type: text/html; charset=utf-8'); ?>
)
Related
I want to download text from HTML form to .txt file on my PC. I have tried many ways but nothing works well:
require(fs) doesn't work with the browser, as far as I understand
require(browserify-fs) saves file in VIRTUAL MEMORY (as far as I understand), so it useless for me
last way I found - using PHP script(I found it in net), but it doesn't work, idk why. I have tried to fix it - nothing, I know nothing about PHP
program.php
<?PHP
$title = $_POST["channel0Title"];
$gain = $_POST["channel0Gain"];
$offset = $_POST["channel0Offset"];
$file = fopen('1.txt', 'w');
ftruncate($file, 0);
$content = $title. PHP_EOL .$gain. PHP_EOL .$offset;
fwrite($file , $content);
fclose($file );
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
main.html
<form action="public\scripts\program.php" method="post">
Channel 8 Title:<br><input type="text" name="channel0Title" value="Channel 7"><br>
Gain:<br><input type="text" name="channel0Gain" value="4.000"><br>
Offset:<br><input type="text" name="channel0Offset" value= "6.000"><br>
<input type="submit" id ="submitButton" value="Submit">
</form>
I have a error Cannot POST /public/scripts/program.php
Could you help me please why such a script doesn't work
Or maybe give advice on what language or framework should I use to simplify data transfer between client and server
Thank you in advance!
P.S. I use Node.js, so load program.php as a static file
I want to make an email-signature page with Elementor and then generate the HTML-code like the code when you that appears when you "View Source" of a page, but code gets not exactly correct. I am using a Custom post type and a Elementor template that is not published so it works only when writing: https://mysite123123.com/a/site/?theme_template_id=1164
I have a PHP snippet plugin and this code gets the Post type HTML but not the same like when you press rightclick/View Source. (Its missing some css etc)
Can you see what Im doing wrong or if you know how to echo the correct html from a webpage like a regular visitor see it?
(Im using this code to give a user a HTML signature)
<?php
global $wp;
$vpost = get_post($post_id);
$urlcard = $vpost->post_name;
$current_url = home_url( $wp->request );
$data = file_get_contents("https://mysite123123.com/a/".$urlcard."/?theme_template_id=1164");
$html_encoded = htmlentities($data);
echo $html_encoded;
You can tell the browser how it should render your data using a Content-Type HTTP Response header.
To have your data rendered as plain text -- as the 'View source' view does -- use "text/plain" as mime-type;
header("Content-Type: text/plain");
echo $data
I did also creating a different class. but still not working...
but if I place the '<script>console.log("message here");</script>' will work..
//index.html
assuming that this is a complete code.
<form action="post.php" method="post">
<input type="text" name="name" id="name"/>
<input type="submit" value="submit"/>
</form>
//post.php
<?PHP
if(isset($_POST['name'])){
echo "<script>console.log('".$_POST['name']."');</script>";
}
?>
my problem is , i cant use console.log in submitting a form. but if I did this in redirection It will work..
The Function under my class is ...
public function console($data) {
if(is_array($data) || is_object($data)) {
echo("<script>console.log('".json_encode($data)."');</script>");
} else {
echo("<script>console.log('".$data."');</script>");
}
}
It does not work within PHP because of the missing " around the String argument of console.log.
The output would've been
<script>console.log(name);</script>
instead of
<script>console.log("name");</script>
Solution
echo '<script>console.log("'.$_POST['name'].'");</script>';
If you are trying to debug or see the value that was posted from the front end to back end then you can simply use the chrome inspector.
Right click anywhere in browser and click inspect element.
click on network tab.
submit your form with desired values.
on the left click on the post.php.
Click on the headers on the right and scroll down to find Form Data.
You will have all your post variables listed there with respective values.
You seem to be trying to debug the $_POST variable, If thats the case, then Please note, that console.log() is a frontend debugging tool used in javascript, and has nothing to do with php.
Few good way of checking the content of variables in php.
1. print_r
This will output the content of a variable that can be an array, object or string in a nice human readable format.
echo '<pre>';
print_r($_POST);
echo '</pre>';
die();
2. var_dump
This will output the content of the variable with extra respective details like datatypes and length.
var_dump($_POST);
die();
In a php file I have tags with dynamic id's.
Example:
<ul id="<?php echo 'ul_'.$xx; ?>">
<li id="test_li">...</li>
</ul>
In a separate JavaScript file I am wanting to use the load function and target that div.
$('<?php echo 'ul_'.$xx; ?>').load('../includes/myphpfile.inc.php #test_li');
My question is, how could I load that php within a JavaScript file?
Notes:- The reason I have it set up this way is due to looping multiple ul's. I just want my ajax file to refresh the li's within the specific ul I am inside of.
Edit: If I use classes instead of Id's it adds my newly inserted rows into all of the ul's with the same class. If I use a plain Id it only works on the first ul. Further explaining why I need to target the way I am.
Unable to find a solution using the path I was on, I looked for alternate methods. Worked out I could add the event in the php file on the submit form button click.
The reason I wasn't using this method at first was because the code was running before it submitted to the DB and therefore was always behind in relation to the new data being submitted.
To my surprise I fixed this by just duplicating the line of code.
<script>
$("<?php echo '#enter_submit_button'.$linkid ?>").click(function(){
/*1*/
$('#<?php echo 'ul__'.$linkid; ?>').load('../includes/pursuit.inc.php <?php echo '#li__'.$linkid; ?>');
$("<?php echo '#insert_new'.$linkid; ?>").fadeOut();
/*2 with delay*/
$('#<?php echo 'ul__'.$linkid; ?>').delay( 800 ).load('../includes/pursuit.inc.php <?php echo '#li__'.$linkid; ?>');
});
</script>
I honestly couldn't say exactly why this works but the logic behind trying the method came from hoping that the double load would pick up the latest submission.
The simplest answer would be to put your JavaScript code into the PHP file inside
<script></script> tags, i.e, just before closing your <body> tag, do this:
<script type="text/javascript">
$('<?php echo 'ul_'.$xx; ?>').load('../includes/myphpfile.inc.php #test_li');
</script>
But if you must use an external js file, then you could do this:
Create a (.php) file and write:
<?php
header("Content-type: application/javascript");
?>
// your js code with php inside (as you would do in HTML)
var othervar='<?php echo $phpVar; ?>';
//<?php //some php ;?>
Then, in your main (html/php) file, put in:
<script src='myjs.php' type='application/javascript'></script>
Hope that solves it.
I have inherited a PHP project that contains several HTML files with the following structure
<?php $this->placeholder('dom.ready')->captureStart(); ?>
var some = javascript.goesHere();
<?php $this->placeholder('dom.ready')->captureEnd(); ?>
<div class="some html goes here">
<h1> <?php echo "with some php in the middle"; ?> </h1>
</div>
Would it be possible to configure Phpstorm to interpret everything between the placeholder lines as JS and the rest of the file as HTML with some PHP?
I know the file structure is very unfortunate, but I can not refactor it for now...
Thanks
There are at least two interesting possibilities...
1. Heredoc notation
https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
For me the most interesting solution was to transform the JavaScript into a PHP string using the heredoc syntax:
<div>
<div>
<?php $this->placeholder('dom.ready')->captureStart(); ?>
<?= <<<JS
var some = javascript.goesHere();
JS
?>
<?php $this->placeholder('dom.ready')->captureEnd(); ?>
</div>
</div>
Ensure to place the closing identifier in the first column of line
2. Change the filename
When you change your file extension from .php into .js.php PhpStorm will also highlight your JavaScript.
I think this solution is nice because you will always be remembered that JavaScript is in your PHP file. And this has to be cleared out...
Both solutions was found on jetbrains.com. Thanks to Andriy Bazanov:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/207046645-Zend-framework-and-code-formating-in-view-files
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000790544-Syntax-Highligth-on-some-special-cases
Probably not really what you want but if you surround the javascript with a script tab phpstorm will interpret it as js. You would need to change your code to look like:
<script>
<?php $this->placeholder('dom.ready')->captureStart(); ?>
var some = javascript.goesHere();
<?php $this->placeholder('dom.ready')->captureEnd(); ?>
</script>
<div class="some html goes here">
<h1> <?php echo "with some php in the middle"; ?> </h1>
</div>
This article says you can use two file extentions to accomplish something similar without using the script tags.
https://confluence.jetbrains.com/display/PhpStorm/Syntax+highlighting+of+PHP+inside+JavaScript+(and+other+languages)