jQuery在Data和编辑事件中传递多个值

I am creating a php file to alter file systems and I want to make this php run in the background on a ubuntu server. The html creates a webpage but the php does not trigger at all.

I followed a youtube video to this point but I need to pass both new and old string to the php in the Data part of my query which I am unsure how to do.

HTML code

        <html>


    <head>
        <meta charset ='utf-8'/>
        <title> JQuery test </title>
        <script src= "https://ajax.googleapis.cpm/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    </head>


    <body>
    <td>Please type in the old string</td>
    <input id= 'Old_String' />
    <td>Please type in the new string</td>
    <input id= 'New_String' />
            <script>
                $(document).ready(function() {
                    $('#New_String').change(function(){  
                    $.ajax({
                        type: 'GET',
                        url: 'replace.php',
                        data: 'Old_String='+$('#Old_String').val(),
                         success : function(msg){
                            $('#results').html(msg);
                         }
                            })

                    });
                });
            </script>

            <table border="0">


            <div id="results' ></div>  

    </body>

My php code

<?php
$valid = true;

if (!empty($_GET["Old_string"])) {
    $Old_string = $_GET["Old_string"];
} else {
    $valid = false;
    echo "Old string is Empty . Please Enter value to proceed
";
}
if (!empty($_GET["New_string"])) {
    $New_string = $_GET["New_string"];
} else {
    $valid = false;
    echo "New string is Empty . Please Enter value to proceed
";
}
if ($valid) {
    echo "all input correct
";
    $myfile = fopen("word.txt", "r+") or die("Unable to open file!");
    if (flock($myfile, LOCK_EX)) {
        $text = fread($myfile, filesize("word.txt"));
        $count = 0;
        $newstring = str_ireplace($Old_string, $New_string, $text, $count);
        file_put_contents("word.txt", $newstring);
        echo "Number of changes made = " . $count;
        flock($myfile, LOCK_UN); // unlock the file
    } else {
        // flock() returned false, no lock obtained
        print "Could not lock $filename!
";
    }
    fclose($myfile);
}
?>
}

For some reason my PHP does not fire at all and no output is shown in the div results. Am I passing the values incorrectly or am I not quite doing this right? Please any suggestion would be appreciated. I am also trying to switch the event so that it triggers on a button click if you could show me how to do that I would very much appreciate it.

You're referencing $_GET["Old_string"] in your PHP code but passing it by query string parameter under the Old_String key in this line.

data: 'Old_String='+$('#Old_String').val(),

use case consistently, either Old_string or Old_String in both places.

To pass both values, I'd use a simple json object instead of building a query string manually.

data: { 'Old_String': $('Old_String').val(), 'New_String': $('New_String').val() }

You had multiple errors.

These languages are case sensitive. You had different cases for the varables all over the place. So I made everything lowercase.

You can't mix the quotes. So when you start with a ' you need to end with a '.

You may need to create the word.txt file manually. The web server normally does not have permissions to create files in the web root dir.

The browser does not pay attention to new lines " " you need to use

Added some error reporting on the top.

index.php

<html>
  <body
  <head>
    <meta charset ='utf-8'/>
    <title> JQuery test </title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  </head>
  <table border="0">
    <tr>
      <td>Old String</td>
      <td align="center"><input type="text" name="old_string" size="30" id="old_string" /></td>
    </tr>
    <tr>
      <td>New String</td>
      <td align="center"><input type="text" name="new_string" size="30" id="new_string" /></td>
    </tr>
    <div id="results" ></div>  
  </table>
  </form>
   <script>
$(document).ready(function() {
    $('#new_string').change(function(){  
    $.ajax({
      type: 'GET',
      url: 'replace.php',
      data: 'old_string='+$('#old_string').val()+'&new_string='+$('#new_string').val(),
      success : function(msg){
        $('#results').html(msg);
      }
    })
  });
});
   </script>
  </body>
</html>

replace.php

  <?php

    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    $valid = true;

    if (!empty($_GET['old_string']) and isset($_GET['old_string'])) {
        $old_string = $_GET['old_string'];
    } else {
        $valid = false;
        echo 'Old string is Empty . Please Enter value to proceed<br>';
    }
    if (!empty($_GET['new_string']) and isset($_GET['new_string'])) {
        $new_string = $_GET['new_string'];
    } else {
        $valid = false;
        echo 'New string is Empty . Please Enter value to proceed<br>';
    }
    if ($valid) {
        echo 'all input correct<br>';
        $myfile = fopen('word.txt', 'r+') or die('Unable to open file!<br>');
        if (flock($myfile, LOCK_EX)) {
            if(filesize('word.txt')>0){
                $text = fread($myfile, filesize('word.txt'));
            } else {
                $text = '';
            }
            $count = 0;
            $newstring = str_ireplace($old_string, $new_string, $text, $count);
            ftruncate($myfile,0);
            fwrite($myfile,$newstring,strlen($newstring));
            echo "Number of changes made = $count<br>";
            flock($myfile, LOCK_UN); // unlock the file
        } else {
            // flock() returned false, no lock obtained
            print 'Could not lock $filename!<br>';
        }
        fclose($myfile);
    }