3

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?"

5
  • 2
    unless you literally want your two field names to be named with the chars $, p, r, o, etc..., you probably want to surround them with <?= and ?>... Commented Dec 24, 2015 at 17:29
  • On newer versions of php it seems that long tags are compulsory so it would need to be name="<?php echo $product['Val2'] ?>" and the same on other inputs, classes etc. htmlspecialchars() should probably also be used around the data as you will be echoing it. Commented Dec 24, 2015 at 22:34
  • @SheaPrice If you are new to php this might be of some use. Good luck! stackoverflow.com/questions/34321429/… Commented Dec 25, 2015 at 14:55
  • There is a possibility that your php set-up doesn't support short tags if thhis scrit has never worked properly programmers.stackexchange.com/questions/151661/… might explain that. Commented Dec 25, 2015 at 15:12
  • Thanks everyone, I won't be able to test this until next week, I will update once I have resolved the issue. Commented Dec 29, 2015 at 17:32

4 Answers 4

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

I've tried this and I get an error that goes to a different portion of my code (which works flawlessly until I try to implement this). *Invalid argument supplied for foreach() in www.index.php line xx
Well, it means there's something with foreach ($<this_var> as $something) {
You are absolutely correct... any idea how to troubleshoot that? I've been messing with it for 2 days and I'm not getting anywhere.
Can you var_dump the $results variable?
Yes @switcher. The $results variable is correct. It parses a json string. I'm wondering if the issue could be on the other side of the code (the backend that's pushing me information) and how it receives these inputs? Is that a plausible issue?
1

"$" 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
        }
    }
?>

2 Comments

I believe this may be what I am wanting. I will test next week and update. Thank you.
None of these have worked for me, and ended up unfortunately breaking my working code.
1

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();

Comments

0

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.   
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.