1

I am currently able to write data to mysql when one of my pages loads. Here is the code for this in my controller.

public function manuelSignUP()
{
DB :: table("users") -> insertGetId
(
array("firstname" => "John", "lastname"=> "John",
"passwordHash" => "password", "userslevelID" => 2)
);

DB :: table("userlevel") -> insertGetID
  (

array("userlevelID" => $userlevelID, "name" => $name)
 );

 return view("pages.manualsignup");
 }

So I would like to call this function through my blade file on a button click, but I have been struggling to do so. Here is my blade file with the button.

<!DOCTYPE html>
 <html>
<head> </head>

<body> This should  </body>
<br><br><br><br>
<form method="post">
<button type="button"> submit </button>
</form>
</html>

Based on google searching I know that I can use ajax to help me with this problem, but I believe there is way to do it by just using html post methods. I am trying to do it the second way, without ajax.

1

2 Answers 2

1

If you're not using ajax you need to specify where your form should go with the action attribute, it's not sufficient to do it with the method alone.

view

<form action="{{ route('signup') }}" method="post">
    <button type="submit"> submit </button>
</form>

routes.php

Route::post('/signup', [
    'as'   => 'signup',
    'uses' => 'YourController@manuelSignUP',
]);

Also, normally you should use a form to input data into, not hard code it in.

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

3 Comments

I tried your suggestion as well this is what I have in the blade.php file. <form action="{{ route("signup") }}" method="post"> <button type="button"> submit </button> I also added this into the route file Route:: post("signup", [ "as" => "signup", "uses" => "tableController@signup"]); and created a function called "signup" in my tableController. Unfortunately, nothing happens when I click on the button, no data is added to my mysql table.
<button type="submit"> submit </button>
if it is type button it won't submit the form
1

I'd recommend you to use RESTful controllers and test it without using ajax first. Start from learning RESTful controllers and Routes:

https://laravel.com/docs/5.1/controllers#restful-resource-controllers https://laravel.com/docs/5.1/routing

All you need is to create RESTful controller and use store method to store your your data in DB. For example:

{!! Form::model($data, array('action' => 'MyController@store')  !!}
{!! Form::text('name', null, array('required', 'class'=>'form-control', 'placeholder'=>'Name')) !!}
{!! Form::submit('Create and store in DB', array('class'=>'btn btn-success')) !!}

When you'll test everything, just use AJAX if you don't want to reload page every time you store the data in DB.

9 Comments

How do you know he's using laravel collective Forms & HTML
It's just the easiest example for a newbie, he should start from somewhere to understand how it works. Yes, Laravel Collective is a different package now, but it was part of Laravel for a long time and most guys will use it anyway. So why not?
Sure, I'm thinking his question isn't tagged as 4.2, so you can assume he's not using it if he's using the current version of Laravel. If you're going to answer with code that relies on an additional package I don't think it would hurt to make mention of it or reference how to install it.
Im using Forms &HTML . It took me a while but I got that part done
@ray, great, in this case it'll be easier for you to understand how storing in DB works.
|

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.