Variable in $_POST

Hi guys, just a quick one, how do I go about echoing the value of the posted field. I’m using PHP_SELF to process the form so wonder if this has anything to do with it. When I press submit, nothing is returned to the page, I loose all the results. Any help massively appreciated! :smiley:


$items = isset($_GET['items']) && ((int)$_GET['items'] > 0) ? (int)$_GET['items'] : 1 ;
<form action="/createorder?items=<?=$items?>" method="post" id="createorder">
<?php foreach(range(0, $items) as $num): ?>
<!-- BOF Product Info -->
<div class="productInfo">
<div class="prodContain">
<span class="fontField bold">Product Description</span>
<input type="text" name="pdname[<?php echo $num; ?>]" value="<?php echo $_POST["pdname[$num]"] ?>" class="prodName" style="width: 280px" />
</div></div>
<!-- EOF Product Info -->
<?php endforeach; ?>
</form>

Like this? http://dev.anthonysterling.com/sp/?items=5


<?php
error_reporting(-1);
ini_set('display_errors', true);

function get_items(){
  $items = array();

  $upper = isset($_GET['items']) && ((int)$_GET['items'] > 0) ? (int)$_GET['items'] : 1 ;

  foreach(range(1, $upper) as $index){

    $value = '';

    if(isset($_POST['pdname']) && is_array($_POST['pdname'])){
      if(isset($_POST['pdname'][$index])){
        $value = $_POST['pdname'][$index];
      }
    }

    array_push(
      $items,
      array(
        'name'  => sprintf('pdname[%d]', $index),
        'value' => $value
      )
    );

  }
  return $items;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.2.0/build/cssreset/reset-min.css" />
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.2.0/build/cssbase/base-min.css" />
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.2.0/build/cssfonts/fonts-min.css" />
    <title>
      SitePoint
    </title>
    <style type="text/css">

    </style>
  </head>
  <body>
    <form action="" method="post">
      <?php foreach(get_items() as $item): ?>
        <input type="text" name="<?php echo $item['name']; ?>" value="<?php echo $item['value']; ?>" /><br />
      <?php endforeach; ?>
      <input type="submit" value="submit" />
    </form>
  </body>
</html>

Hey dude, yeah bang on, exactly like that! :slight_smile: What did I do wrong :S

At first guess, I would say that pdname[0] doesn’t exist because your $items array starts with $items[1].

A quick view of the outputted html should confirm, or increase ?items= and you’ll probably see the input incorrectly offset.

Ahh right, cool I get you, all working beautifully now. Cheers again, massive help.