我想构建一个字符串/文本操作应用程序,该应用程序:
应用程序由表单用户使用单击按钮将文本作为字符串输入,这使用$_POST方法使用oop方法操作字符串:
分配给按钮的示例方法:
public function revers_sentence() {
$this->string = strrev($this->string);
return $this;
}然后,被操纵的字符串以相同的形式显示。用户可以使用转换后的字符串进行另一次操作。
如何将方法分配给按钮以触发函数并以相同的形式显示结果?它能在一个php文件中实现发送并得到结果吗?(我想将类存储在单独的文件中)
有什么帮助/想法值得赞赏吗-有智者吗?
编辑:
<?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(),那么如何使每个新字符串成为对象?
发布于 2010-12-23 12:56:59
我会这样做:
<?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>发布于 2010-12-23 12:40:45
你可以有这样的表格:
<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>当您收到数据时,可以这样做:
if ( isset($_POST['reverse']) ) {
// User clicked on the reverse button
} elseif ( isset($_POST['otherManip']) ) {
// User clicked on the other button
}发布于 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}。
https://stackoverflow.com/questions/4518719
复制相似问题