2017-08-08 121 views
0

首先,對不起,如果這是重複的。我查看了一些解決方案,但都沒有成功。我一直試圖做這個簡單的事情,像3小時沒有結果。

我有這個網站:

<form method="post" action="?c=Logo&action=save" id="form"> 
    <!-- some inputs --> 
    <img src="" data-filename="new-logo.png"> 
    <input type="submit" value="submit"> 
</form> 

在這裏,我想修改的形式,所以我的PHP文件會認識到$ _ POST [「新標誌」]。

// $_POST['new-logo'] = $('img').data('filename') 
$logo = new Logo($_POST['new-logo']); 
$logo->save(); 

我想我應該使用jQuery函數$ .post()但不知何故我無法管理。謝謝大家的幫助:-)

SOLUTION
最後,因爲我發現了一個很簡單的解決方案。因爲我希望我的數據在我的PHP文件來處理,我只是增加了一個<input type="hidden">,並修改了其與JS值:-)

+0

你需要把加密類型attribut在表單標籤'''<形式......方法= 「郵報」 ENCTYPE =「多/ form-data「>'''看到這個https://stackoverflow.com/questions/8659808/how-does-http-file-upload-work和這裏https://www.w3schools.com/php/php_file_upload.asp – TotPeRo

+0

你應該發佈你嘗試過的東西,即使它不起作用。那麼我們至少有一個起點,試圖告訴你你錯在哪裏。 –

回答

0

你也可以將數據陣列從頭開始像這樣:

$('#form').on('submit', function(e){ 
    e.preventDefault(); 

    $.ajax({ 
     type: "POST", 
     url: $(this).attr('action'), 
     data: {key1:"value", key2:"value2"}, 
     dataType: "json", 
     success: function(data) { 
      //do success stuff here 
     }, 
     error: function() { 
      //do error stuff here. 
     } 
    }); 
}); 
+0

先生,你爲什麼複製我的entier回答O.o – yoeunes

+0

我沒有。看得更近。 – catbadger

0

您可以將其添加爲隱藏輸入。

$("#form").submit(function() { 
    $(this).append($("<input>", { 
     type: "hidden", 
     name: "new-logo", 
     value: $(this).find("img").data("filename") 
    }); 
}); 
-1
 $logo = new Logo($_POST['new-logo']); 

是 「新標識」 功能還是什麼?很確定,不能有空間。

+0

http://php.net/manual/zh/language.oop5.basic.php#language.oop5.basic.new – louisfischer

+0

對不起m8。從來沒有真正與PHP課等工作。 :/ –

0

你可以添加這對你POST請求是這樣的:

$('#form').on('submit', function(e){ 
    e.preventDefault(); 

    var $form = $(this); 

    var datastring = $form.serializeArray(); 
    datastring.push({name:'new-logo', value:$form.find('img').data('filename')}); 

    $.ajax({ 
     type: "POST", 
     url: $form.attr('action'), 
     data: datastring, 
     dataType: "json", 
     success: function(data) { 
      //var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this 
      // do what ever you want with the server response 
     }, 
     error: function() { 
      alert('error handing here'); 
     } 
    }); 
});