1

I am into procedural programming and new to OOP. With the following code, I would like to print a message "Submitted successfully" on clicking submit button. But it is not happening. In the procedural programming I do something like isset($_POST[$name]). Here how to refer button name?

<?php

//myclass

class InsertData {
    public $txtwidth;
    public $txtheight;
    public $txtname;
    public $btnwidth;
    public $btnheight;
    public $btnname;

    function setTextField($tw, $th, $tname) {
        $this->txtwidth = $tw;
        $this->txtheight =$th;
        $this->txtname = $tname;        
    }

    function setButton($bw, $bh, $bname) {
        $this->btnwidth = $bw;
        $this->btnheight =$bh;
        $this->btnname = $bname;
    }

function displayText() {
    if(isset($_POST[$this->btnname])) {         
            echo "Submitted successfully";
        }
}
    function getTextField() {
        $str = "<form name='inputform' action='#' method='post'>
        <input type='text' name='".$this->txtname."' Style=width:".$this->txtwidth."px;height:".$this->txtheight."px><br><br>
         <input type='submit' name='".$this->btnwidth."' Style=width:".$this->btnheight."px;height:".$this->btnname."px>
         </form>";  


        return $str;
    }

}
?>

My code

<?php

//my code

include "InsertData.php";
$txt = new InsertData();

$txt->setTextField(800,200,'yourname');
$txt->setButton(100, 200, 'click');
echo $txt->getTextField();
echo $txt->displayText();
?>
1
  • 1
    There's little gain in switching to OOP if you hard-code everything inside your classes: your code will not be reusable or testable. Commented Feb 1, 2016 at 9:59

3 Answers 3

3

check this function you exchange the value of width,name and height

function getTextField() {
        $str = "<form name='inputform' action='#' method='post'>
        <input type='text' name='".$this->txtname."' Style=width:".$this->txtwidth."px;height:".$this->txtheight."px><br><br>
         <input type='submit' name='".$this->btnname."' Style=width:".$this->btnwidth."px;height:".$this->btnheight."px>
         </form>";  


        return $str;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

You gave width value equal to height and height to name and name to width.
2

Just modify function displayText() to

function displayText() {
  return "Submitted successfully";
}

And on submit do a ,

if(isset($_POST['your_btn_name'])) {   
  echo  $txt->displayText();    
}

3 Comments

Simplest answer yet, I like this a lot! +1
I am getting the error 'Fatal error: Using $this when not in object context in E:\wamp\www\OOP\InsertDataDisplay.php on line 9' as $this has been used outside the class.
@rajesh check my edit ...just in $_POST give your button name
0

Try passing it as a parameter to the classes function, for example:

<?php
class Foo
{
    public function showMessage($message = '')
    {
        if ($message != '')
        {
            print "You submitted: {$message}";    
            return;    
        }
        print "Message failed to send...";
        return;
    }
}

$foo = new Foo();
?>
<form action="#" method="post">
    <input type="text" name="message" />
    <br />
    <input type="submit" name="submit" value="Submit my message!" />
</form>
<?php
if (isset($_POST['submit']))
{
    $foo->showMessage($_POST['message']);
}

And without a message, it says: Message failed to send...
And with a message it says: You submitted: message (I used the message of message)

2 Comments

I would like to have form also inside the class. In that case, how to check isset() for submit button?
@Rajesh, the same way, do if ((isset$_POST) && isset($_POST['submit'])){ ... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.