php - How to get two forms on one submit -
this question has answer here:
i have 2 forms this:
<h2>first form</h2> <form id="first-form"> <table> <tr> <td> <input type="text" name="left[]" value=""> </td> <td> <input type="text" name="right[]" value=""> </td> </tr> <tr> <td> <input type="text" name="left[]" value=""> </td> <td> <input type="text" name="right[]" value=""> </td> </tr> <tr> <td> <input type="text" name="left[]" value=""> </td> <td> <input type="text" name="right[]" value=""> </td> </tr> <tr> <td> <input type="text" name="left[]" value=""> </td> <td> <input type="text" name="right[]" value=""> </td> </tr> </table> </form> <!-- more html --> <h2>second form</h2> <form id="second-form"> <label> <input type="search" name="sentence" value="i bananas"> </label> <label> <input type="text" name="parse-as" value="s"> </label> <label> <select name="include-features"> <option>yes</option> <option selected="selected">no</option> </select> </label> <label> <select name="show-earley"> <option>yes</option> <option selected="selected">no</option> </select> </label> <button type="submit">parse</button> </form>
note first form can contain more rows; user can add more rows form. can see, there's 1 submit button. that's because both forms have submitted together. in jquery i'd this:
$("#second-form").submit(function(event) { $("first-form").submit(); event.preventdefault(); });
as front-end developer of team, need pass down values of both forms back-end. unfortunately i'm @ loss how this. figured this:
$("#second-form").on("submit", function(event) { $("#first-form").submit(); $.post("php/do.php", $("#second-form").serialize()); event.preventdefault(); }); $("#first-form").on("submit", function(event) { $.post("php/do.php", $("#first-form").serializearray()); event.preventdefault(); });
.serializearray()
seems necessary because of array-like values in html #first-form
. in second form, however, don't need array. issue i'm having is, code need in do.php
can handle input. how access data, i.e. serialized forms in php?
i tried number of things such
<?php $data = parse_str($_post["right[]"]); var_dump($data); $date1 = parse_str($_post["right[][0]"]); var_dump($data); $data2 = parse_str($_post["right[][1]"]); var_dump($data); ?>
but of these return undefined index
error, seems mean "selector" wrong. how go selecting array first form, , values of second form in php? , jquery method of submitting first-form
second correct?
why don't :
<h2>first form</h2> <form id="first-form"> <table> <tr> <td> <input type="text" name="left[]" value=""> </td> <td> <input type="text" name="right[]" value=""> </td> </tr> <tr> <td> <input type="text" name="left[]" value=""> </td> <td> <input type="text" name="right[]" value=""> </td> </tr> <tr> <td> <input type="text" name="left[]" value=""> </td> <td> <input type="text" name="right[]" value=""> </td> </tr> <tr> <td> <input type="text" name="left[]" value=""> </td> <td> <input type="text" name="right[]" value=""> </td> </tr> </table> <!-- more html --> <h2>second form</h2> <label> <input type="search" name="sentence" value="i bananas"> </label> <label> <input type="text" name="parse-as" value="s"> </label> <label> <select name="include-features"> <option>yes</option> <option selected="selected">no</option> </select> </label> <label> <select name="show-earley"> <option>yes</option> <option selected="selected">no</option> </select> </label> <button type="submit">parse</button> </form>
maybe it's not cleanest way it, it's easier. results :
<?php $data = parse_str($_post["right[]"]); var_dump($data); $date1 = parse_str($_post["right[][0]"]); var_dump($data1); $data2 = parse_str($_post["right[][1]"]); var_dump($data2); ?>
Comments
Post a Comment