Jump to content

passing multidimensional array onto a hidden field woes


ukweb

Recommended Posts

Hi all

 

Basically, I have a form which when done will calculate a quote for turf for given areas. Basically each time the user specifies an area it goes into an array, and they can add as many as they want, but the problem seems to be once submitted and it arrives back into the program, it isn't decoding so the multidimensional array is lost. What am I doing wrong here?

 

Full code below, thanks in advance

 

Ste

<?php // Framework

// Get the sections
$sections_array = base64_decode(unserialize($_POST['sections']));

// Build the rest of the array
foreach ($sections_array as $section) {

$sections[] = base64_decode(unserialize($section));
}

// Set the initial unit
$units = $_POST['units'];

// Do the new add
if ($_POST['a'] == 'add_section') {

// Add the new measure
if ((is_numeric($_POST['width'])) && (is_numeric($_POST['length']))) {

	$sections[] = array('length' => $_POST['length'],
					    'width' => $_POST['width']);
}
}

// Build the output
foreach ($sections as $section) {

$output .= '<strong>'.$section['width'].' wide</strong> x <strong>'.$section['length'].' long</strong><br />';
$sections_array = base64_encode(serialize($section));
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
body,td,th {
font-family: Verdana, Geneva, sans-serif;
font-size: 11px;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.wrap {
text-align: center;
height: 350px;
width: 410px;
padding: 0 10px;
}
</style>
</head>

<body>
<div class="wrap">
  <h2>Turf Calculator</h2>
  <p>To begin, enter the width and length of the first area of garden you wish to turf. You can keep adding sections to work out the final quantity required.</p>
  <p><?php echo $output; ?></p>
  <form id="form1" name="form1" method="post" action="">
    <table width="0" border="0" cellspacing="0" cellpadding="2">
      <tr>
        <td>Length:</td>
        <td><input type="text" name="length" id="length" /></td>
      </tr>
      <tr>
        <td>Width:</td>
        <td><input type="text" name="width" id="width" /></td>
      </tr>
      <?php if ($units == '') { ?>
      <tr>
        <td>Units:</td>
        <td><select name="units" id="units">
          <option value="feet">Feet</option>
          <option value="m" selected="selected">Metres</option>
          <option value="yards">Yards</option>
        </select></td>
      </tr>
      <?php } ?>
    </table>
    <p>
      <input type="submit" name="add" id="add" value="Add This Section" />
      <input name="a" type="hidden" id="a" value="add_section" />
      <input name="sections" type="hidden" id="sections" value="<?php echo base64_encode(serialize($sections_array)); ?>" />
      <?php if ($units != '') { ?>
      <input name="units" type="hidden" id="units" value="<?php echo $units; ?>" />
      <?php } ?>
    </p>
  </form>
  <p> </p>
</div>
</body>
</html>

 

Link to comment
Share on other sites

This line:

 

$sections_array = base64_encode(serialize($section));

 

should probably be

 

$sections_array[] = base64_encode(serialize($section));

 

Give that a go.  It would be simpler if you used sessions though, I think.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.