首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >知道如何使用表单构建php字符串操作应用程序吗?

问知道如何使用表单构建php字符串操作应用程序吗?
EN

Stack Overflow用户
提问于 2010-12-23 12:22:39
回答 3查看 284关注 0票数 0

我想构建一个字符串/文本操作应用程序,该应用程序:

应用程序由表单用户使用单击按钮将文本作为字符串输入,这使用$_POST方法使用oop方法操作字符串:

分配给按钮的示例方法:

代码语言:javascript
复制
public function revers_sentence() {
    $this->string = strrev($this->string);
    return $this;
}

然后,被操纵的字符串以相同的形式显示。用户可以使用转换后的字符串进行另一次操作。

如何将方法分配给按钮以触发函数并以相同的形式显示结果?它能在一个php文件中实现发送并得到结果吗?(我想将类存储在单独的文件中)

有什么帮助/想法值得赞赏吗-有智者吗?

编辑:

代码语言:javascript
复制
<?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);
  }

?>

致命错误:调用第14行中非对象上的成员函数odwroc(),那么如何使每个新字符串成为对象?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-12-23 12:56:59

我会这样做:

代码语言:javascript
复制
<?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>
票数 1
EN

Stack Overflow用户

发布于 2010-12-23 12:40:45

你可以有这样的表格:

代码语言:javascript
复制
<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>

当您收到数据时,可以这样做:

代码语言:javascript
复制
if ( isset($_POST['reverse']) ) {
  // User clicked on the reverse button
} elseif ( isset($_POST['otherManip']) ) {
  // User clicked on the other button
}
票数 0
EN

Stack Overflow用户

发布于 2010-12-23 12:43:37

要在一个PHP文件中实现发送和获取,您必须将forms操作设置为您所在的实际站点。我做了这个ba写<form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> FORMFIELDS AND DATA </form>

若要处理返回的值,只需构建一个if-语句,该语句检查传递的值if (isset($_POST['buttonname'])){ //DO SOMETHING}。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4518719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档