admin管理员组

文章数量:1022712

I have input in my form and I want to save the name, type, and value of that input in my database when the user saves the form.

I'm getting the name and value of the input but can't get the type.

I've tried it with JavaScript and it works. But I want to save it using Laravel in the controller. Is there any way that I can get the type?

using JavaScript: document.getElementsByName("name")[0].type returns "text".

Thanks in advance.

I have input in my form and I want to save the name, type, and value of that input in my database when the user saves the form.

I'm getting the name and value of the input but can't get the type.

I've tried it with JavaScript and it works. But I want to save it using Laravel in the controller. Is there any way that I can get the type?

using JavaScript: document.getElementsByName("name")[0].type returns "text".

Thanks in advance.

Share Improve this question asked Feb 17, 2022 at 13:07 kmbokmbo 111 silver badge3 bronze badges 4
  • Without the code it's hard to say ... Maybe you could add the type to the name of the input, and then extract the type at the server. Or include a hidden input which has the type as its value, and name the pairs like name="name[]" ..? – Teemu Commented Feb 17, 2022 at 13:09
  • Please provide your code(Controller as well as blade file). – VIKAS KATARIYA Commented Feb 17, 2022 at 13:11
  • Controller: function createForm(Request $request) { foreach ($request->all() as $key => $value) { if ($key != '_token') { $file = File::create([ 'name' => $key, 'type' => 'want to get this from input', 'value' => $value ]); } } return redirect()->back()->with('success', 'Form has been created successfully'); } – kmbo Commented Feb 17, 2022 at 13:51
  • Blade code <form action="{{ route('user.createForm') }}" method="post"> @csrf <div class="form-group"> {!! $input->description !!} </div> <button type="submit" class="btn btn-success">Save</button> </form> – kmbo Commented Feb 17, 2022 at 14:06
Add a ment  | 

2 Answers 2

Reset to default 3

Using the built in php function :

gettype — Get the type of a variable

See this below pseudo code:

$input = "Your Name";

echo gettype($input);

The only thing which you can related to each input field is it name and it value. To have type of each field you can use hidden field which will have name like type_of_name for a field which has name name and type_of_password for a field which has name password and so on. as all that field will be pass to the request you can access them inside of you controller and get type of each form field by getting input element which as name equal to the field name prefixed with the type_of

Here is a sample code

<form action="" method="post">
   
    <input type="text" name="name">
    <input type="hidden" name="type_of_name" value="text">

    <input type="password" name="password">
    <input type="hidden" name="type_of_password" value="password">
    {{-- etc. --}}
</form>

I have input in my form and I want to save the name, type, and value of that input in my database when the user saves the form.

I'm getting the name and value of the input but can't get the type.

I've tried it with JavaScript and it works. But I want to save it using Laravel in the controller. Is there any way that I can get the type?

using JavaScript: document.getElementsByName("name")[0].type returns "text".

Thanks in advance.

I have input in my form and I want to save the name, type, and value of that input in my database when the user saves the form.

I'm getting the name and value of the input but can't get the type.

I've tried it with JavaScript and it works. But I want to save it using Laravel in the controller. Is there any way that I can get the type?

using JavaScript: document.getElementsByName("name")[0].type returns "text".

Thanks in advance.

Share Improve this question asked Feb 17, 2022 at 13:07 kmbokmbo 111 silver badge3 bronze badges 4
  • Without the code it's hard to say ... Maybe you could add the type to the name of the input, and then extract the type at the server. Or include a hidden input which has the type as its value, and name the pairs like name="name[]" ..? – Teemu Commented Feb 17, 2022 at 13:09
  • Please provide your code(Controller as well as blade file). – VIKAS KATARIYA Commented Feb 17, 2022 at 13:11
  • Controller: function createForm(Request $request) { foreach ($request->all() as $key => $value) { if ($key != '_token') { $file = File::create([ 'name' => $key, 'type' => 'want to get this from input', 'value' => $value ]); } } return redirect()->back()->with('success', 'Form has been created successfully'); } – kmbo Commented Feb 17, 2022 at 13:51
  • Blade code <form action="{{ route('user.createForm') }}" method="post"> @csrf <div class="form-group"> {!! $input->description !!} </div> <button type="submit" class="btn btn-success">Save</button> </form> – kmbo Commented Feb 17, 2022 at 14:06
Add a ment  | 

2 Answers 2

Reset to default 3

Using the built in php function :

gettype — Get the type of a variable

See this below pseudo code:

$input = "Your Name";

echo gettype($input);

The only thing which you can related to each input field is it name and it value. To have type of each field you can use hidden field which will have name like type_of_name for a field which has name name and type_of_password for a field which has name password and so on. as all that field will be pass to the request you can access them inside of you controller and get type of each form field by getting input element which as name equal to the field name prefixed with the type_of

Here is a sample code

<form action="" method="post">
   
    <input type="text" name="name">
    <input type="hidden" name="type_of_name" value="text">

    <input type="password" name="password">
    <input type="hidden" name="type_of_password" value="password">
    {{-- etc. --}}
</form>

本文标签: javascripthow to get the type of input in laravelStack Overflow