Jump to content

Submit POST with dynamic input


FooKelvin

Recommended Posts

Hi All,

 

I have generate a form which contain the id and name.

the name and id i use php foreach them from database, to make it simple, im just copy the HTML and paste it here.

each of them have individual approval status and remarks.

once submit, it should update individual rows.

I am using ajax to post.

 

My current code.


<html>
<body>
<hr>
<section>
    <h3 id="title454">
        Requestor
    </h3>
 </section>
<form id="form" action="" method="post">
<table>
<tr>
<th>Employee Name</th>
<th>Employee ID</th>
<th>Remark</th>
<th>Action</th>
</tr>
<tr><td>Kelvin</td><td>EE123</td><td><textarea name="remark[]"></textarea></td><td><select name="status[]"><option disabled value="">-Pending-</option><option value="Approved">Approve</option><option value="Reject">Reject</option></select><br></td></tr><tr><td>James</td><td>EE156</td><td><textarea name="remark[]"></textarea></td><td><select name="status[]"><option disabled value="">-Pending-</option><option value="Approved">Approve</option><option value="Reject">Reject</option></select><br></td></tr></table>
<hr>
<table>
<ul>
    <section>
        <h3 id="title454">
            1st Level Approval
        </h3>
    </section>
		<li>
            <label>Amount Approved</label>
            <span>
				<select name="amount-approved">
					<option disabled value="" selected>-Select-</option>
					<option>50%</option>
					<option>100%</option>
				</select>
			</span>
        </li>
</ul>
</table>
<input id="submit" type="button" name="submit" value="submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        // click on button submit
        $("#submit").on('click', function(){
            // send ajax
            $.ajax({
                url: 'tier_one_submit_status.php', // url where to submit the request
                type : "POST", // type of action POST || GET
                dataType : 'json', // data type
                data : $("#form").serialize(), // post data || get data
                success : function(result) {
                    // you can see the result from the console
                    // tab of the developer tools
                    console.log(result);
                },
                error: function(xhr, resp, text) {
                    console.log(xhr, resp, text);
                }
            })
        });
    });

</script>
</body>
</html>

after click the submit button, i would like to update the data individually.

Please help.

Link to comment
Share on other sites

Instead of naming the fields like "remark[]", put keys into those arrays. Like "remark[EE123]".

 

When you foreach on them you'll have that ID available to use.

foreach ($_POST["remark"] as $id => $remark) {
	$status = $_POST["status"][$id];
Make sure to double-check that the employee with that ID is allowed to be updated by this form - probably by using whatever criteria you had when you first showed the form.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.