str_replace to replace html content using php - php

i'm creating a html editor using JavaScript and php, where the user selects html element from browser, edits this element inline and submits the changes to the server.
Now to apply the changes, i've used str_replace to compare the changes sent by the user with existing content in the html file at server end and replace it.
The selected element's content($_POST['content1']) and new content($_POST['contentreplace1']) are sent using $_POST
But this functionality is not working properly.
The existing code is not getting replaced with the new one.
<?php
$orig="".trim($_POST['content1']);
$new="".trim($_POST['contentreplace1']);
$file = 'index.html';
$file_contents = file_get_contents($file);
$file_contents_new = str_replace($orig,$new,$file_contents);
file_put_contents($file,$file_contents_new);
?>
What I'm doing wrong here?
Any suggestions or recommendations are welcomed.
Please share your thoughts and experiences.
Thanks for all the help in advance.

Related

How to add an item to a PHP array on HTML user input

<?php
$friends = array('notch',);
foreach ($friends as $friend) {
echo '<img src="http://mcapi.ca/avatar/'.$friend.'"/>';
}
?>
I would like to change the item in the PHP array on HTML user input from a textarea. Could someone provide the HTML & PHP code in order for this to work? The array changing would mean that an image would update. Is this possible to do without having to reload the page for the image to show? I am new to PHP, so my knowledge is fuzzy. Help is greatly appreciated.
As all the comments already say:
No, this is not possible with PHP. PHP runs on the server, prepares a page (html output), and this page is sent to your browser. Therefore, user input can only happen after PHP has finished its work. What you want to do can only be done on the client side - have a look at JavaScript.
As said, you are talking about 2 different worlds(Client side/Front end and server side/Backend).
One thing you could do is, use something like KnockoutJS(or some MVVM/framework, there are several) and when the changes happens, you can interact with your php in execution time without having to reload the page as you were asking.
i have modify your code, hope so it will help you to sort out your problem.
<?php
$friends = array('notch'); // i have remove comma after 'notch'
<script>
var item = document.getElementById("put here your input text field id").value;</script>
foreach ($friends as $friend) {
echo '<img src="http://mcapi.ca/avatar/'.$friend.'"/>';
}
?>

Can't post form value to variable in PHP (Text area)

I am trying to write a form's content to a text file on my computer, which is from where the server is running, and I can successfully write variables that are defined like$newVar = "Text to write."; , but when I try to pass a value from a form in my HTML code by $newVar = $_POST["varOutput"]; , nothing will be written in the file. My HTML form appears as so:
<form id="hidden" action="submit.php" method="post">
<textarea name="varOutput" form="hidden" rows="73" cols="100" id="varholder"></textarea>
</form>
I have some JavaScript that puts a string into the text area, but the string is different for each user, as they will interact with the website differently. I have tried countless times to change every little detail in my code to make it work correctly, but my efforts have been fruitless. Any help would be appreciated.
EDIT:
The PHP code is
$output = $_POST["varOutput"];
$fp = fopen("text1.txt","a");
$savestring = ($output . “n”);
fwrite($fp,$savestring);
fclose($fp);
That is the entirety of the php file that the javascript references. The "n" is to test to see if anything happens. It prints every time.
Debug
1) check if the value is set in textarea.
You can use firebug or inspect hidden elements to know if value is set correctly.
2) print all posted data to on php to know if your data is posted to backend.
After hours of trying different ways, I found out that I could get the $_POST to work if I used an <input type="submit" /> element inside the form. This allowed the php variable to read the text inside the form and write it to my text file. Thanks for everyone's help, and I hope this will help someone else. Have a nice day.

Find and store all innerhtml of div to php variable

I’m trying to store the content of a div to a variable.
Example:
<div class="anything">
<p>We don't know the content of this div</p>
</div>
I want to search for <div class="anything"> and store everything between opening and the end tag.
We also want to avoid using absolute pathnames, so that it only searches the current HTML/PHP file for this div where the code is present.
Is this possible with PHP, or is this only possible with JavaScript ?
PHP is not that intelligent. He doesn't even know what he says.
PHP is a server-side language. It has absolutely NO clue about what the DOM (ie. what is displayed in your browser's window) is when it delivers a page. Yeah I know, PHP rendered the DOM, so how could it not know what's in there?
Simply put, let's say that PHP doesn't have a memory of what he renders. He just knows that at one particular moment, he is delivering strings of characters, but that's all. He kind of doesn't get the big picture. The big picture goes to the client and is called the DOM. The server (PHP) forgets it immediately as he's rendering it.
Like a red fish.
To do that, you need JavaScript (which is on the client's computer, and therefore has complete access to the rendered DOM), or if you want PHP to do this, you have to retrieve an full-rendered page first.
So the only way to do what you want to do in PHP is to get your page printed, and only then you can retrieve it with an http request and parse it with, in your case, a library such as simpleHtmlDom.
Quick example on how to parse a rendered page with simpleHtmlDom:
Let's say you know that your page will be available at http://mypage.com/mypage.php
$html = file_get_html('http://mypage.com/mypage.php');
foreach($html->find('div.anything') as $element)
echo $element->src . '<br>';
you probably need a combination of those.
In your Javascript:
var content = document.getElementsByClassName("anything")[0].innerHTML();
document.getElementByID('formfield').value(content);
document.getElementByID('hiddenForm').submit();
In your HTML/PHP File:
<form id="hiddenForm" action="path/to/your/script">
<input type="hidden" name="formfield" value="" />
</form>
In the script you defined in the form action:
if(!empty($_POST)){
$content = $_POST['formfield'];
// DO something with the content;
}
Alternatively you could send the data via AJAX but I guess you are new to this stuff so you should start slowly :)
Cheers!
steve
You could use JS to take the .innerHTML from the elements you wan and store them in .value of some input fields of a form and then use a submit button to run the PHP form handling as normal. Use .readOnly to make the input fields uneditle.

AJAX/JQUERY post and include

Find this confusing...
If a user fills in a contact form (contactus.php) and submits the form using AJAX/JQuery to (process.php) and on the (process.php) there is a include function but the PHP file isn't being included..
Why could that be??
LOL if it makes sense. All help would really be appreciated.
include("file_name_goes_here.php");
OR__________________________________________________
How can I make (index.php) read a text file's contents as php coding and then excute it?
$data = file_get_contents("file.txt");
Can you load your txt file as a string and use eval? Note: I'm not suggesting this is a good idea!

Have html in a string - need to show it as a separate html page using code

I have a script which takes in html from the user as in full page html either from the user or grabs it via curl or from an email. The thing is that I have the html in a string but on the same page I need to show the htmnl in a separate iframe. I don't want to reput any database, curl or imap code in the page referenced by teh iframe at all - is there a way for me to show html passed into a url somehow? like as in a get variable .. the html can be huge here... sorry if it sounds weird.
You can put the grabbed html into a temp file and put the link to that temp file into the src="" of the iframe.
Create them using tempnam(), then create a small script that gets the (preferably obfuscated) filename and simply prints it out if it was really a temp file created by you.
Be careful! if it doesn't check the filename well, you are giving full read access to your server... Put the link to this script in the src of the iframe. You can also create temp files in the public folder of your www server, but I wouldn't want temp/garbage there.
(if i am not misunderstanding your question)
You could put the string in a textarea inside a form and submit the form ..
the receiving page would read the posted data and render it on the page..
I'm not quite sure what you want to do, put you could always post the string as a POST variable, no limitations on how long they can be.
You can encode pieces of HTML with urlencode which is automatically decoded when you retrieve it with $_GET or you can use e.g. base64_encode and base64_decode. The problem is that there are limits to $_GET and $_POST. Both can be set in your configuration settings, but sending large amounts of data via the URL is really not-done because that's not how it should be used.
But if I read your question correctly, you can fetch the HTML at the top of the page, and then load it into an iframe?
if ($_GET['url']) {
$html = file($_GET['url']);
}
if ($_POST['html']) {
$html = $_POST['html'];
}
And then include it:
<html>
..
<iframe ..><php echo $html; ?></iframe>
..
</html>

Categories