I have a form like this:
<form id="cform">
<label>folder name:</label>
<input type="text" name="Folder[name]" />
<input id="cfolder" type="submit" value="create" />
</form>
I want to submit the form in jQuery ajax, so I have code like this:
$.ajax({
type: "POST",
url: "/folder/create",
data: $('#cform').serialize(),
success: function(msg){
//sucess
}
});
this is all easy so far, but now I want to submit an extra dynamic id($id value is available) along with the whole form. how Can I do that? I tried this:
data: $('#cform').serialize()+"&Folder[id]="+$id,
but it didn't work, the server side did not get the id. the tricky part he开发者_运维百科re for me is Folder[name], Folder[id], because the server side recevie the data in php like this:
if(isset($_POST['Folder'])){
//assign data here
}
hope this is clear and thanks for any suggestion.
As an alternative suggestion:
You can use .serializeArray
[docs] and $.param
[docs]. Then jQuery will take care of all the encoding:
var data = $('#cform').serializeArray();
data.push({name: "Folder[id]", value: $id});
// later
data: $.param(data);
I don't understand if you have a problem with $_POST['Folder']
or not, but if you use names with brackets at the end, PHP will parse the data into an array.
Thus you can access the data with $_POST['Folder']['name']
and $_POST['Folder']['id']
.
For more information have a look at Variables From External Sources.
How about sending data this way -
data: $('#cform').serialize()+"&FolderId=" + $id
then try checking for $_POST['FolderId']
on the back-end?
An example on jsfiddle.
you does not use this
`data: $('#cform').serialize()+"&Folder[id]="+$id,`
to concatenate , use this one
data: $('#cform').serialize()+"&Folderid="+$id,
and in php file get it by
if(isset($_POST['Folderid'])){
//assign data here
}
精彩评论