0

I am sending a form data via ajax call to a php script. I am serializing the data in ajax and on the php script I want to loop through that data to extract the values. This is my ajax call

$("#submitAttendance").click(function(){
            var data = $('form#attendanceForm').serialize();
            $.ajax({
                url: 'save-attendance.php',
                method: 'post',
                data: {formData: data},
                success: function(data){
                    console.log(data);
                    alert(data);
                }
        });
        });

and in the attendance.php I am doing

print_r(($_POST['formData']));//prints the entire serialize data

when I do this

parse_str($_POST['formData'], $searcharray);

print_r(($searcharray));//prints only last user and all radio buttons

I want to extract values so I can save it in db. This is my form

<form action="" id="attendanceForm">
            <?php
            if(mysqli_num_rows($result)>0){
                while($row = $result->fetch_assoc()){ ?>
                    <tr>
                        <input type="hidden" value="<?php echo($row['id']);?>">
                        <td><input type="text" name="name" value="<?php echo $row['fullname'];?>" readonly></td>
                        <td><input type="text" name="email" value="<?php echo $row['email'];?>" readonly</td>
                        <td><input type="text" name="class" value="<?php echo $row['class'];?>" readonly</td>
                        <td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
                        <td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
                    </tr>


                <?php }
            }
            ?>
                <input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
            </form>
4
  • 1
    You dont need to do ' data: {formData: data}' just do data: data. You can than do a normal ' $_POST['name']' to retrieve your posted values. Commented Dec 8, 2015 at 20:42
  • 1
    Ye you are passing an array of data so you need to rename than as an array Commented Dec 8, 2015 at 20:47
  • i have to do some kind of loop since values are repeated. @Franco Commented Dec 8, 2015 at 20:56
  • You need now to loop trough the $_POST array to get your values in the php file. Commented Dec 8, 2015 at 21:02

2 Answers 2

1

You need to rename your items to be able to post arrays (that is call them "whatever" + "[]" and loop over them in PHP), e.g.:

HTML:

<form action="" id="attendanceForm">
            <?php
            if(mysqli_num_rows($result)>0){
                while($row = $result->fetch_assoc()){ ?>
                    <tr>
                        <input type="hidden" value="<?php echo($row['id']);?>">
                        <td><input type="text" name="name[]" value="<?php echo $row['fullname'];?>" readonly></td>
                        <td><input type="text" name="email[]" value="<?php echo $row['email'];?>" readonly</td>
                        <td><input type="text" name="class[]" value="<?php echo $row['class'];?>" readonly</td>
                        <td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
                        <td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
                    </tr>


                <?php }
            }
            ?>
                <input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
            </form>

Later in PHP:

foreach ($_POST["formData"]["name"] as $name)
    echo "Wow, $name is a really pretty name!";

Additionally, I am not sure what present and absent are meant to do and why they should have the same name (an id). You are already posting the id as an hidden field, why should it be done twice? One overrides the other one (as the names have to be unique).

Sign up to request clarification or add additional context in comments.

8 Comments

i want to have a diff id associated with each user
ok that's great, it returned me the complete response now but will you like to tell why we have to put [] in front of name
As said, a name has to be unique, so you will need to send an array instead. Thus, it comes down to name[0], name[1] and so on which is unique as well.
and how I will loop through the results? simple foreach loop is not working
@Adamnick: It will, see my updated answer and the PHP part at the end.
|
0

In addition to @Jan answer I did following to get the complete data and loop through it

parse the incoming data

parse_str($_POST['formData'], $searcharray);

then loop through the array

for ($i = 0 ; $i <= sizeof($searcharray) ; $i++){
            $name = $searcharray['name'][$i];
            $email=   $searcharray['email'][$i];
            $class =  $searcharray['class'][$i];
            $present=  ($searcharray['present'][$i]);
    }

and my form code is

<form action="" id="attendanceForm">
            <?php
            if(mysqli_num_rows($result)>0){
                $i=0;
                while($row = $result->fetch_assoc()){

                    ?>
                    <tr>
                        <input type="hidden" value="<?php echo($row['id']);?>">
                        <td><input type="text" name="name[]" value="<?php echo $row['fullname'];?>" readonly></td>
                        <td><input type="text" name="email[]" value="<?php echo $row['email'];?>" readonly</td>
                        <td><input type="text" name="class[]" value="<?php echo $row['class'];?>" readonly</td>
                        <td><input type="radio" value="present" name="present[<?php echo $i; ?>]" checked></td>
                        <td><input type="radio" value="absent" name="present[<?php echo $i; ?>]"></td>
                    </tr>


                <?php $i++;
                }
            }
            ?>
                <input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
            </form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.