使用AJAX在PHP中更改文件
我正试图复制一个文件并把一些信息放进去。首先,我使用了表单方法action(方法1)。这起作用了。但我想使用Ajax保持在同一页面上。然后我创建了方法2,但是这个方法不起作用 这就是我的文件夹的外观。“Document.docx”的文件中有“$naam” HTML方法1:使用AJAX在PHP中更改文件,php,Php,我正试图复制一个文件并把一些信息放进去。首先,我使用了表单方法action(方法1)。这起作用了。但我想使用Ajax保持在同一页面上。然后我创建了方法2,但是这个方法不起作用 这就是我的文件夹的外观。“Document.docx”的文件中有“$naam” HTML方法1: <form method="post" action="kopie.php"> <ul> <li><input type="text" name="factu
<form method="post" action="kopie.php">
<ul>
<li><input type="text" name="factuur" id="factuur" placeholder="factuurnaam"></li>
<li><input type="text" name="naam" id="naam" placeholder="naam"></li>
<li><input type="submit" name="submit" id="submit"></li>
</ul>
<h2 class="ans"></h2>
</form>
您需要在数据上定义输入值
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
$.ajax({
url:'kopie.php',
方法:“POST”,
数据:{
factuur:$('input[name=factuur]')。val(),
naam:$('input[name=naam]')。val()
},
成功:函数(){
$('#ans').html(“它起作用了”);
}
})
})
})
在您的AJAX中尝试这一点-我将记录更改
$(document).ready(function(e){
e.preventDefault(); //keeps the page from refreshing
$("#submit").click(function() {
//notice I'm gathering the form data here.
var data = { 'factuur' : $("#factur").val(),
'naam' : $("#naam").val()
}
$.ajax({
url: 'kopie.php',
method: 'POST',
data: data, //referencing the data variable above
dataType: html
success: function() {
$('#ans').html("It worked");
}
})
})
})
你说“不工作”是什么意思?我的目标是创建Document.docx的副本并更改它的一些内部文本。使用方法1的html创建新文件并更改文本。使用方法2,不会创建任何文件。您可能还希望在单击事件中加入
偶数.preventDefault()
。除非您在浏览器中打开开发人员工具并查看是否有错误,否则我们无法为您提供太多帮助。其他人指出,您没有发送任何数据,但您甚至还没有确认是否安装了jQuery或您正在使用Web服务器。这还不是全部。不幸的是,没有发生任何事情。我可能没有获得所有语法的正确性。但你的想法是对的?尝试检查控制台错误。
$factuur = $_POST['factuur'];
$zip = new ZipArchive;
//This is the main document in a .docx file.
$fileToModify = 'word/document.xml';
$wordDoc = "Document.docx";
$newFile = $factuur . ".docx";
copy("Document.docx", $newFile);
$naam2 = $_POST['naam'];
if ($zip->open($newFile) === TRUE) {
$oldContents = $zip->getFromName($fileToModify);
$newContents = str_replace('$naam', $naam2, $oldContents);
$zip->deleteName($fileToModify);
$zip->addFromString($fileToModify, $newContents);
$return =$zip->close();
If ($return==TRUE){
echo "Success!";
}
} else {
echo 'failed';
}
$newFilePath = 'factuur/' . $newFile;
//Move the file using PHP's rename function.
$fileMoved = rename($newFile, $newFilePath);
$(document).ready(function(e){
e.preventDefault(); //keeps the page from refreshing
$("#submit").click(function() {
//notice I'm gathering the form data here.
var data = { 'factuur' : $("#factur").val(),
'naam' : $("#naam").val()
}
$.ajax({
url: 'kopie.php',
method: 'POST',
data: data, //referencing the data variable above
dataType: html
success: function() {
$('#ans').html("It worked");
}
})
})
})