0

I'm trying to post array data to another php, but it doesn't work..

here is my code below.

in func.php

function search($x, $y)
{

  ...

  $martx = array();
  $marty = array();


  foreach($load_string->channel as $channel) {
    foreach($channel->item as $item) {
      array_push($martx, $item->mapx);
      array_push($marty, $item->mapy);

    }
  }

    echo"<form method=post action='map.php'>";
    for($i = 0; $i < 8; $i++)
    {
      //echo $martx[$i]."<br/>";
      //echo $marty[$i]."<br/>";
      echo "<input type='hidden' name='martx[]' value='".$martx[$i]."'>";
      echo "<input type='hidden' name='marty[]' value='".$marty[$i]."'>";
    }
    echo "</form>";

    header("location: map.php?x=$x&y=$y");
}

martx and marty have data from parsed xml $load_string

And I want to post these data to map.php by form with POST. So, I expect that I can use two arrays martx and marty in map.php like $_POST[martx][0]..

But when I run this code, page remains in func.php, instead of redirecting to map.php

Am I make some mistake?

Thanks in advance.

========================================================================

Thank you all for your kind concern and helpful advice!

I edit my code with your advice,

  1. I delete all echo with using javascript

  2. And I add submit code

here is my code below

....

$martx = array();
$marty = array();

foreach($load_string->channel as $channel) {
foreach($channel->item as $item) {
  array_push($martx, $item->mapx);
  array_push($marty, $item->mapy);

  }
}

?>
<form method='post' action='map.php?x=<?=$x?>&y=<?=$y?>' id='market'>
  <script language="javascript">
    for(i = 0; i < 8; i++)
    {
      document.write('<input type="hidden" name="martx[]" value="<?=$martx[i]?>">');
      document.write('<input type="hidden" name="marty[]" value="<?=$marty[i]?>">');
    }
    document.getElementById('market').submit();
    </script>
</form>
<?php
//header("location: map.php?x=$x&y=$y");

}

With this code, page redirect to map.php successfully.

But I can't get data like $_POST['martx'][i] in map.php

I think document.write line cause problem

when I write code like

document.write('<input type="hidden" name="martx[]" value="$martx[i]">');

result of $_POST['martx'][i] is "$martx[i]"

Is any error in this code?

I want to use POST method but if I can't post data with POST,

then I'll use session-method as @Amit Ray and @weigreen suggested.

thanks again for your concern.

furyhunter
  • 129
  • 13

2 Answers2

0

First, You try to use header('location: xxx') to redirect user to another page.

As the result, you are not submitting the form, so you won't get data like $_POST[martx][0] as you expect.

Maybe you should try using session.

weigreen
  • 1,232
  • 8
  • 13
  • Actually, the `header()` will be ignored because it's after he echoed. See http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Barmar Jun 04 '16 at 12:28
0

I can see some errors that you are making when you are doing header redirect. There should be no output before you call header redirect but you are doing echo before redirect. use session to tackle this issue

function search($x, $y)
{

  ...

  $martx = array();
  $marty = array();


  foreach($load_string->channel as $channel) {
    foreach($channel->item as $item) {
      array_push($martx, $item->mapx);
      array_push($marty, $item->mapy);

    }
  }

 if (session_status() == PHP_SESSION_NONE) {
    session_start();
 }
 $_SESSION["martx"] = $martx;
 $_SESSION["marty"] = $marty;
 header("location: map.php"); exit;

then in map.php you can retrieve the session variables

if (session_status() == PHP_SESSION_NONE) {
        session_start();
 }
$martx = $_SESSION["martx"];
$marty = $_SESSION["marty"];

Then you can use a for loop or foreach loop to iterate through the values

Amit Ray
  • 3,445
  • 2
  • 19
  • 35