Jump to content

My first attempt at AJAX, empty array


Drewser33

Recommended Posts

What I am trying to do is to submit as POST values to database_write.php, from within the while statement.  What is happening is I am getting the second row of data every time I change the primary button.

 

Currently database_write.php is just doing print_r($_POST), And my array is always the same, no matter which select box I choose from.  How can I get the values to be associated with the row I am currently changing? Any help would be great, thanks.

 

What I have so far:

				<table class="table table-bordered table-hover">
				<thead>
			    	<th>Room Number</th>
			    	<th>Primary Caregiver</th>
			    	<th>Seconday Caregiver</th>
				</thead>
				<tbody class="list">
			<?php                           
				$sql = 'SELECT alarm_device_id, alarm_description, alarm_device_type, notes FROM alarm_device where notes in (\'MSU\') ORDER BY alarm_description';

				$retval = mysql_query( $sql, $con );
				if(! $retval )
				{
				  die('Could not get data: ' . mysql_error());
				}
				$x=0;
				while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
				{
					$id = $row['alarm_device_id'];
					$alarm_description = $row['alarm_description'];
					echo '<form id="msu_form">';
					echo "<tr><td>{$row['alarm_description']}</td>";
					echo "<td>";
					
					$query2 = "SELECT alert_device_id,alert_description FROM alert_device WHERE notes = 'MSU'";
					$result2 = mysql_query($query2) or die("Error in alarm_device select:" . mysql_error());
					$count2 = mysql_num_rows($result2);
					if($count2 > 0)
					{
						//echo '<select name='.$x.'>';
						echo '<select id="Primary" name="primary" onchange="doAjaxPost(this)">';
						while($row2 = mysql_fetch_array($result2))
						{
							echo "<option value=".$row2['alert_device_id'].">".$row2['alert_description']."</option>";
						}
						echo "</select>";
					}else
					{
						echo "Please update alert device to this area";
					}
					echo "</td>";
					echo "<td>";					
					$query3 = "SELECT alert_device_id,alert_description FROM alert_device WHERE notes = 'MSU2'";
					$result3 = mysql_query($query3) or die("Error in alarm_device select:" . mysql_error());
					$count3 = mysql_num_rows($result3);
					if($count3 > 0)
					{
						echo '<select id="Secondary" name="secondary">';
						while($row3 = mysql_fetch_array($result3))
						{
							echo "<option value=".$row3['alert_device_id'].">".$row3['alert_description']."</option>";
						}
						echo "</select>";
					}else
					{
						echo "Please update alert device to this area";
					}
					echo "</td>";
					$aid = $id + $x;
					//echo $aid;
					//$ad = $alarm_description + $x;
					echo '<input type="hidden" id="ID" name="ID" value="'.$id.'"/>';
					//echo '<input type="hidden" id="desc" name="desc" value="'.$ad.'"/>';
					//echo '<td>'."<input type='submit' name='btnupdate' value='UPDATE' /></td>";
					//echo '<td><input type="button" value="Ajax Request" onClick="doAjaxPost()"></td>';
					echo '</form>';
					$x = $x+1;
				 }
			?>
			        <script>  
          function doAjaxPost() {  
           // get the form values  
           var primary = $('#Primary').val();
           var secondary = $('#Secondary').val();
           var hidden = $('#ID').val();
           //var desc = $(sel).parent().nextAll('#desc').val();
           $.ajax({  
             type: "POST",  
             url: "functions/database_write.php",
             data: $('#msu_form').serialize(),
             //data: "Primary="+primary+"&Hidden="+hidden+"&Secondary="+secondary,
             success: function(resp){  
                //we have the response  
              alert("'" + resp + "'");  
             },  
             error: function(e){  
               alert('Error: ' + e);  
             }  
           });  
          }  
          </script>

				</tr>

				</tbody>
				</table>
Link to comment
Share on other sites

There's no need to create a form in a loop, making several forms.

I'm surprised that you get the "second" value, and not the last one.  Maybe you only have two loop iterations.

 

Put the form tag before the while loop, and the end form tag after the loop.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.