Skip to main content
9thReg
Known Participant
November 8, 2015
Question

Problem with form inside while loop.

  • November 8, 2015
  • 0 replies
  • 1563 views

Hello all,

I'm trying to have forms inside a while loop so a record can be updated without going to a new page.  It works fine for only the first record pulled from the database, but it for the second record on, it still only pulls the data from the frist record.  Will someone please be kind enough to help me get this working?

I've got this in two parts.

the main form:

<?php

$query = "SELECT * FROM commission_type WHERE comm_emp_id = '{$_SESSION['emp_number']}'";

$result=mysql_query($query);

while ($row = mysql_fetch_array($result)) {

?>

<div class="testbox">

<?php

$id = $row['comm_id'];

$comm_type = $row['comm_type'];

?>

<form  method="post" name="form" action="">

<table style="100%;">

<tr>

   <td style="width: 30%;"><input name="comm_id" id="comm_id" type="text" value="<?php echo $id; ?>" ></td>

   <td style="width: 30%;">Commission Type</td>

   <td style="width: 30%;"><input name="comm_type" id="comm_type" type="text" value="<?php echo $comm_type; ?>"></td>

   <td style="width: 20%;"><a href="#" onclick="update_function()">Update</a></td>

<script type="text/javascript">

function update_function()

{

var comm_id=$("#comm_id").val();

var comm_type=$("#comm_type").val();

var dataString = "comm_id=" + comm_id + "&comm_type=" + comm_type;

    $.ajax({ 

        type: "POST", 

        url: "update_comm_type.php", 

        data: dataString,

        beforeSend: function()

        {

            $('html, body').animate({scrollTop:0}, 'slow');

            $("#response").html('<div class="prev_box">Loading...<br clear="all"><br clear="all">');

        }, 

        success: function(response)

        {

            $("#response").html(response);

        }

    });

}

</script>

</tr>

</table>

</form>

</div>

<div id="response" class="testbox"></div>

<?php

}

and the php to go along with it. - update_comm.php

<?php

require_once('conn.php');

$id = $_POST['comm_id'];

$comm_type = $_POST['comm_type'];

$result = mysql_query( "UPDATE commission_type

        SET comm_type = '$comm_type'

        WHERE comm_id = '$id' ");

if($result == 1)

{

echo "Record Updated Sucessfully";

echo "<br>record id - ".$id;

echo"<br>Comm type - ".$comm_type; 

}

else

{

echo 'Try Again';

echo " ". mysql_error() ." ";  

}

?>

This topic has been closed for replies.