2012-02-16 37 views
0

我有一個數組在PHP中,我有一個輸入字段,我想知道如何讓我的值在輸入字段中的數組?需要發佈到PHP和jQuery數組?

array(
"message"=>$_POST['test'], 
"others"=>"blah" 
) 

<input type="text" id="putIntoArray" > 
<a href="#" onclick="jqueryPOST(this.value);">Post</a> 

function jqueryPOST(value){ 
    var message = $("#putIntoArray").val(); 
    //I dont know how to put this value into "message" array? 
} 

因此,與郵政鏈接,它應該把輸入值放入數組「消息」,但不知道如何?這也是最好的方法嗎?

謝謝!

回答

0
$.post('url_here',{ parameter:value, parameter:value, parameter:value}); 

like in your case - DEMO

<input type="text" id="putIntoArray" > 
<a href="#" id="post">Post</a>​ 

//attaching click handler 
$('#post').on('click',function(){ 
    jqueryPOST(); 
}); 

function jqueryPOST() { 

    var message = $("#putIntoArray").val(); 

    //the something you didn't know :) 

    $.post('/echo/json/', { 
     test: message   //<<<HERE's test! 
    }, function() { 

     //what you want to do after? 
     alert('posted!'); 

    }); 

}​ 
+0

怎麼樣$ _ POST [ '測試']? – hellomello 2012-02-16 07:08:46

+0

我有一個數組了,我不是要創建一個數組。我試圖將輸入值推送到現有的數組中。 – hellomello 2012-02-16 07:09:26

+0

oops,將參數錯誤地命名。更新。 – Joseph 2012-02-16 07:10:05

0

可以使用出任@joseph說,或者您可以使用

$.ajax({ 
      type: "POST", 
      url : "your url for posing data", 
      data:"test="+value, 
     }); 
+0

你甚至可以使用'$ .post()',那麼你不必指定類型。 – Arjan 2012-02-16 07:10:03

+0

如果你注意到我的答案,我說你可以用@joseph表示的方式 和約瑟夫建議$ .post的方式 – Poonam 2012-02-16 07:11:37

+0

我注意到,我只是沒有閱讀@Joseph的所有答案。 – Arjan 2012-02-16 07:35:43