php - jQuery在数据和编辑事件中传递多个值
问题描述
我正在创建一个 php 文件来更改文件系统,并且我想让这个 php 在 ubuntu 服务器的后台运行。html 创建一个网页,但 php 根本没有触发。
到目前为止,我关注了一个 youtube 视频,但我需要将新旧字符串都传递给查询的数据部分中的 php,我不确定该怎么做。
HTML 代码
<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>
我的php代码
<?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\n";
}
if (!empty($_GET["New_string"])) {
$New_string = $_GET["New_string"];
} else {
$valid = false;
echo "New string is Empty . Please Enter value to proceed\n";
}
if ($valid) {
echo "all input correct\n";
$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!\n";
}
fclose($myfile);
}
?>
}
出于某种原因,我的 PHP 根本没有触发,并且 div 结果中没有显示任何输出。我是错误地传递了值还是我做得不对?请任何建议将不胜感激。我也在尝试切换事件,以便它在单击按钮时触发,如果你能告诉我如何做到这一点,我将非常感激。
解决方案
你有多个错误。
这些语言区分大小写。您对所有地方的变量都有不同的情况。所以我把所有的东西都小写了。
你不能混用引号。因此,当您以 ' 开头时,您需要以 ' 结尾。
您可能需要手动创建 word.txt 文件。Web 服务器通常无权在 Web 根目录中创建文件。
浏览器不注意换行 "\n" 你需要使用
在顶部添加了一些错误报告。
索引.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>
替换.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);
}
推荐阅读
- c# - 如何在 Entity Framework 数据库中查询两个日期之间的记录,然后返回该信息以显示在屏幕上
- azure - 需要有关 Azure 中托管磁盘的帮助
- javascript - jqgrid过滤工具栏清除搜索设置错误的默认值
- django - Celery 和 django 不能按预期工作
- reactjs - 如何在 npm 脚本中使用自定义 env(.env.production.abc) 文件
- angular - 如何将运行时有效的功能从组件移动到服务
- python - 将等高线图和线图作为单独的子图返回 - Matplotlib
- javascript - 在 if 语句上使用 Promise
- c# - 调用 API 同步还是异步?
- visual-studio-code - Intellisense 在代码片段中不起作用 - VS Code