user1667633
user1667633

Reputation: 4619

Send jQuery array on php form submit

Right now I am updating user_list List using jquery and submitting the form data using Ajax

now I want to submit that form like normal form submit

Here is the problem I am unable to pass the JQuery updated user_list along form ?

Is There any way to do this ?

What I have tried

<script>

var user_names=[];

function check_selected(id){

    user_names.push(id);

        $("#username").val(user_name);

}
</script>

Where username is a form field like

<input type ="hidden" val = "" id="username" name ="username">

Upvotes: 0

Views: 136

Answers (3)

user1406062
user1406062

Reputation: 883

I am not sure if this will help or not, but you have used a variable that may have not been initialized

$("#username").val(user_name);

should be

$("#username").val(user_names);    //missing s

besides, if you are after sending an array to the server, and the server is php, I would suggest adding [] square brackets at the end of the field name, this is a clean way of sending the list and the server can build the array and you would be able to access it as a list

 $_POST['list_name'][index]

Upvotes: 0

Andreas
Andreas

Reputation: 21881

val is not a valid attribute for the <input /> element. Change it to value

<input type="hidden" value="" id="username" name="username" />

Upvotes: 1

GBD
GBD

Reputation: 15981

Try this

<script>

var user_names=[];

function check_selected(id){

    user_names.push(id);

        $("#username").val(user_names.join(","));

}
</script>

Upvotes: 0

Related Questions