2012-12-10 48 views
1

我在PHP中的提交按鈕上執行的操作不會執行。一切工作正常,沒有錯誤,但是當我點擊按鈕沒有任何反應。記事本++看到文件lines.php已被修改。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> 
+1

PHP是在服務器端 –

+1

執行我使用WAMP –

+1

你不能用JavaScript事件運行PHP運行它在本地主機上,除非你使用AJAX –

回答

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> 
+0

php是在頁面加載運行,所以是你的INSERT將工作,但不是當你點擊按鈕時,你可以嘗試加載頁面並在點擊之前檢查你的數據庫 –

1

問題解決了。我已經使用rewrited的$工程文件中的以下內容:

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