How to Use ajax to upload an array of data

Source: Internet
Author: User
Tags html header

PHP does not automatically convert multiple check boxes with the same name to an array as ASP does, which makes it inconvenient to use. However, there is still a solution, that is, to use JavaScript for preprocessing. Multiple check boxes with the same name still exist in the form of arrays in Javascript. Therefore, before submitting a form, you can use JavaScript to combine the information groups in the check box into a number of character groups and assign values to the hidden elements in the form, then, use the explode function in PHP to parse the array, so that the check box information can be passed. The following is an example.

Suppose there is a form like this: kaishi entertainment city

<form name="form1" id="form1" method="post" action="http://www.nowamagic.net/index.php" onSubmit="return Checker()"> <input type="checkbox" name="item" value="1">1<br> <input type="checkbox" name="item" value="2">2<br> <input type="checkbox" name="item" value="3">3<br> <input type="checkbox" name="item" value="4">4<br> <input type="hidden" name="items" value=""> <input type="submit" value="Submit"> </form> 

This form has four check boxes whose names are item. When you click the submit button, the checker function will be called, and if the checker returns the true form, it will be submitted, if the return value is false, the form will not be submitted. Here, the checker function is the preprocessing function to be compiled. Add the following Javascript in the HTML header section:

Function Checker () {form1.items. value = ""; if (! Form1.item. length) // there is only one check box, form1.item. length = undefined {If (form1.items. checked) form1.items. value = form1.item. value;} else {for (I = 0; I <form1.item. length; I ++) {If (form1.item (I ). checked) // check box {form1.items. value = form1.item (I ). value; For (j = I + 1; j <form1.item. length; j ++) {If (form1.item (j ). checked) {form1.items. value + = ""; // use space as the delimiter form1.items. value + = form1.item (j ). value ;}} break ;}} return true ;}

In this way, you can combine the values of all selected check boxes into a string array, and use the following statement in myphp. php:

$items = explode(" ", $HTTP_POST_VARS["items"]);  

These options can be separated into arrays. Note that the value in the option cannot contain delimiters (here it is a space ).

How to Use ajax to upload an array of data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.