简体   繁体   中英

PHP code isn't allowing multiple input values to send through my form

I am running into a wall with an application I have built. I am brand new to PHP (less than 1 month) and I have written a very complex form with the help of a mentor. Due to a confidentiality agreement I cannot send my entire code here for an example. Specifically the issue I am having is that my form isn't able to send multiple values to two different input "slots". It will send a singular input value to the database, but it should be registering the different values inputted.

<?php                        
{
foreach($results['tags'] as $part){
if ($part['category'] == "Part"){
?>
<tr>
<td><?= $part['Part']; ?></td>
<td class="text-center"><?= $product['Amount']; ?></td>
<td><input type="text" name=$product['Val1'] /></td>
<td><input type="text" name=$product['Val2'] /></td>
</tr>

<?php
    }
      }

         }

            ?>

My mentor suggested this as an answer but I am not sure what he means: "It seems like the [val1/2] needs to be tied to the product instead of the transaction. Right now, it's not inside that “tags” section. Does that make sense?"

You're trying to name your vars with $ . I suppose you want to output php values there, so change your string to look like this:

<td><input type="text" name="<?= $product['Val1'] ?>"/></td>

"$" represents PHP variable and hence it needs to be inside the php tag. Also remember to enclose the echo value with quotes(").

<?php       
     foreach($results['tags'] as $part) {
        if ($part['category'] == "Part") {
?>
<tr>
    <td><?= $part['Part']; ?></td>
    <td class="text-center"><?= $product['Amount']; ?></td>
    <td><input type="text" name="<?= $product['Val1']; ?>" /></td>
    <td><input type="text" name="<?= $product['Val2']; ?>" /></td>
</tr>
<?php
        }
    }
?>

The answer was I needed to iterate them since they aren't arrays and append them to the string.

This was accomplished thanks to @SonnY

  <script type="text/javascript">
function sendData() {
  var inputs = document.getElementById('equipment-table').getElementsByTagName('input'),
      data = [],
      name, val1, val2;

  for (var i = 0; i < inputs.length; i++) {
    if ( inputs[i].type === 'submit') {
      continue;
    }

    if ( inputs[i].value ) {
      name = inputs[i].name.split('-val');
      val1 = inputs[i].name.split('val1');

      if (val1.length > 1) {
        data.push({name: name[0], val1: inputs[i].value});
      }
      else {
        data.push({name: name[0], val2: inputs[i].value});
      }
    }
  }

  window.setTimeout(function() {
    sendData();
  },30000);
}

sendData();

Merry Christmas

Since, Not Much Data Provided. I am assuming from my side. Hope it helps.

Place the value in value attribute. Give name as array type. Like Slot1[] , Slot2[] .. Since, There may be more than 1 value for Val1 , so name is of array type.

<form action='SubmitSomePage.php' method='POST'>

    <?php                        
    {
        foreach($results['tags'] as $part)
        {
            if ($part['category'] == "Part")
            {?>
                <tr>
                    <td><?= $part['Part']; ?></td>
                    <td class="text-center"><?= $product['Amount']; ?></td>
                    <td><input type="text" value="<?=$product['Val1'];?>" name='Slot1[]'/></td>
                    <td><input type="text" value="<?= $product['Val2'];?>" name='Slot2[]' /></td>
                </tr>
                <?php
            }
        }
    }
    ?>
    .
    .
    <input type='submit' value='Submit Details'>

</form>

SubmitSomePage.php

<?

$slot1 = $_POST['Slot1'];
$slot2 = $_POST['Slot2'];

$TotalSlot1 = sizeof($slot1);

for($i = 0; $i<$TotalSlot1;$i++)
{
    $slot1Value = $slot1[$i];       
    $slot2Value = $slot2[$i];

    $Query = "INSERT INTO TableName Set Val1='$slot1Value', Val2='$slot2Value'";
    //Execute Query Here.   
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM