Al-Amin's Blog

How to use core php form in laravel pattern ?

  Al-Amin Sarker  |   26 Jul, 2018 |   PHP, Laravel |   view 58 times

Laravel is a powerful php software framework and it's super easy to learn. Now I'm going to teach you that how to create a html form using laravel pattern. It's super simple and easy! So, keep reading my article for learn this. Here is a simple core php form using laravel pattern. First I'm going to create a dummy html form in the index.php file.


 
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>My first form</title>
</head>
<body>
	<form method="" action="">
		<p>Username <input type="text" name="u_name"></p>
		<p>Password <input type="password" name="pass"></p>
		<p><input type="submit" name="submit" value="Login"></p>
	</form>
</body>
</html>

Above that form's, I used two input fields like username and password field. And finally one submit button.

Now it's time to create PHP function. So, I'm going to create new php file like my_form.php and create functions...

First I create form_open() function and pass two parameters $action and $method. Action means which url will be loaded after click the login button and the method is which method data will pass from browser to server like POST, GET etc. and return html form opening tag!

After that function, I'm going to create html form closing tag like below...


 
	function form_open($action, $method)
	{
		return '
'; } function form_close() { return '
'; }

Finally I create input tag function and pass three values. 1st is mention for input type operator, 2nd is input name and 3rd is input value.


function form_input($type, $name, $value = null)
{
	return '';
}

Now adding above three methods in one file(my_form.php).


function form_open($action, $method)
	{
		return '
'; } function form_close() { return '
'; } function form_input($type, $name, $value = null) { return ''; }

And include this my_form.php file into index.php file using php require_once method!

Now it's time to replace form opening tag to form_open method with parameters. First value is null, because same page will be loaded after submit the login button. And second value is post, because I want to pass data from browser to server by post method.

Now replace input type tag to form_input method with parameters. 1st one is input type which is text, password, submit etc. 2nd one is field name and 3rd one is field value(It's optional).

And finally replace form close tag to form_close method.

 
<?php require_once('my_form.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>My first form</title>
</head>
<body>
	<?php echo form_open('', 'post'); ?>
		

Username <?php echo form_input('text', 'u_name'); ?>

Password <?php echo form_input('password', 'pass'); ?>

<?php echo form_input('submit', 'submit', 'Login'); ?>

; <?php echo form_close(); ?> </body> </html>

Subscribe to newsletter

About Al-Amin Sarker

Profile Picture

Al-Amin Sarker is a Bangladesh times best Web Engineer. He helps to build web based software. He studied at Computer Science and Engineering in Rajshahi University, Rajshahi, Bangladesh. He has more than 6 years of experience on building robust and scalable web applications (not websites) with cutting edge technologies Continue reading...

Do you love my source code ?

Hire me to Develop

Do you want me to
Develop at
Your Software ?
Click here to Hire

facebook box