2013-04-04 69 views
0

我想發送一個包含圖像base64(png)值(GET請求太大)的JavaScript變量。我如何發送這個base64值到php頁面(POST請求),以解碼base64,然後保存它。如何將javascript變量數據發送到php

var base64 = this.tobase64(); // store base64 value to this variable 

     $.post('/js/uploadify/handler.php', {basevalue: base64}); 

       }); 

    window.location = "http://localhost/file/handler.php"; 

我想上面的代碼,但它不工作,

我想用一個按鈕的onclick事件發送的值(這是不是一個提交按鈕)。我沒有使用表格。

PHP代碼

$val = $_POST['basevalue']; 
echo $val; 

我得到通知:未定義指數:basevalue錯誤

感謝!

+1

發佈php代碼。 – Kaloyan 2013-04-04 21:22:23

+2

'window.location'可能會破壞POST。 – 2013-04-04 21:25:26

+0

check edit添加了php代碼 – user1910290 2013-04-04 21:26:47

回答

2

等待$.post()成功。

function wl(){ 
window.location = "http://localhost/file/handler.php"; 
} 
var base64 = this.tobase64(); // store base64 value to this variable 
$.post('/js/uploadify/effectsuploadify.php',{ 
    basevalue: base64, 
    success: wl, 
}); 
+0

您是否從腳本之後刪除了'window.location =「http://localhost/file/handler.php」;'? – Mooseman 2013-04-04 21:40:40

+0

好的。我刪除了那個。現在它不重定向我如何解決這個問題。 – user1910290 2013-04-04 21:41:43

+0

你在控制檯中看到任何錯誤嗎? – Mooseman 2013-04-04 21:42:30

0

POST只發送表單的域的值到下一頁。 爲了將圖像發送到下一頁,請將其分配給隱藏字體的值,並使用java腳本提交表單。

var form = document.createElement("form"); 
form.setAttribute("method", "POST"); 
form.setAttribute("action", "http://localhost/file/handler.php"); 

var hiddenField = document.createElement("input"); 
hiddenField.setAttribute("type", "hidden"); 
hiddenField.setAttribute("name", "image"); 
hiddenField.setAttribute("value", base64); 

form.appendChild(hiddenField); 
document.body.appendChild(form); 
form.submit(); 
相關問題