1

How do i implement button click php when either one of the buttons is clicked? I am trying to do is, if sumbit button is clicked the php script below shows it written to a file and if view comments is hit shows comments

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

I have two form buttons IF submit comments is clickedthe php code should write the user’s name and comments to a text file (use fputs function to write to a file. Also you should insert a newline after each name and each comment). When user’s name and comments are successfully recorded, php should direct

On the other hand, if a user clicks on the ‘view all comments’ button, the php code should show all users and their corresponding comments (use fgets function)

 <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);
?>
2
  • PHP executes server side. You're going to have to look into JavaScrpt and Ajax. And even then, I'm not so sure you will be able to write to the user's machine. Commented Jul 1, 2014 at 15:14
  • I am using xampp appache module Commented Jul 1, 2014 at 15:15

4 Answers 4

1

Your submit buttons should like below :

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

And your php code for each buttons should like below:

<?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
    }

?>
Sign up to request clarification or add additional context in comments.

Comments

1

use name attribute on buttons

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

then on php you can check something like

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

or

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

Best regards, Nebojsa

Comments

1

For buttons, I usually use <button> tags and not <input> tags, as long as you're using <!doctype html> for HTML5.

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

Then use PHP to figure out what the user clicked like this:

<?php

    $action = $_POST["action"];

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

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

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

?>

Comments

-1

First give your submit buttons a name like so:

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

and then use this PHP:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.