Home >>PHP Tutorial >Form action
Action is used to give reference/link of another page. If we want to separate the business logic (PHP script) from Presentation layer (HTML script) then use action Property of Form . It reduce the complexity of bulk of codes. Because All scripts are separately define on their own page. In the previous Form Post method PHP script and HTML script were define on the same page ,so it show the design part with the output of program. But using action property HTML script define on a separate page and Business logic(PHP script) on another separate page.
Save it as DesingView.php
<body> <form method="post" action="Logic.php"> <table border="1" align="center"> <tr> <td>Enter your name</td> <td><input type="text" name="n"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="sub" value="SHOW MY NAME"/> </td> </tr> </table> </form> </body>
Save it as Logic.php
<?php $name=$_POST['n']; echo "Welcome ".$name; ?>
First we make Form using HTML script. We design a textbox to take input through user and a submit button with value("show my name") . When name is entered by user and click on submit button the value of textbox redirect to php script page. Because Action Attribute is used here for link . Information that is send by user is collect by using $_POST[] and store in a local variable($name). Now a local variable is concatenate with String(“welcome”) and print, output will become Welcome Sanjeev.