javascript - PHP multiple query string values to array -
i have form, allows user add multiple 'row' of same input fields, basically, asking users specify number of children have.
the form simple, follows (abbreviated reading purposes):
<form name="guestform" method="post"> .... <div class="children-part"> <ul> <li> <li> <li> <label>name:</label> <input type="text" name="childfirstname" /> <li> <li> <label> </label> <input type="text" name="childlastname" /> <li> <li> <label>gender:</label> <input type="radio" name="childgender" value="m" /> <label>male</label> <input type="radio" name="childgender" value="f" /> <label>female</label> <li> </ul> </div> .... </form> i have [+] button on page that, via javascript, clones div.children-part block n-th amount of entries (usually 3).
i take form , serialize data can pass via ajax call web service inserted database. now, single child it's easy query string values, becomes more complicated more one...
example of complex query_string:
&childfirstname=1&childlastname=1&childfirstname=2&childlastname=2&childfirstname=3&childlastname=3&childfirstname=4&childlastname=4 my question is, there way these values array without writing complex loop statements?
i split them on $-character, array , determine how many of single string literal array contains, iterate on each 1 individually build multi-dimensional array of data need (more 1 child), feels overkill simple?
adding [] should allow iterate , access data parallel arrays. alternative adding , maintaining index each "child" , incrementing that. e.g. name="childfirstname[currentindex]" each time row added, update , increment javascript variable containing current index.
<form name="guestform" method="post"> .... <div class="children-part"> <ul> <li> <li> <li> <label>name:</label> <input type="text" name="childfirstname[]" /> <li> <li> <label> </label> <input type="text" name="childlastname[]" /> <li> <li> <label>gender:</label> <input type="radio" name="childgender[]" value="m" /> <label>male</label> <input type="radio" name="childgender[]" value="f" /> <label>female</label> <li> </ul> </div> .... </form>
Comments
Post a Comment