我正在尝试复制文件并输入一些信息。首先我使用了form-method-action(方法1)。这很有效。但我想用Ajax保持在同一页面上。所以我创建了方法2,但这种方法不起作用。
这就是我的文件夹的样子。 'Document.docx'在其文件中有'$ naam'
HTML方法1:
<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>
HTML方法2:
<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>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function() {
$.ajax({
url: 'kopie.php',
method: 'POST',
data: {
factuur: factuur,
naam: naam
}
success: function() {
$('#ans').html("It worked");
}
})
})
})
</script>
PHP for BOTH METHODS:
$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);
答案 0 :(得分:0)
您需要在数据上定义输入值。
$(document).ready(function(){
$("#submit").click(function() {
$.ajax({
url: 'kopie.php',
method: 'POST',
data: {
factuur: $('input[name=factuur]').val(),
naam: $('input[name=naam]').val()
},
success: function() {
$('#ans').html("It worked");
}
})
})
})
&#13;
答案 1 :(得分:0)
在你的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");
}
})
})
})