Jump to content

How to print div or rectangle shape by input value


avinashd

Recommended Posts

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;
    }
    ?>
Link to comment
Share on other sites

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 by QuickOldCar
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.