PHP按钮操作不起作用

时间:2012-12-10 02:42:18

标签: php

我在PHP中的提交按钮上实现的操作未执行。一切正常,没有错误,但当我点击按钮没有任何反应。 Notepad ++发现文件lines.php已被修改。

<html>
<head>
    <title>
        Watch your Language
    </title>
</head>
<body>
    <H2 align="center">
        <form method="post">
            <input type="submit" onclick="English()" name="introdu" value="Choose english">
        </form>
        <a href="index.php">Home</a> | <a href="bot.php">Talk to robot</a>
    </h2>
</body>
</html>

 <script type="text/javascript">
function English()
{<?php
         $myFile = "lines.php";
         $fh = fopen($myFile, 'w');

$eng = <<<TEST
<?php
class TextToSpeech {
    public $mp3data;
    function __construct($text="") {
        $text = trim($text);
        if(!empty($text)) {
            $text = urlencode($text);
            $lang_en="http://translate.google.com/translate_tts?tl=en&q=\{$text}";
            $lang_ro="http://translate.google.com/translate_tts?tl=ro&q=\{$text}";
            $lang_fr="http://translate.google.com/translate_tts?tl=fr&q=\{$text}";
            $language=$lang_en;
            $this->mp3data = file_get_contents($language);
        }
    }

    function setText($text) {
        $text = trim($text);
        if(!empty($text)) {
            $text = urlencode($text);
            $en="http://translate.google.com/translate_tts?tl=en&q=\{$text}";
            $ro="http://translate.google.com/translate_tts?tl=ro&q=\{$text}";
            $fr="http://translate.google.com/translate_tts?tl=fr&q=\{$text}";
            $lang=$en;
            $this->mp3data = file_get_contents($lang);
            return  $this->mp3data;
        } else { return false; }
    }

    function saveToFile($filename) {
        $filename = trim($filename);
        if(!empty($filename)) {
            return file_put_contents($filename,$this->mp3data);
        } else { return false; }
    }
}
?>
TEST;
echo $eng;
fwrite($fh, $eng);
fclose($fh); 
?>
alert("File created");
}
</script>

3 个答案:

答案 0 :(得分:1)

此代码使用相同的方法,并且完美地运行

<html>
<head><title>Umanizator</title>

</head>
<body><H2 align="center">
<form method="post">
Intrebare
 <input type="text" name="intrebare">
Raspuns
 <input type="text" name="raspuns">
<input type="submit" onclick="Confirmare()" name="introdu" value="Exporta">
</form>
<a href="index.php">Home</a> | <a href="bot.php">Discuta cu robotul</a></h2>
</body>
</html>

<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("robo") or die(mysql_error());

error_reporting(E_ALL & ~E_NOTICE);
$intrebare=$_POST['intrebare'];
$raspuns=$_POST['raspuns'];
$cod1='mysql_query("INSERT INTO dialog  (intrebare, raspuns) VALUES (\'';
$cod2="'  ,  '";
$cod3="') \")  or die(mysql_error());";


$myFile = "lines.txt";
$intrebari = "intrebari.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$op = fopen($intrebari, 'a') or die("can't open file");

$delimiteaza="\n /*---*/  \n";

fwrite($fh, $delimiteaza);
fwrite($fh, $cod1);
fwrite($fh, $intrebare);
fwrite($fh, $cod2);
fwrite($fh, $raspuns);
fwrite($fh, $cod3);
fwrite($op, $intrebare);
fwrite($op, $delimiteaza);

fclose($fh);


?>

 <script type="text/javascript">
function Confirmare()
{
<?php
mysql_query("INSERT INTO dialog  (intrebare, raspuns) VALUES ('".$intrebare."'  ,  '".$raspuns."') ")  or die(mysql_error());
?>
alert("Linia de cod a fost exportata!");
}
</script>

答案 1 :(得分:1)

这基本上只能通过Ajax调用获得。这是我在Stack Overflow上发现的一个很好的例子:

https://stackoverflow.com/a/7165616/1439793

答案 2 :(得分:1)

问题解决了。我使用以下内容重写了$ eng文件:

<<<TEST
<?php
class TextToSpeech {
    public \$mp3data;
    function __construct(\$text="") {
        \$text = trim(\$text);
        if(!empty(\$text)) {
           \$text = urlencode(\$text);
            \$lang_en="http://translate.google.com/translate_tts?tl=en&q={\$text}";
            \$lang_ro="http://translate.google.com/translate_tts?tl=ro&q={\$text}";
            \$lang_fr="http://translate.google.com/translate_tts?tl=fr&q={\$text}";
            \$language=\$lang_en;
            \$this->mp3data = file_get_contents(\$language);
        }
    }

    function setText(\$text) {
        \$text = trim(\$text);
        if(!empty(\$text)) {
            \$text = urlencode(\$text);
            \$en="http://translate.google.com/translate_tts?tl=en&q={\$text}";
            \$ro="http://translate.google.com/translate_tts?tl=ro&q={\$text}";
            \$fr="http://translate.google.com/translate_tts?tl=fr&q={\$text}";
            \$lang=\$en;
            \$this->mp3data = file_get_contents(\$lang);
            return  \$this->mp3data;
        } else { return false; }
    }

    function saveToFile(\$filename) {
        \$filename = trim(\$filename);
        if(!empty(\$filename)) {
            return file_put_contents(\$filename,\$this->mp3data);
        } else { return false; }
    }
}
?>
TEST;