Help required with form submission

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GLF
Forum Newbie
Posts: 6
Joined: Wed Mar 02, 2011 6:02 pm
Location: Jersey Channel Islands

Help required with form submission

Post by GLF »

Can any body help, I am new to PHP and am learing php via various books. I would like to make a user defined png image to create a graph. The problem is that the code I am using does not work. I have simplified the code just to create a black box. If I can get this to work then I can produce my graph. This is the code, can some one let me know where I have gone wrong?


if (!$_POST) {
echo "
<html>
<head>
<title>Iamge Creation Block</title>
</head>
<body>

<h1>Create an Image</h1>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="POST">
<P><strong>Image Size:</strong><br/>
W: <input type="text" name="w" size="5" maxlength="3" />
H: <input type="text" name="H" size="5" maxlength="3" /><p>

<P><input type="submit" name="submit" value="Create Image" /></P>


</form>
</body>
</html>";

} else {


//create the canvas
$myimage = imagecreate($_POST["W"], $_POST["H"]);

//set up some colors
$black = ImageColorAllocate ($myimage,0,0,0);




//output the image to the browser
header ("Content-type: image/png");
ImagePng($myimage);

//clean up after your self
ImageDestroy($myimage);

}

?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Help required with form submission

Post by social_experiment »

GLF wrote:The problem is that the code I am using does not work.
This is a pretty generic explanation :) Anything specific that does (or does not) happen?

From a syntax point of view this is going to give you errors. If you use " (double quotes) to encase a statement any other double quotes have to be escaped ( \"). You can leave $_SERVER['PHP_SELF'] and go with action="", it also calls the page on itself.

Code: Select all

if (!$_POST) {
echo "
<html>
<head>
<title>Iamge Creation Block</title>
</head>
<body>

<h1>Create an Image</h1>
<form action=\"\" method="POST">
<P><strong>Image Size:</strong><br/>
W: <input type=\"text\" name=\"W\" size=\"5\" maxlength=\"3\" />
H: <input type=\"text\" name=\"H\" size=\"5\" maxlength=\"3\" /><p>

<P><input type=\"submit\" name=\"submit\" value=\"Create Image\" /></P>
</form>
</body>
</html>";
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
GLF
Forum Newbie
Posts: 6
Joined: Wed Mar 02, 2011 6:02 pm
Location: Jersey Channel Islands

Re: Help required with form submission

Post by GLF »

Uh, thank you for the escape of double quotes, I did not know that!
Sorry the comment that it does not work is a little vague to say the least.
What I am trying to do is set out a form that will set size peramitors eg 600 wide x 600 Heigh. The code will then in this case draw a black box 600x600.
The code will be adapted to provide the peramitors for a graph, but if I can't even make one single simple block image it is not going to work!!
The problems are
1 - When displayed on the browser the if (!$_POST) { echo code is displayed and all the code after the html closing tag. Only the form should display then the black image when run.
2 - The form does display correctally but when the values are entered and called nothing happens.

I have tried placing php opening tags in various places but this just results in nothing being displayed :banghead:

In the mean time I will ammend the code to inclue the escape quotes and the ACTION\POST line.

Thank you for your help
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Help required with form submission

Post by social_experiment »

GLF wrote:1 - When displayed on the browser the if (!$_POST) { echo code is displayed and all the code after the html closing tag. Only the form should display then the black image when run.

Code: Select all

<?php
 if (isset($_POST['submit'])) {
  // process the script
 }
 else {
  // display form
 }
?>
The code above is an example of what you have in mind. The isset($_POST['submit']) checks if the submit button has been clicked and if that is the case, your code is executed, if not, the form is displayed.
GLF wrote:2 - The form does display correctally but when the values are entered and called nothing happens.
The first bit of advice should solve the problem (i think). Also, if you have given your input fields a name like 'W' it's best to use the same value when attempting to retrieve the value,

Code: Select all

<input type="text" name="W" />
<?php
// the value will be in $_POST['W'], not $_POST['w'] or $_POST[w]
echo $_POST['W'];
?>
I'm not 100% certain about this but it's best to use the values you created imho.
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
GLF
Forum Newbie
Posts: 6
Joined: Wed Mar 02, 2011 6:02 pm
Location: Jersey Channel Islands

Re: Help required with form submission

Post by GLF »

Hi

I have made the suggested ammendments but still to no avail...... Any other ideas would be appriciated... :?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Help required with form submission

Post by social_experiment »

Code: Select all

<?php
if ($_POST['submit']) {
//create the canvas
$myimage = imagecreate($_POST["w"], $_POST["h"]);

//set up some colors
$black = ImageColorAllocate ($myimage,0,0,0);

//output the image to the browser
header ("Content-type: image/png");
ImagePng($myimage);

//clean up after your self
ImageDestroy($myimage);
}
?>
<html>
<head>
<title>Image create</title>
</head>
<body>
<h1>Create an Image</h1>
<form action="" method="POST">
<P><strong>Image Size:</strong><br/>
W: <input type="text" name="w" size="5" maxlength="3" />
H: <input type="text" name="h" size="5" maxlength="3" /><p>

<P><input type="submit" name="submit" value="Create Image" /></P>
</form>
</body>
</html>
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
GLF
Forum Newbie
Posts: 6
Joined: Wed Mar 02, 2011 6:02 pm
Location: Jersey Channel Islands

Re: Help required with form submission

Post by GLF »

Hey, Thanks this works. I think that I have been making my life more difficult or more complex than it need be.

Cheers :D :D

GLF
GLF
Forum Newbie
Posts: 6
Joined: Wed Mar 02, 2011 6:02 pm
Location: Jersey Channel Islands

Re: Help required with form submission

Post by GLF »

After a few hours work and adaptation, I have now linked the form with my graph and it works perfectly ~ Thanks

GLF :D :D
GLF
Forum Newbie
Posts: 6
Joined: Wed Mar 02, 2011 6:02 pm
Location: Jersey Channel Islands

Re: Help required with form submission

Post by GLF »

This is my code for now untill I adapt it to obtain the input data from a data base.

Cheers :D :D



<?php

if ($_POST['submit']) {

$img=imagecreate(600,600);

$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,220,220,220);


$img_width=600;
$img_height=600;

$margins=20;
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2;


$values=array(
"0 - 250,000" => $_POST["Cal_1"] ,
"250,001 ~ 500,000" => $_POST["Cal_2"],
"500,001 ~ 750,000" => $_POST["Cal_3"],
"750,001 ~ 1,000,000" => $_POST["Cal_4"] ,
"1,000,001 ~ Above" => $_POST["Cal_5"],

);


$bar_width=70;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);


imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);



$max_value=max($values);
$ratio= $graph_height/$max_value;



$horizontal_lines=20;
$horizontal_gap=$graph_height/$horizontal_lines;

for($i=1;$i<=$horizontal_lines;$i++){
$y=$img_height - $margins - $horizontal_gap * $i ;
imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
$v=intval($horizontal_gap * $i /$ratio);
imagestring($img,0,5,$y-5,$v,$bar_color);

}



for($i=0;$i< $total_bars; $i++){

list($key,$value)=each($values);
$x1= $margins + $gap + $i * ($gap+$bar_width);
$x2= $x1 + $bar_width;
$y1=$margins + $graph_height- intval($value * $ratio);
$y2=$img_height-$margins;
imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);
imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}
header ("Content-type:image/png");
ImagePng($img);

ImageDestroy($img);
}

?>

<html>
<head>
<title>Create Graph</title>
</head>
<body>
<h1>Number of Transactions</h1>
<form action="" method="POST">

Transactions .................0 ~ 250,000:
<input type="text" size="8" name="Cal_1" maxlength="5" />
<p>

Transactions...... 250,001 ~ 500,000:
<input type="text" size="8" name="Cal_2" maxlength="5" />
<p>

Transactions...... 500,001 ~ 750,000:
<input type="text" size="8" name="Cal_3" maxlength="5" />
<p>

Transactions.. 750,001 ~ 1,000,000 : <input type="text" size="8" name="Cal_4" maxlength="5" />
<p>Transactions...... 1,000,001 ~ Above:
<input type="text" size="8" name="Cal_5" maxlength="5" /><p>

<p><input type="submit" name="submit" value="Create Graph" /></p>
</form>

</body>
</html>
Post Reply