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

  • 2
    Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Nico Haase Jun 18 '19 at 13:04
  • It's not a duplicate. This isn't a question on the differences between double and single quotes in PHP... – CrimsonTorso Jun 18 '19 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 – Raj Jun 18 '19 at 13:16
  • So, if this should not be a duplicate, can you explain what that variable `$replace_with` should contain? – Nico Haase Jun 18 '19 at 13:16
  • I've editted the thread. – CrimsonTorso Jun 18 '19 at 13:24
  • 1
    I do not see your target text yes in
    Date of certification: 17 June 2019
    , 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 ?
    – Raj Jun 18 '19 at 13:25
  • Can I added that this is a field I know nothing about. I don't know any PHP. – CrimsonTorso Jun 18 '19 at 13:31
  • Your form has no ``? Could you give us the (correct) form html you are using? – jibsteroos Jun 18 '19 at 13:35

1 Answers1

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>
Raj
  • 497
  • 4
  • 8