avinashd Posted October 3, 2015 Share Posted October 3, 2015 I am new in php. i want to create a program with input fields as width, height etc. where user input their value and display output as total area, I already finished till here, but i also need a rectangle or div shape as entered input value(width and height). shape should be change according to input values, I tried but could not succeed, can someone please guide me how to do it? here it is my code, although it is very poor coding, still learning. <?php if(!isset($_POST['submit'])){ ?> <form method="post"action="<?php echo $_SERVER['PHP_SELF'];?>"> <h2>Please enter rectangle value</h2> <p>Enter width value <input type="text"name="w"/></p> <p>Enter height value <input type="text"name="h"/></p> <p><input type="submit"name="submit"value="Submit"/> <input type="reset"name="reset"value="Reset"/></p> </form> <?php }else{ $w=$_POST['w']; $h=$_POST['h']; if(empty($_POST['w'])|| empty($_POST['h'])){ echo"please enter value<br/>"; } elseif(!ctype_digit($w)|| (!ctype_digit($h))){ echo"value must be integer<br/>"; } else { $w=$_POST['w']; $h=$_POST['h']; } echo "Total area is ".calc($w,$h); } $w=0; $h=0; function calc($w,$h){ $area=$w*$h; if($w==$h){ echo"It is a square<br/>"; }else{ echo"It is a rectangle<br/>"; } return $area; } ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 3, 2015 Share Posted October 3, 2015 (edited) Use the style attribute to apply the width and height of the div <div style="width:<?php echo $w; ?>px;height:<?php echo $h; ?>px;background-color:red;border:1px solid #000;"></div> Edited October 3, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted October 3, 2015 Share Posted October 3, 2015 (edited) You want to define the default $w and $h variable above, as they are now are rewriting the POST values <?php $w=0; $h=0; if(!isset($_POST['submit'])){ ?> <form method="post"action="<?php echo $_SERVER['PHP_SELF'];?>"> <h2>Please enter rectangle value</h2> <p>Enter width value <input type="text"name="w"/></p> <p>Enter height value <input type="text"name="h"/></p> <p><input type="submit"name="submit"value="Submit"/> <input type="reset"name="reset"value="Reset"/></p> </form> <?php }else{ if(empty($_POST['w'])|| empty($_POST['h'])){ echo"please enter value<br/>"; } elseif(!ctype_digit($w)|| (!ctype_digit($h))){ echo"value must be integer<br/>"; } else { $w=$_POST['w']; $h=$_POST['h']; } echo "Total area is ".calc($w,$h); } function calc($w,$h){ $area=$w*$h; if($w==$h){ echo"It is a square<br/>"; }else{ echo"It is a rectangle<br/>"; } return $area; } ?> <div style="width:<?php echo $w;?>px;height:<?php echo $h;?>px;background-color:red;border:1px solid #000;"></div> Edited October 6, 2015 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.