0

I'm trying to edit a local .html file and the specific contents of that file with a PHP file on my localhost. I want to add forms where a user can input the information and the PHP file will replace the text / fill in the text on the HTML document with the user defined variables. Any suggestions?

I've already tried str_replace, file_get_contents and file_put_contents, but It's still not working. My local xampp server is running Apache 2.

    $main = $_POST['value'];
    echo $main;

The 'value' is from a HTML form, which I've tried using for getting user input.

function replace_string_in_file($filename, $string_to_replace, $replace_with){
    $content=file_get_contents($filename);
    $content_chunks=explode($string_to_replace, $content);
    $content=implode($replace_with, $content_chunks);
    file_put_contents($filename, $content);
}
$filename="alt.html";
$string_to_replace="yes";
$replace_with = "$main";
replace_string_in_file($filename, $string_to_replace, $replace_with)

Within the HTML file, there is some text I've set called 'yes'. I expected the output to change the text 'yes', to a defined user variable, but it won't change at all.

Below is my HTML text from the other document that I want to edit: <div class="t m0 x7 h6 y9 ff2 fs6 fc0 sc0 ls0 ws0">Date of certification: <span class="_ _6"> </span><span class="ff1">17 June 2019</span></div>

The form data that the user inputs data into is below as well:

<form method="post" action="">
<input type="text" name="value">
<input type="

Also, just to be sure people know, the variable $replace_with = "$main"; is the form data in HTML. $main = $_POST['value']; Thanks

8
  • 2
    Possible duplicate of What is the difference between single-quoted and double-quoted strings in PHP? Commented Jun 18, 2019 at 13:04
  • It's not a duplicate. This isn't a question on the differences between double and single quotes in PHP... Commented Jun 18, 2019 at 13:07
  • Can you provide the html content , your same code is working for me, the only difference i have is, I used $_GET['value'] instead of POST Commented Jun 18, 2019 at 13:16
  • So, if this should not be a duplicate, can you explain what that variable $replace_with should contain? Commented Jun 18, 2019 at 13:16
  • 1
    I do not see your target text yes in <div class="t m0 x7 h6 y9 ff2 fs6 fc0 sc0 ls0 ws0">Date of certification: <span class="_ _6"> </span><span class="ff1">17 June 2019</span></div> , when i tried with target text Date , it still work as expected.,Try to hardcode the value of $main and see if that makes a difference ? Commented Jun 18, 2019 at 13:25

1 Answer 1

1

Try this, We expect "Date" in alt.html to be changed to whatever user input is.

HTML file to be used asking for user input

<!DOCTYPE html>
<html>
<body>

<h2>User Input</h2>

<form action="process.php" method="POST">
   Enter your value: <input type="text" name="user_data" value="">
  <br><br>
  <input type="submit" value="Submit">
</form> 

</body>
</html>

process.php

<?php
function replace_string_in_file($filename, $string_to_replace, $replace_with){
    $content=file_get_contents($filename);
    $content_chunks=explode($string_to_replace, $content);
    $content=implode($replace_with, $content_chunks);
    file_put_contents($filename, $content);
}
$filename="alt.html";
$string_to_replace="Date";
$main = $_POST['user_data'];
$replace_with = "$main";
echo $replace_with;
replace_string_in_file($filename, $string_to_replace, $replace_with);

alt.html

<div class="t m0 x7 h6 y9 ff2 fs6 fc0 sc0 ls0 ws0">Date of certification: <span class="_ _6"> </span><span class="ff1">17 June 2019</span></div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.