Jump to content

Submit large form created with select statement


Go to solution Solved by Adamhumbug,

Recommended Posts

Hi,

I have a very large form that has been created with a select statement.

When i am submitting the form and running the sql insert, i obviously cant list out all of the fields like you would for a small form.

How would one go about submitting a form with a large number of fields.

The code below is one of the lines of the table

<tr>
  <td style="width:70%;">
    <span value="1">Item</span>
  </td>
  <td class="text-center">
    <input class="eqQty text-center up" type="text">
  </td>
</tr>

I am sure that the answer is faily simple but googling has not been friendly to me on this one.

Kind Regards

Hi,

Thanks for this.

So i have made some changes and have used serialise.

I have three things to import per line, the first is an ID and should be the same on every line.

The second is the value of a hidden input which will be different per line.

The third is the value of a text box which may be different on each line.

I do not want to include rows where the number in the input is zero or blank.

I have this

$jobId = $_SESSION['current_job_id'];
$equipmentId = serialize($_POST['equipmentId']);
$equipmentQty = serialize($_POST['equipmentQty']);



  $sql = "INSERT INTO ssm_equipment_order (job_id, equipment_id, equipment_quantity) VALUES ('$jobId', '$equipmentId', '$equipmentQty')";

but when printing $sql i get the following

INSERT INTO ssm_equipment_order (job_id, equipment_id, equipment_quantity) VALUES ('25', 'a:22:{i:0;s:1:"2";i:1;s:1:"1";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";i:5;s:1:"6";i:6;s:1:"7";i:7;s:1:"8";i:8;s:1:"9";i:9;s:2:"10";i:10;s:2:"11";i:11;s:2:"12";i:12;s:2:"13";i:13;s:2:"14";i:14;s:2:"15";i:15;s:2:"16";i:16;s:2:"17";i:17;s:2:"18";i:18;s:2:"19";i:19;s:2:"20";i:20;s:2:"21";i:21;s:2:"22";}', 'a:22:{i:0;s:2:"10";i:1;s:2:"10";i:2;s:0:"";i:3;s:0:"";i:4;s:0:"";i:5;s:0:"";i:6;s:0:"";i:7;s:0:"";i:8;s:0:"";i:9;s:0:"";i:10;s:0:"";i:11;s:0:"";i:12;s:0:"";i:13;s:0:"";i:14;s:0:"";i:15;s:0:"";i:16;s:0:"";i:17;s:0:"";i:18;s:0:"";i:19;s:0:"";i:20;s:0:"";i:21;s:0:"";}')

So i feel like i am on the right lines and will keep looking.  One thing i am not sure on is how to only include in the array rows where the equipmentQty is not 0 or blank.

So i changed the HTML to be the following to use data[ ]

<tr>
  <td style="width:70%;">Item 1</td>
  <input type="hidden" name="data[equipmentId]" value="1">
  <td class="text-center">
    <input name="data[equipmentQty]" class="eqQty text-center up" type="text">
  </td>
</tr>
<tr>
  <td style="width:70%;">Item 2</td>
  <input type="hidden" name="data[equipmentId]" value="2">
  <td class="text-center">
    <input name="data[equipmentQty]" class="eqQty text-center up" type="text">
  </td>
</tr>

And the php

$_POST['data'];
print_r($_POST['data']);

This gives me the following regardless of how many rows i type a quantity into, also, it is not putting the quantity into the array.  Also, equipment id 22 is the last item in the table.

Array ( [equipmentId] => 22 [equipmentQty] => )

A bit lost

Edited by Adamhumbug

You vastly improved your form code with your second posting but then you added some new problems.

If you are using an array for the multiple instances your input items then LEAVE OUT THE INDEX.  What good is it to define a single index for your array elements when you want to have multiple instances of that element?? They are all going to be id'ed as element 'equipmentId'.  (PS - leave out the up and lower cases in php - it's just a pia down the road.)

PPS - you still have a tag outside of the td elements that will get displaced when output.

Other than that - I have no idea what your goal is here.

you only need one form field per item. use a meaningful name for the field, such as qty, and use the item id as the field's array index value - name='qty[item id goes here]'. in the form processing code, use a foreach() loop. this will give you the item id and the submitted quantity for the id.

to remove empty items use array_filter() with no call back function (empty strings and zeros are false and will be removed.)

lastly, you need to insert a separate row in your table for each item and you need to use a prepared query, with place-holders in the sql query statement for each value, then supply the values when you execute the query. you would prepare the query once, before starting to loop over the data, then only get each set of values and execute the query inside of the loop.

try

form code

echo "<table>";
for ($i=1; $i<=5; $i++) {
    echo <<<FORM
        <tr>
          <td style="width:70%;">Item $i</td>
          <td class="text-center">
            <input type="hidden" name="equipmentId[$i]" value="$i">
            <input name="equipmentQty[$i]" class="eqQty text-center up" type="text">
          </td>
        </tr>
FORM;
}
echo "</table>";

processing code

if ($_SERVER['REQUEST_METHOD']=='POST') {
    $jobId = $_SESSION['current_job_id'];
    // prepare insert query
    $insert = $db->prepare("INSERT INTO ssm_equipment_order (job_id, equipment_id, equipment_quantity) VALUES (?,?,?) ");
    
    foreach ($_POST['equipmentId'] as $k => $eid) {
        if ($_POST['equipmentQty'][$k] > 0) {
            $data = [ $jobId, $eid, $_POST['equipmentQty'][$k] ] ;
            $insert->execute($data);
        }
    } 
}

 

  • 2 weeks later...

Hi Barand,

Thanks for the suggestion.  Is this in place of what i have to currently create my form.

			<?php 
					include '_includes/dbconn.php';

						$sql = "SELECT * FROM ssm_equipment";
						if ($result = mysqli_query($conn, $sql)){
							if(mysqli_num_rows($result)>0){

								echo "<form method='post' action='actions/submit-equipment-order-action.php' id='equipmentOrderForm'>";
								echo "<div class='input-group mt-3 mb-3'>";
								echo "<div class='input-group-prepend'>";
								echo "<span class='input-group-text'>Search (alt/option+s)</span>";
								echo "</div>";
								echo "<input id='equipmentTableSearch' onkeyup='searchEquipmentTable()' type='text' class='form-control' placeholder='Plates...Soup Spoon...Red Wine Glass...'>";
								echo "<button type='submit' name='equipment_submit_button' class='btn btn-primary'>Submit</button'>";
								echo "</div>";
								echo "<table id='equipmentTable' class='mt-3 table table-striped table-hover table-bordered'>";
								echo "<thead>";
								echo "<tr class='text-center'><th>Equipment</th><th>Quantity</th></tr>";
								echo "</thead>";
								echo "<tbody>";
								while ($row = mysqli_fetch_array($result)){
									echo "<tr>";
									echo "<td style='width:70%;'>".$row['equipment_name']."</td>";
									echo "<input type='hidden' name='equipmentId[]' value='".$row['equipment_id']."'>";
									echo "<td class='text-center'><input name='equipmentQty[]' class='eqQty text-center up' type='text'></td>";
									echo "</tr>";
								}
								echo "</tbody>";
								echo "</table>";
								
								echo "</form>";

							}

						}

			?>

 

  • 3 weeks later...
  • Solution

So, thank you all for your support here, i am now able to submit this large form successfully.

 

The submission code is as follows.

 

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (session_status() == PHP_SESSION_NONE) {
  session_start();
}

if (!isset($_SESSION['user_name'])){
	header("location: index.php");
}

include '../_includes/dbconn.php';	
 
if ($_SERVER['REQUEST_METHOD']=='POST') {
    $jobId = $_SESSION['current_job_id'];
    $qty = $_POST['equipmentQty'];
    // prepare insert query
    $stmt = $conn->prepare("INSERT INTO ssm_equipment_order (job_id, equipment_id, equipment_quantity) VALUES (?,?,?) ");
    
    
    foreach ($_POST['equipmentId'] as $k => $eid) {

        if ($qty[$k] > 0) {
            // $data = [$jobId, $eid, $qty[$k] ] ;
            $stmt->bind_param("sss", $jobId, $eid, $qty[$k]);

            $stmt->execute();
        }

        
    } 
    
}

header("location: ../order-equipment.php");

My issue now is that when this page loads, it needs to get the information out of the database that builds the large form and fill the inputs with the quantities that were submitted before.

 

I have written the following:

 

<?php
					include '_includes/dbconn.php';
		
			$current_job_id = $_SESSION['current_job_id'];
		$sql = "SELECT * FROM ssm_equipment_order where job_id = $current_job_id";
		if ($result = mysqli_query($conn, $sql)){
			if(mysqli_num_rows($result)>0){
				$sql = "SELECT * FROM ssm_equipment";
						if ($result = mysqli_query($conn, $sql)){
							if(mysqli_num_rows($result)>0){
								echo "<form method='post' action='actions/submit-equipment-order-action.php' id='equipmentOrderForm'>";
									echo "<div class='input-group mt-3 mb-3'>";
									echo "<div class='input-group-prepend'>";
									echo "<span class='input-group-text'>Search (alt/option+s)</span>";
									echo "</div>";
									echo "<input id='equipmentTableSearch' onkeyup='searchEquipmentTable()' type='text' class='form-control' placeholder='Plates...Soup Spoon...Red Wine Glass...'>";
									echo "<button type='submit' name='equipment_submit_button' class='btn btn-primary'>Submit</button'>";
									echo "</div>";
									echo "<table id='equipmentTable' class='mt-3 table table-striped table-hover table-bordered'>";
									echo "<thead>";
									echo "<tr class='text-center'><th>Equipment</th><th>Quantity</th></tr>";
									echo "</thead>";
									echo "<tbody>";

									$current_job_id = $_SESSION['current_job_id'];
									$sql2 = "SELECT * FROM ssm_equipment_order WHERE job_id = $current_job_id";
									$result2 = mysqli_query($conn, $sql2);
						

											while ($row2 = mysqli_fetch_array($result2)){ echo "XX "; }
											while ($row = mysqli_fetch_array($result)){
												echo "<tr>";
												echo "<td style='width:70%;'>".$row['equipment_name']."</td>";
												echo "<input type='hidden' name='equipmentId[]' value='".$row['equipment_id']."'>";
												echo "<td class='text-center'><input name='equipmentQty[]' class='eqQty text-center up' type='text'></td>";
												echo "</tr>";
											}
										echo "</tbody>";
									echo "</table>";
									
								echo "</form>";
							}
						}
			}else{
				
	
		//the above is doing an if there is an equipment order already
						$sql = "SELECT * FROM ssm_equipment";
						if ($result = mysqli_query($conn, $sql)){
							if(mysqli_num_rows($result)>0){
								echo "<form method='post' action='actions/submit-equipment-order-action.php' id='equipmentOrderForm'>";
									echo "<div class='input-group mt-3 mb-3'>";
										echo "<div class='input-group-prepend'>";
											echo "<span class='input-group-text'>Search (alt/option+s)</span>";
										echo "</div>";
										echo "<input id='equipmentTableSearch' onkeyup='searchEquipmentTable()' type='text' class='form-control' placeholder='Plates...Soup Spoon...Red Wine Glass...'>";
										echo "<button type='submit' name='equipment_submit_button' class='btn btn-primary'>Submit</button'>";
									echo "</div>";
									echo "<table id='equipmentTable' class='mt-3 table table-striped table-hover table-bordered'>";
										echo "<thead>";
											echo "<tr class='text-center'><th>Equipment</th><th>Quantity</th></tr>";
										echo "</thead>";
										echo "<tbody>";
											while ($row = mysqli_fetch_array($result)){
												echo "<tr>";
													echo "<td style='width:70%;'>".$row['equipment_name']."</td>";
													echo "<input type='hidden' name='equipmentId[]' value='".$row['equipment_id']."'>";
													echo "<td class='text-center'><input name='equipmentQty[]' class='eqQty text-center up' type='text'></td>";
												echo "</tr>";
											}
										echo "</tbody>";
									echo "</table>";
									
								echo "</form>";
							}
						}
					}
				}
?>

The following line is successfully showing the correct number of results, for how many items have a qty but i do not know how to use this information to populate the correct text boxes in the table.

while ($row2 = mysqli_fetch_array($result2)){ echo "XX "; }

 

Edited by Adamhumbug
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.