簡體   English   中英

PHP:在類內部使用isset()函數

[英]PHP: Using isset() function inside the class

我是過程編程的專家,也是OOP的新手。 使用以下代碼,我想在單擊“提交”按鈕時打印一條消息“已成功提交”。 但這沒有發生。 在過程編程中,我執行isset($ _ POST [$ 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;
    }

}
?>

我的密碼

<?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();
?>

檢查此功能,您交換寬度,名稱和高度的值

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;
    }

只需將函數displayText()修改為

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

在提交時,

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

嘗試將其作為參數傳遞給類函數,例如:

<?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']);
}

並且沒有消息,它說: 消息發送失敗...
並顯示一條消息: 您提交了:message (我使用message的消息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM