2017-07-06 76 views
-1

我试图复制一个文件并在其中放入一些信息。首先,我使用了form-method-action(方法1)。这工作。但我想用Ajax来保持同一页面。所以然后我创建了方法2,但此方法不起作用。使用AJAX更改PHP中的文件

这是我的文件夹的样子。该 'Document.docx' 有 '$ NAAM' 在它的文件

enter image description here

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两种方法:

$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

[您是否观看过浏览器开发工具中的AJAX请求/响应?你有没有在项目中包含jQuery库?是否有任何错误报告?你在网络服务器上运行这个吗?](http://jayblanchard.net/basics_of_jquery_ajax.html) –

+0

那么我的目标是创建一个Document.docx的副本并改变它的一些内部文本。使用方法1的html,它会创建一个新文件并更改文本。使用方法2时,不会创建任何文件 – Soccerlife

回答

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"); 
 
     } 
 
    }) 
 
    }) 
 
})

+0

这不是全部。 –

+0

这不起作用。 – Soccerlife

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"); 
       } 
      }) 
     }) 
    }) 
+0

不幸的是没有什么反应 – Soccerlife

+0

我可能没有得到所有的语法正确。但你明白了吗?尝试检查控制台错误。 – Difster