How do i dynamically add textboxes without refreshing the page and then insert that into the database? :   

 - I want to be able to dynamically add text fields whenever the button is clicked
 - I want to be able to get the data from the text fields and insert it into the database.
 - As of now everything else works and I just need this, maybe I can also add a remove button to remove the last text field dynamically also
 - the reason why I need this to be dynamic is because I don't want to refresh the page cause all the data will be lost

 
 

    
      
    <html>
        <form action="checklist3.php"  method="post">
        <button type='submit' name='submit' id='buttonParent'>Submit</button>
        <button type="submit" name="back">Back</button>
        
        <?php
        session_start();
        require_once('../mysql_connect.php');
        $rowarray=$_SESSION['rowarray'];
        $dishname=$_SESSION['dishname'];
        	
        
                       echo
                          '<br><br><br><br><br>
                            <div class = "Table">
                              <table border = "2pt solid black" align = "left" cellpadding = "2px"
                                     bordercolor = black>
                              <tr>
        						  <td width = "7%">
                                  <div align = "left"><b>BRAND NAME</div></b>
                                </td>
        						<td width = "7%">
                                  <div align = "left"><b>INGREDIENT</div></b>
                                </td>
                              
        						<td width = "3%">
                                  <div align = "left"><b>QUANTITY</div></b>
                                </td>
                               
        						<td width = "7%">
                                  <div align = "left"><b>MEASUREMENT</div></b>
                                </td>
        						
                              </tr>';
        					 
        		for($x=0;$x<sizeof($rowarray);$x++)
        		{
        			
        			  $query = "select R.name AS RAWNAME, I.name AS INGREDIENTNAME, R.quantity AS RAWQUANTITY from rawmaterial R JOIN ingredient I ON R.ingredient_id = I.ingredient_id where R.rawmaterial_id='{$rowarray[$x]}'";
                      $res = mysqli_query($dbc, $query);
        		  while($fetch = mysqli_fetch_array($res, MYSQL_ASSOC))
        		  {
        			
        			 
        			  
        			  echo "<tr>
        						
        						<td width=\"7%\">
        						  <div align=\"left\">{$fetch['RAWNAME']}</div>
        						</td>
        						<td width=\"3%\">
        						  <div align=\"left\">{$fetch['INGREDIENTNAME']}</div>
        						</td>
        						<td width=\"3%\">
        						  <div align=\"left\"><input type='name' name='quantity[]' placeholder={$fetch['RAWQUANTITY']}></input></div>
        						</td>
        						<td width=\"7%\">
        						  <div align=\"left\">";
        						  echo "<select class='measure[]' name = 'measure[]'>";
        							$mesr = mysqli_query($dbc, 'select measure from measure_ref');
        							while($row=mysqli_fetch_array($mesr,MYSQLI_ASSOC))
        							{
        							$mes=$row['measure'];
        							echo '<option value ='.$mes.'>'.$mes.'</option>';
        							}
        							echo "</select>";
        						 echo" </div>";
        						echo"</td>";
        					echo"</tr>";	
        			
        					
        			  }		
        
        		}	  
                 
        	
                      echo '</table></div>';
        ?>
        
        <?php		 
        	if(isset($_POST['submit']))
        	{
        		$quantarray=array();
        		$quant=$_POST['quantity'];
        		$row2=array();
        		foreach($quant as $row2)
        		{
        			
        			array_push($quantarray,$row2);
        			
        		}
        		//error checking for negative quantities
        		for($x=0;$x<sizeof($quantarray);$x++)
        		{
        			$select="select quantity from rawmaterial where rawmaterial_id='{$rowarray[$x]}'";
        			$resselect=mysqli_query($dbc,$select);
        			while($row=mysqli_fetch_array($resselect,MYSQLI_ASSOC))
        			{
        					$q=$row['quantity'];
        				if($q-$quantarray[$x]<0)
        				{
        				$quantarray=array();
        				$row2=array();
        				echo 'insufficient quantity';
        				}
        			}
        			
        		}
        	$messarray=array();
        		$mes=$_POST['measure'];
        		$row=array();
        		foreach($mes as $row)
        		{
        			
        			array_push($messarray,$row);
        			
        		}
        		print_r($messarray);
        		print_r($quantarray);
        		$size=sizeof($messarray);
        		for($x=0;$x<$size;$x++)
        		{
        			$getquant="select quantity from rawmaterial where rawmaterial_id='{$rowarray[$x]}'";
        			$resquant=mysqli_query($dbc,$getquant);
        			
        			$row = mysqli_fetch_array($resquant, MYSQL_ASSOC);
        			$newquant=$row['quantity']-$quantarray[$x];
        			echo $newquant;
        
        			$q2="UPDATE  rawmaterial set measure='{$messarray[$x]}', quantity='{$newquant}' where rawmaterial_id='{$rowarray[$x]}'";
        			$res2=mysqli_query($dbc,$q2);
        
        			$select="select * from rawmaterial where rawmaterial_id='{$rowarray[$x]}'";
        			$resselect=mysqli_query($dbc,$select);
        			
        			$fetch2 = mysqli_fetch_array($resselect, MYSQL_ASSOC);
        			$rmid=$fetch2['rawmaterial_id'];
        
        			$getrecipeid="select recipe_id from recipe where recipe_name='{$dishname}'";
        			$recipeid=mysqli_query($dbc,$getrecipeid);
        			$fetch3 = mysqli_fetch_array($recipeid, MYSQL_ASSOC);
        			$recid=$fetch3['recipe_id'];
        			$q="insert into recipe_items (recipe_id, rawmaterial_id,quantity,measure) values('{$recid}','{$rmid}', '{$quantarray[$x]}' , '{$messarray[$x]}')";
        			$result = mysqli_query($dbc, $q);
        		}
        		
        	}
        		
        			echo '<br>';
        			echo "Add Procedures";
        			
        		 
        		echo  '<a href="#" id="plus_row">Add Row</a>';<---------------- This is the add button
        		echo "<input type='button' value='Remove Button' id='removeButton'>";
        		echo "<body>";
        		
        
        echo "</body>";
        	
        	if(isset($_POST['home']))
        	{
        		header("Location: http://".$_SERVER['HTTP_HOST'].  dirname($_SERVER['PHP_SELF'])."/chefmenu.php");
        	}
        	if(isset($_POST['back']))
        	{
        		header("Location: http://".$_SERVER['HTTP_HOST'].  dirname($_SERVER['PHP_SELF'])."/checklist2.php");
        	}
        	if(isset($_POST['add']))
        	{
        		[THIS IS WHERE THE CODE SHOULD BE]
        	}
        	
        ?>
        
        </form> 
        
        
        			
        </html>