3

enter image description here

Well my script should looks like that. I have to do it mainly in PHP. Button add should save data to file, show should read that file and put it into textarea, delete have to delete chosen line and reset resets everything.

<?php
    $plik =fopen("data.dat","a+");
    @fputs($plik, $_POST["name"]. " " . $_POST["sname"] . " " . $_POST["adres"] . " " . $_POST["number"] . "<br>" );    
    fclose($plik);  
?>

<html>

   <body>

   <form action = "<?php $_PHP_SELF ?>" method = "POST">

         Name: <input type = "text" name = "name" /><br>
         Second Name: <input type = "text" name = "sname" /><br>
         Adres: <input type = "text" name = "adres" /><br>
         Number: <input type = "text" name = "number" /><br>
         <input type = "submit" name="add" value="Add"/>
         <input type = "button" name="show" value="Show"/>
         <input type = "button" name="reset" value="Reset"/>
         <input type = "button" name="delete" value="Delete"/><br>
         <textarea id="lista" name="lista" rows="20" cols="40" style="overflow:scroll" readonly="" wrap="off"></textarea>

      </form>

   </body>

</html>

My script looks like that and i don't know what should i do next. How to add functions to those buttons and how should they look like?

6
  • You need to switch the behavior on the button, no? if(isset($_POST['add'])) {//do something for add button etc. }elseif(isset($_POST['show'])){ //do something for show button and so on. Commented Feb 27, 2019 at 20:08
  • 1
    You are asking for too many things, I don't know how to explain them! please read more about php forms with PHP php.net/manual/en/tutorial.forms.php Commented Feb 27, 2019 at 20:08
  • What do u mean by switching behavior? U mean it should be button like i changed from submit? Commented Feb 27, 2019 at 20:14
  • If you want buttons to take action without submitting the form and reloading the page, you need to use AJAX. Commented Feb 27, 2019 at 20:26
  • If you want to do it just in PHP, the buttons should be type="submit". Give them all the same name, and then test the value of the parameter in the PHP script. Commented Feb 27, 2019 at 20:27

1 Answer 1

2

I think this should do the job :

<?php
    if(isset($_POST['action'])) {
       switch($_POST['action']) {
         case('Add'): ... break;
         case('Show'): ... break;
         case('Reset'): ... break;
         case('Delete'): ... break;
         default: ...
       }
    }
    ?>

    <html>
       <body>

       <form action = "<?php $_PHP_SELF ?>" method = "POST">
             Name: <input type = "text" name = "name" /><br>
             Second Name: <input type = "text" name = "sname" /><br>
             Adres: <input type = "text" name = "adres" /><br>
             Number: <input type = "text" name = "number" /><br>
             <input type = "submit" name="action" value="Add"/>
             <input type = "submit" name="action" value="Show"/>
             <input type = "submit" name="action" value="Reset"/>
             <input type = "submit" name="action" value="Delete"/><br>
             <textarea id="lista" name="lista" rows="20" cols="40" style="overflow:scroll" readonly="" wrap="off"></textarea>
          </form>

       </body>
    </html>

As you can see I have just changed the type of the action buttons to 'submit' and set the same name for all of them. Then in php just test if the action is set, and then choose the correct action to execute. Hope it helps

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

2 Comments

I've got "Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\www\index.php on line 2" with this code :/
Hey I forgot a ')' at the end of the if statement, should be good now, can you confirm ?

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.