2014-07-01 953 views
1

如果點擊其中一個按鈕,我該如何實現按鈕點擊php?我試圖做的是,如果點擊了透過按鈕下面的PHP腳本顯示它寫入一個文件,如果查看評論正在熱播節目評論需要Php按鈕動作事件

<html> 
<body> 

    <form action="http://localhost/class/assignment10.php" method="post"> 
    User Name: <input type="text" name="uname"><br> 
    User Comments: <textarea type ="text" name="tcomment"></textarea><br> 
    <input type="submit" value="Submit Comments"/> 
    <input type="submit" value="View Comments"/> 


    </form> 

    </body> 
    </html> 

我有兩個表單按鈕IF提交意見是clickedthe PHP代碼應將用戶名和註釋寫入文本文件(使用fputs函數寫入文件,並且在每個名稱和每個註釋之後插入換行符)。如果用戶點擊'查看所有評論'按鈕,php代碼應該顯示所有用戶及其相應的評論(使用fgets功能)

<html> 
<head> 
<title>PHPLesson10</title> 
</head> 

<body> 

<?php 
    // Collect user name and add a line break at the end. 
    $customer = $_POST['uname']."\n"; 
    // Collect comment and add a line break. 
    $comments = $_POST['tcomment']."\n"; 

    $file = fopen("C:/data/feedback.txt", "a"); 
    If ($file) // if the file open successfully 
    { 
    // Write the variables to the file. 
    fputs($file, $customer); 
    fputs($file, $comments); 
    fclose($file); 
    } 
    Else 
    { 
    Echo "Please try again."; 
    } 
    ?> 

    </body> 
    </html> 


    <?php 
    // Open the file feedback for reading 
$file = fopen("C:/data/feedback.txt", "r"); 
// While the end of the file has NOT reached 
While (!feof($file)) 
{ 
    // Print one line of file 
Echo fgets($file); 
Echo "<br />"; 
} 
// close the connection 
fclose($file); 
?> 
+0

PHP執行服務器端。你將不得不看看JavaScrpt和Ajax。即使如此,我也不確定你能寫信給用戶的機器。 – durbnpoisn

+0

我正在使用xampp appache模塊 –

回答

1

你的提交按鈕應該如下所示:

<input type="submit" name="submit_comment" value="Submit Comments"/> 
<input type="submit" name="view_comment" value="View Comments"/> 

併爲每個按鈕你的PHP代碼下面想:上的按鈕

<?php 
    if(isset($_POST['submit_comment'])) 
    { 
     //the php code will be here for save comment 
    } 
    if(isset($_POST['view_comment'])) 
    { 
     //the php code will be here for view comment 
    } 

?> 
-1

首先給你的提交按鈕的名稱,像這樣:

<input type="submit" name="button1" value="Submit Comments"/> 
<input type="submit" name="button2" value="View Comments"/> 

,然後用這個PHP:

<?php 
    if (isset(@$_POST['button1'])) { 
     //Code to execute 
    } 
    if (isset(@$_POST['button2'])) { 
     //Code to execute 
    } 
?> 
1

使用name屬性

<input type="submit" value="Submit Comments" name="btn_submit"/> 
<input type="submit" value="View Comments" name="btn_view"/> 

然後PHP你可以檢查像

if (isset($_POST['btn_submit']))... 

if (isset($_POST['btn_view']))... 

最好的問候, 內博伊沙

1

對於按鈕,我爲你使用<!doctype html>對HTML5通常使用<button>標籤,而不是<input>標籤,只要。

<form action="http://localhost/class/assignment10.php" method="post" id="commentForm"> 
    <button type="submit" name="action" value="submit" form="commentForm"> 
     Submit Comments 
    </button> 
    <button type="submit" name="action" value="view" form="commentForm"> 
     View Comments 
    </button> 
</form> 

然後使用PHP弄清楚用戶點擊這樣的:

<?php 

    $action = $_POST["action"]; 

    if ($action == "submit") { 
     // submission code 
    } 

    if ($action == "view") { 
     // viewing code 
    } 

    else { 
     die("Your request could not be completed."); 
    } 

?>