0

Hi...

I want to display associative using variable as its key value.
I used the following script.
But it is not functioning.

<body>
<?php
if(isset($_POST['st']))
{
while ($item = current($_POST)) 
{

        echo $_POST[$key];

    next($_POST);
}
}
?>
<form id="form1" name="form1" method="post" action="mysql_inj.php">
  <p>
    <label for="textfield"></label>
    <input type="text" name="t1" id="textfield" />
  </p>
  <p>
    <input type="text" name="t2" id="textfield2" />
  </p>
  <input type="submit" name="st" id="textfield3" />
</form>
</body>
</html>
4
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by ko ko
0

I am presuming your are allocating a value to your $item variable somewhere above this code. But you have two problems, the first being that if you are going to use the While loop you need to use == not just =:

while ($item == current($_POST))

Secondly, you cannot just echo $_POST[$key] as $key has nothing assigned to it.

It would be much better to use a foreach loop:

foreach ($_POST as $key => $value) {
    if ($item == $value) {
        echo $value
    }
}
0

take a look at array_keys

maybe something like...

$array_keys = array_keys($_POST);
foreach($array_keys as $key){
   echo $key." = ".$_POST[$key]; // or whatever you want to do here
}
0

Sorry Its my code mistake.Actually the code is

    <?php


        > if(isset($_POST['st']))
        > {
        > while ($item = current($_POST))
        > {
          $key=key($_POST);
        > echo $_POST[$key];
        > next($_POST);
        > }
        > }
        > ?>

Thanks for help

Edited by divyakrishnan

0
foreach ($_POST as $key => $value) {
     echo $value;
}

What's still wrong ? The above method should work.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.