开发者

Any idea how to build a php string manipulation app using forms?

I want to build an string/text manipulation app that:

An app consists of a form User inputs text there as a string using Click a button - which usues $_POST method to manipulate a string using oop method to :

Example method assigned to a button:

public function revers_sentence() {
    $this->string = strrev($this->string);
    return $this;
}

Then the manipulated string is displayed in the same form. User can do another manipulation with a converted string

How to assign method to a button to trigger the function and display results in the same form? Can it be achived in a single 开发者_Go百科php file to send and get a result? (I want to store my classes in a seperate file)

Any help/idea Appreciated - any wise guy?

edit:

<?php
  require_once('mod.php');


  $string='';


  if (isset($_POST['string']))
    $string=$_POST['string'];

  if (isset($_POST['DoStuff']))
  {
    //$string = reverse_1_word($string);
    **$string->reverse();**<-------------------------------Fatal error:---------
  }

  if (isset($_POST['DoOtherStuff']))
  {
    $string = doOtherStuffWithThisString($string);
  }

?>

Fatal error: Call to a member function odwroc() on a non-object on line 14 so how to make every new string an object ?


I would do something like that:

<?php
  Class MyString{
    private $string='';
    function __construct(){
      if (isset($_POST['string']))
         $this->string=$_POST['string'];
    }
    function doStuffWithThisString(){
      $this->string=$this->string.'!';
    }
    function doOtherStuffWithThisString(){
      $this->string=$this->string.'!!!';
    }
    function getString(){
      return $this->string;
    }
  }

  $myString = new MyString();

  if (isset($_POST['DoStuff']))
  {
    $myString->doStuffWithThisString();
  }
  if (isset($_POST['DoOtherStuff']))
  {
     $myString->doOtherStuffWithThisString();
  }

?>

<form action="" method="post">
<!-- blank action attribute will post form to the current page-->
  <input type="text" value="<?=$string->getString()?>" name="string" />
<!-- <?=$string->getString()?>  is the same as <?php echo $string->getString(); ?> -->
  <input type="submit" value="Do Stuff" name="DoStuff" />
  <input type="submit" value="Do Other Stuff" name="DoOtherStuff" />
</form>


You could have a form like this :

<form action="currentFile.php" method="post">
  <input type="text" value="myValue" name="text" />
  <input type="submit" value="Reverse !" name="reverse" />
  <input type="submit" value="Other manipulation" name="otherManip" />
</form>

When you receive the data, you can do this :

if ( isset($_POST['reverse']) ) {
  // User clicked on the reverse button
} elseif ( isset($_POST['otherManip']) ) {
  // User clicked on the other button
}


to achive the send and get in a single PHP file you have to set the forms action to the actual site you are on. I do this ba writing <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> FORMFIELDS AND DATA </form>

To work with the returned values you just have to build an if-statement, that checks for the passed values if (isset($_POST['buttonname'])){ //DO SOMETHING}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜