Jump to content

Dynamic Form Save


SH1989

Recommended Posts

Hi All,

Come a little stuck with an issue and hoping someone can shed some light on it.

Basically, I have a form that pulls down the list of users and then pulls down a list of jobs that can be assigned to them. I can get the form to pull all this down, but when I post the data, I am unsure of how to process this for each user and insert into the database.

I have tried adding the [] onto the field name, but this doesn't work as one of the dropdowns that you choose the position for, populates another dropdown of site addresses you are allocating too.

I have attached the Script to see if anyone is able to assist with any guidance.

$userClient = "VARIABLE OF WHO IS LOGGED IN";  
<form action="actions/assign.php" method="post">
	  <?php 
	  $sql = "SELECT * FROM Engineers where Engineer_Company = '".addslashes($userClient)."' and userType = '5'";
	  $result = mysqli_query($db, $sql);
	  
	  while($row = mysqli_fetch_assoc($result)) {
		  		$userSID = $row["userID"];
		  echo '<tr>
		  			<td>'.$row["userName"].'    
					
					<input type="hidden" class="form-control"  id="usersID[]" name="usersID'.$row["userID"].'" value="'.$row["userID"].'">
					<input type="hidden" class="form-control"  id="date" name="date" value="'.date('d-m-Y').'">
</td>
					<td>';
		  
		  			$sql1 = "SELECT * FROM job_orders where companyID = '".addslashes($userClient)."'";
					$result1 = mysqli_query($db, $sql1);
	  				echo "<select style='width: 250px;' id='job$userSID' name='job$userSID'>";
		  			echo '<option value="">-- Please Select --</option>';

	  				while($row1 = mysqli_fetch_assoc($result1)) {
		  				echo '<option value="'.$row1["jobID"].'">'.$row1["jobtitle"].'</option>';
					}
	  
	  	  echo "</select>";	  
		  echo '</td>
		  
		  
		  <td>'; 
				  
	
		  
    $query = $db->query("SELECT * FROM Engineer_Company WHERE Company_ID = '".addslashes($userClient)."'");
    $rowCount = $query->num_rows;

        if($rowCount > 0){
			echo '    <select name="company'.$userSID.'" id="company'.$userSID.'" style="width: 250px;">
        <option value="">-- Select Company --</option>';
            while($row = $query->fetch_assoc()){ 
                echo '<option value="'.$row['Engineer_Company_ID'].'" class="form-control form-control-sm">'.$row['Engineer_Company_Name'].'</option>';
            }
			echo '</select>';
        }
   
		  
		  
		  echo'</td>
		  
		    <td>
			
			<div class="form-group">


    <select id="site'.$userSID.'" name="site'.$userSID.'"  style="width: 250px;">
        <option value="">-- Select Site --</option>
                              </select>
           
	
	
	
    </div>
			'; 
				  
	echo'</td>
    </tr>';

		  
		  echo '    <script type="text/javascript">
$(document).ready(function(){
    $("#company'.$userSID.'").on("change",function(){
        var diag_id = $(this).val();
        if(diag_id){
            $.ajax({
                type:"POST",
                url:"ajax-client-side.php",
                data:"diag_id="+diag_id,
                success:function(html){
                    $("#site'.$userSID.'").html(html);
                }
            }); 
        }
    });
    

});
</script>';
	  }
	  ?>

  </tbody>
</table>
	
			<input class="btn btn-dark float-right" type="submit" value="Allocate">

			  </form>
			


Cheers!

Link to comment
Share on other sites

sorry for all the negativity in this reply.

code that doesn't work and doesn't contain any useful comments, doesn't tell us what the expected result is.

it would be more helpful if you posted an .sql dump of some sample data and show or describe what result you expect based on that sample data.

some comments about the posted code -

  1. be consistent in what you name same meaning values. the company_id value is being referred to as - userClient, Engineer_Company, companyID, and Company_ID.
  2. don't use addslashes() at all. use a prepared query when supplying external/unknown data to an sql query statement.
  3. don't run SELECT queries inside of loops.
  4. don't run the same SELECT query multiple times.
  5. don't mingle sql specific 'business' logic, that knows how to query for and retrieve data, with the 'presentation' logic. 
  6. don't copy variables to other variables without any reason.
  7. don't write unnecessary html markup.
  8. don't pass unnecessary values through forms.
  9. don't echo static html.
Link to comment
Share on other sites

Your form should contain a hidden field for the clientID then three selects for each engineer, indexed by the engineers' ids

hidden name=clientID value=$clientID

For each engineer:

    select name=job[$engID]
    select name=company[$engID]
    select name=site[$engID]

To process the POST data

foreach ($_POST['job' as $engID => $job) {
    $company = $_POST['company''][$engID'];
    $site = $_POST['site'][$engID];

    // update database with the engineer, job, company, site values
}

 

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.