Purple_Rain
Purple_Rain

Reputation: 77

Php button action event needed

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

Upvotes: 1

Views: 991

Answers (4)

Reece Kenney
Reece Kenney

Reputation: 2964

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

Upvotes: -1

Andy Castille
Andy Castille

Reputation: 157

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.");
    }

?>

Upvotes: 1

Nebojsa Susic
Nebojsa Susic

Reputation: 1260

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

Upvotes: 1

Suman Biswas
Suman Biswas

Reputation: 883

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
    }

?>

Upvotes: 1

Related Questions