FlyingCat
FlyingCat

Reputation: 14250

Weird php form issue

I am having a simple but weird issue about post and form elements.

I have a form

<form id='form' name='form' method='post' action='test.php'>


      <?php
          foreach ($tests as $test){
              echo $test['ID']."<br>"; //output 1  2  3  4  5   6
              echo $_POST['testSelect'].'<br>';  //output 3 3 3 3 3 3

              if($test['ID'] == $_POST['testSelect']){  //which 3 mataches 3
                  echo 'match';
              }
          }
      ?>



    <select name='testSelect'>
      <?php
          foreach ($tests as $test){
              echo '<option value="'.$test['ID'].'">'.$test['Name'].'</option>';
          }
      ?>
    </select>
     <input type='submit' value='Go'></input>

I want to match $_POST and my variable and output "match" when it matches.

However, I don't see 'Match' shows in my output. It doesn't make sense at all!

Can Anyone help? Thanks a lot

Upvotes: 0

Views: 65

Answers (3)

nl-x
nl-x

Reputation: 11832

I think this has to do with the Post param being a String and the Test param probably being an Integer. Put parseInt() around the Post variable

Upvotes: 1

Charles R. Portwood II
Charles R. Portwood II

Reputation: 750

Are you sure you're actually performing a POST request? Turning on error_reporting might help too.

error_reporting(-1);
ini_set('display_errors', 1);

Upvotes: 1

Twisted1919
Twisted1919

Reputation: 2499

Maybe just try like

if((int)trim($test['ID']) == (int)trim($_POST['testSelect'])){  
  echo 'Matched';
}

Upvotes: 1

Related Questions