2012-09-26 39 views
1

我有一個叫做admin.php的頁面,它有一些cods和按鈕。我的問題是按鈕的一個應用操作代碼,原本是HTML代碼,但我把它轉化爲PHP和代碼是這樣的:將操作按鈕命令合併到主頁

echo "<form action=\"write.php\" method=\"post\">\n"; 
echo " <p>your MoD name: <input type=\"text\" name=\"mdname\" /></p>\n"; 
echo " <p><input type=\"submit\" /></p>\n"; 
echo "</form>\n"; 

此代碼調用write.php和write.php代碼是這樣的:

<?php 
header ('Location: admin.php '); 
$myFile = "methods\actmod.txt"; 
unlink($myFile); 
$handle = fopen("methods\actmod.txt", "a"); 
foreach($_POST as $variable => $value) { 
    fwrite($handle, $variable); 
    fwrite($handle, "="); 
    fwrite($handle, $value); 
    fwrite($handle, "\r\n"); 
} 
fwrite($handle, "\r\n"); 
fclose($handle); 
exit; 
?> 

該代碼將一些數據寫入txt文件,該文件調用actmod.txt並返回到管理頁面。我想問是否有任何方法可以將write.php合併到主admin.php頁面,所以當我點擊管理頁面中的提交按鈕時,它贏得了使用任何外部文件並直接從管理頁面運行命令,我不會管理頁面後面有任何外部文件?

我已經看到大量的網頁有這樣的「http://site.com/admin.php?act=write

我希望每一個人都明白我的意思。我是新來的PHP和地址我搜索了很多,我體味到任何人的幫助。 謝謝

+0

什麼是 「瑪吉」 是什麼意思?它是「合併」嗎? – Barmar

+0

對不起,我的英語:D –

回答

-1

在你admin.php的

<?php 
$post = count($__POST) > 0 ? true : false; 
if ($post) { 
    // do the work you are doing in write.php 
} else { 
    // do the work you are already doing in admin.php 
} 

可能有更好的方法來識別請求是POST或GET,我只是想給你提示和休息是你的。:)

0

使用這種在admin.php的

<?php 

if(array_key_exists('action', $_GET)&&$_GET['action']=='write') { 
    $myFile = "methods\actmod.txt"; 
    // unlink($myFile); deletes your file, if you want to ADD text, don't :) 
    $handle = fopen($myFile, "a"); 
    foreach($_POST as $variable => $value) { 
     fwrite($handle, $variable); 
     fwrite($handle, "="); 
     fwrite($handle, $value); 
     fwrite($handle, "\r\n"); 
    } 
    fwrite($handle, "\r\n"); 
    fclose($handle); 
} 

echo "<form action=\"admin.php?action=write\" method=\"post\">\n"; 
echo " <p>your MoD name: <input type=\"text\" name=\"mdname\" /></p>\n"; 
echo " <p><input type=\"submit\" /></p>\n"; 
echo "</form>\n"; 
?> 
+0

非常感謝。它工作完美。 –

+0

所以請點擊勾號接受答案:D – sics

+0

@MrBG我的答案與此答案有何不同? – vikas