Home > Blockchain >  how to get the type of input in laravel
how to get the type of input in laravel

Time:02-18

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.

CodePudding user response:

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>

CodePudding user response:

Using the built in php function :

gettype — Get the type of a variable

See this below pseudo code:

$input = "Your Name";

echo gettype($input);
  • Related