Problem making Pie form

Hi,

The program supposed to allow user to enter 1-10 categories to made the pie chart.

I’m stuck with the problem in my php code, not sure whats the actual problem is but it I think somehow the data didn’t manage to get into PHP code.

here is my PHP code:

[php]

<?php function handleError() { // Redirect browser to error page if anything seems wrong with the // input header("Location: Errors.html"); exit; } //check the data - submitted in two text area form elements // was there any data $cats = $_POST["catlist"]; $vals = $_POST["numlist"]; $title = $_POST["title"]; echo $cats; echo $vals; if(empty($cats) || empty($vals) || empty($title)) { handleError(); } // There was some data. // Supposed to be a series of lines, try splitting on newline $categories = split("\n", $cats); $values = split("\n",$vals); // The number of numeric values must equal the number of // category values $numc = count($categories); $numv = count($value); if($numc != $numv) { handleError(); } if(($numc<1) || ($numc>10)) { handleError(); } // Check the values // Use regex checks $numpat = '/^[0-9]+\.?[0-9]*$/'; $catpat = '/^[A-Za-z0-9 ]+$/'; for($i=0;$i<$numc;$i++) { $tmpc = rtrim($categories[$i]); $tmpv = rtrim($values[$i]); if(!preg_match($catpat,$tmpc)) { handleError(); } if(!preg_match($numpat,$tmpv)) { handleError(); } } $titlepat = '/^[A-Za-z0-9,; ]+$/'; if(!preg_match($titlepat,$title)) { handleError(); } // data look ok // echo "Data ok"; echo $cats; echo $vals; //header("Content-type: image/png"); $width = 1000; $height = 700; $im = imagecreate($width, $height); $white = imagecolorallocate ($im, 255, 255, 255); $black = imagecolorallocate ($im, 0, 0, 0); $color[] = imagecolorallocate ($im, 0, 255, 255);//cyan $color[] = imagecolorallocate ($im, 0, 0, 255);//blue $color[] = imagecolorallocate ($im, 255, 0, 255);//fuschia $color[] = imagecolorallocate ($im, 0, 128, 0);//green $color[] = imagecolorallocate ($im, 0, 255, 0);//lime $color[] = imagecolorallocate ($im, 128, 0, 0);//maroon $color[] = imagecolorallocate ($im, 0, 0, 128);//navy $color[] = imagecolorallocate ($im, 128, 128, 0);//olive $color[] = imagecolorallocate ($im, 128, 0, 128);//purple $color[] = imagecolorallocate ($im, 255, 0, 0);//red // font directory $fontdir = "C:\\Windows\\Fonts\\"; // font try Lucida Bright $font = "LBRITE.TTF"; $fd = $fontdir . $font; $titlex = 10; $titley = 50; $titleangle = 0; $ptsize = 32; imagettftext($im, $ptsize, $titleangle, $titlex, $titley, $black, $fd, $title); //imagepng($im); //imagedestroy($im); // Work out angles for pie graph $total = 0; for($i=0;$numv;$i++) { $tmpv = rtrim($values[$i]); $total += $tmpv; } $angles = array(); for($i=0;$numv;$i++) { $tmpv = rtrim($values[$i]); $anagle = ($tmpv*360)/$total; $angles[] = $anangle; } // Define circle - centre point, width, height $cx = 250; $cy = 250; $cwidth = 255; $cheight = 255; // Draw angles $startangle = 0; for($i=0;$numv;$i++) { $endangle = $startangle + $angles[$i]; $arccolor = $color[$i]; imagefilledarc($im, $cx, $cy, $cwidth, $cheight, $startangle, $endangle, $arccolor, IMG_ARC_PIE); $startangle = $endangle; } // Name table $left = 450; $top = 120; $step = 36; $ptsize = 20; $angle = 0; for($i=0;$numc;$i++) { $str = rtrim($categories[$i]); $txtcolor = $color[$i]; imagettftext($im, $ptsize, $angle, $left, $top, $txtcolor, $fd, $str); $top += $step; } header("Content-type: image/png"); imagepng($im); imagedestroy($im); ?>

[/php]

And this is is my HTML form and javascript to input the values into textarea



var numadded = 0;
function doAdd()
{
   // Pick up data from the category and value input fields;
   // In my form these are named 'cat' and 'val'
   var catstr = document.getElementById("cat").value;
   var valstr = document.getElementById("val").value;
  

   
   
   var numpattern = new RegExp("^[0-9]");
   // Check that the "value" field macthes the regex
   if(!numpattern.test(valstr)) {
      alert("non numeric data in value field" + catstr + valstr );
      return;
   } 
   // The other pattern says a string of letters, digits, and spaces
   var catpattern = new RegExp("^[A-Za-z0-9]");
   if(!catpattern.test(catstr)) {
       alert("Invalid data in category name field" );
       return;
   } 
   // If data look ok, pick up references to the textareas; in
   // my page these had id values catlist and numlist
   var cats = document.getElementById("catlist");
   var nums = document.getElementById("numlist");
   // Append text, inserting a new line character between
   // data sets
   if(numadded>0) {
       cats.value = cats.value + "\n";
       nums.value = nums.value + "\n";
   } 
   numadded++;
   cats.value = cats.value + catstr;
   nums.value = nums.value + valstr;
}

<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="./checksubmit.js"></script>
  </head>
  <body>
      <h1>Pie Graph Data Entry</h1>
      <form>
          <table border="1">
              <tr>
                  <th colspan="2">data item</th>
              </tr>
              
              <tr>
                  <td>Category</td>
                  <td><input type="text" id="cat"></td>
              </tr>

              <tr>
                  <td>Value</td>
                  <td><input type="text" id="val"></td>
              </tr>
               <tr>
              <td colspan="2"><center><input type="button" value="Add item" onclick="doAdd()"></center></td>
                </tr>
          </table>
            </form>
        <form method="post" action="./PieGraph.php">
      <table border ="1">
             
                
              <tr>
                  <td colspan="2">Data for submission</td>
              </tr>

              <tr>
                  <td>category</td>
                  <td>Value</td>
              </tr>
              
              <tr>
                  <td>
                      <textarea rows="10"  cols="10" id="catlist"></textarea>
                  </td>
                  <td>
                      <textarea rows="10"  cols="10" id="numlist"></textarea>
                  </td>
              </tr>
          
                      <tr>
                  <td>Title for plot</td>
                  <td><input type="text" id="title"></td>
              </tr>

              <tr>
              <td colspan ="2"><input type="submit" value="Plot graph"></td>
              </tr>

              <tr>
              <td colspan ="2"><input type="Reset" value="Reset"></td>
              </tr>

          </table>
      </form>
      </body>
</html>

Sponsor our Newsletter | Privacy Policy | Terms of Service