How do i create an array from form input - php

I'm trying to write a php script that takes the input from this form and creates an array with a unique key for each input box/text boxes content.
<div class="container">
<div class="row">
<div class="col-3 mx-auto">
<form action="adminhandler.php">
<div class="form-group">
<label for="product name">Enter product name</label>
<input type="text" class="form-control" id="productName" aria-describedby="emailHelp" placeholder="" name="productname">
</div>
<div class="form-group">
<label for="product name">Select product category</label>
<select name="select" class=" custom-select">
<option selected>Open this select menu</option>
<option value="1">Formal Shoes</option>
<option value="2">Sneakers</option>
<option value="3">Tracksuits</option>
<option value="3">Clothing</option>
<option value="3">Accessories</option>
</select>
</div>
<div class="form-group">
<label for="product price">Enter product price(Naira)</label>
<input type="text" class="form-control" id="productPrice" aria-describedby="emailHelp" placeholder="" name="price">
</div>
<div class="form-group">
<label for="product name">Select image</label>
<input type="file" class="form-control-file" id="productimage" aria-describedby="emailHelp" placeholder="">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>

I think you want to use a form as an array and then pass the same data as an array.If you are using the $_POST, you will need to use the name attribute of HTML. And use something like this :
<input name="person[1][first_name]" value="jane" />
This when passing through PHP will be sent as Array.
Here is the reference :
http://php.net/manual/en/reserved.variables.post.php
An Excerpt from one of the contributors :
<form>
<input name="person[0][first_name]" value="john" />
<input name="person[0][last_name]" value="smith" />
<input name="person[1][first_name]" value="jane" />
<input name="person[1][last_name]" value="jones" />
</form>
When Trying To Process Values using PHP
<?php
var_dump($_POST['person']);
//will get you something like:
array (
0 => array('first_name'=>'john','last_name'=>'smith'),
1 => array('first_name'=>'jane','last_name'=>'jones'),
)
?>

Related

How do I generate an id for form data input to save the data in an array?

Good day all I have a simple laravel project that takes some information about actors and saves it in a json file after the user fills in the form.My problem is that I save simple data like name, surname, nationality ,age I need a Id something like "edf33e5751b2ea2fd25ad118edbc647e" to save the data in a json file by using arrays.How do I take the user input data from the form add an ID to the data so I can save it to a json file and later use the data by ID?
This is my form where I get my data and send it to my controller to be saved.
<form method="POST" action="{{route('actor.store')}}">
#csrf
<div class="form-row">
<div class="form-group col-md-6">
<label for="input">Actor Name</label>
<input type="text" class="form-control" placeholder="Actor Name" name ="name" required="required" minlength="3" maxlegnth="30">
</div>
<div class="form-group col-md-6">
<label for="input">Actor Surname</label>
<input type="text" required="required" minlength="3" maxlegnth="30" class="form-control" id="actorSurname" placeholder="Actor Surname" name ="surName">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="input">Actor Nationallity</label>
<select id="nationallity" class="form-control" name ="nationallity" required="required" minlength="3" maxlegnth="30" >
<option selected>--Select Actor Nationallity.</option>
<option>South African</option>
</select>
</div>
<div class="form-group col-md-6">
<label for="input">Actor Age</label>
<input type="number" required="required" class="form-control" id="actorAge" placeholder="Actor Age" name ="age">
</div>
</div>
<a class="btn btn-primary" href="/actor/index" role="button">Back to List</a>
<button type="submit" class="btn btn-primary">Create</button>
</form>

GET-method don´t get the date in the URL

i have a Form with different inputs. Now I call GET and want to give all information to another site.
<form action="/listShifts" method="GET">
<div class="row">
<div class="m-1 col-sm-3">
<label class="control-label"><spring:message
code="shifts.startdate"/></label>
<input type="date" class="form-control" value="">
</div>
<div class="m-1 col-sm-3">
<label class="control-label"><spring:message
code="shifts.enddate"/></label>
<input type="date" class="form-control" value="">
</div>
<div class="m-1 col-sm-3">
<label class="control-label"><spring:message
code="shiftstatus.name"/></label>
<select name="stateId" class="form-control">
<option value=""><spring:message code="shifts.selectshiftstate"/></option>
<option value=""><spring:message code="shifts.allstates"/></option>
<c:forEach items="${states}" var="state">
<option value="${state.stateId}">${state.stateName}</option>
</c:forEach>
</select>
</div>
<button type="submit" class="mt-4 btn btn-outline-info align-self-center"><spring:message
code="shifts.filter2"/></button>
</form>
The state ID is written to the URL. The date not. How can i change this? Don´t know how to fix that problem.
You need to add name parameter to your inputs to submit through forms.
If you want to submit date, Just add name attribute to your date field.
<div class="m-1 col-sm-3">
<label class="control-label">
<spring:message code="shifts.startdate"/>
</label>
<input type="date" class="form-control" value="" name="start_date">
</div>
<div class="m-1 col-sm-3">
<label class="control-label">
<spring:message code="shifts.enddate"/>
</label>
<input type="date" class="form-control" value="" name="end_date">
</div>
Now, you are getting only state, Because you added name attribute only to your state dropdown.
<select name="stateId" class="form-control">
...

How can i access the uploaded file in the form given below, i'm getting undefined index error

The form
<form action="adminhandler.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="product name">Enter product name</label>
<input type="text" class="form-control" id="productName" aria-describedby="emailHelp" placeholder="" name="productname">
</div>
<div class="form-group">
<label for="product name">Select product category</label>
<select name="select" class=" custom-select">
<option selected>Open this select menu</option>
<option value="1">Formal Shoes</option>
<option value="2">Sneakers</option>
<option value="3">Tracksuits</option>
<option value="3">Clothing</option>
<option value="3">Accessories</option>
</select>
</div>
<div class="form-group">
<label for="product price">Enter product price(Naira)</label>
<input type="text" class="form-control" id="productPrice" aria-describedby="emailHelp" placeholder=""
name="price">
</div>
<div class="form-group">
<label for="exampleFormControlFile1">Example file input</label>
<input type="file" class="form-control-file" id="exampleFormControlFile1" name="rogue">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
My php code
$new=($_FILES['rogue']['name']);
print_r($new);
The exact error code is "Notice: Undefined index: rogue in C:\xampp\htdocs\eshop\data\adminhandler.php on line 13"

how to use different forms in one blade file in laravel 5.6

I am using laravel 5.6 and I am developing auto classifieds web application. in My app I have 3 different vehicle categories as car, van, truck and I have blade file to select this three different vehicle types. when I select this vehicles my urls show like this,
http://localhost:8000/post-ad/Truck/8 <- this is category id
http://localhost:8000/post-ad/van/7
http://localhost:8000/post-ad/Car/5
now when I clicked one of above vehicle category page redirect to show.blade.php file, so, now I need 3 different forms to submit data to each vehicles, this is my vehicles form, car form
<form method="post" action="{{url('form')}}" enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" id="cid" name="cid" value="{{ $catagories->id }}" />
<div class="form-group">
<label for="exampleFormControlSelect1">District</label>
<select class="form-control" id="exampleFormControlSelect1" name="district">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Town</label>
<select class="form-control" id="exampleFormControlSelect1" name="town">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Brand</label>
<select class="form-control" id="exampleFormControlSelect1" name="brand">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Model</label>
<select class="form-control" id="exampleFormControlSelect1" name="model">
</select>
</div>
</form>
van form
<form method="post" action="{{url('form')}}" enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" id="cid" name="cid" value="{{ $catagories->id }}" />
<div class="form-group">
<label for="exampleFormControlSelect1">District</label>
<select class="form-control" id="exampleFormControlSelect1" name="district">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Town</label>
<select class="form-control" id="exampleFormControlSelect1" name="town">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Brand</label>
<select class="form-control" id="exampleFormControlSelect1" name="brand">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Model</label>
<select class="form-control" id="exampleFormControlSelect1" name="model">
</select>
</div>
</form>
Truck Form
<form method="post" action="{{url('form')}}" enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" id="cid" name="cid" value="{{ $catagories->id }}" />
<div class="form-group">
<label for="exampleFormControlSelect1">District</label>
<select class="form-control" id="exampleFormControlSelect1" name="district">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Town</label>
<select class="form-control" id="exampleFormControlSelect1" name="town">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Brand</label>
<select class="form-control" id="exampleFormControlSelect1" name="brand">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Model</label>
<select class="form-control" id="exampleFormControlSelect1" name="model">
</select>
</div>
</form>
I need show each form when some user click each vehicle links on one blade file. how can I do this?
You may add name to your routes, for example:
Route::group(['prefix' => 'post-ad'], function () {
Route::get('Truck/{id}', 'TruckController#fetch')->name('track');
Route::get('Van/{id}', 'VanController#fetch')->name('van');
Route::get('Car/{id}', 'CarController#fetch')->name('car');
})
Put your form to another files like:
truck-form.blade.php
van-form.blade.php
car-form.blade.php
In your view:
#if(request()->route()->getName() = 'track')
#include('truck-form')
#elseif(request()->route()->getName() = 'van')
#include('van-form')
#elseif
#include('van-form')
#endif

Multi input type text to mysql php

I am trying to pass 5 values in same time using this code.
There is an error in strip_tags. Can any one tell me how to solve it,
php code >
$option = strip_tags($_POST['option']);
$survey_name = strip_tags($_POST['survey_name']);
if(isset($_POST['sendv'])){
if($option) {
foreach($option as $o){
$resource = mysql_query("INSERT INTO options
(value,ques_id)
VALUES
('$o','$survey_name')
"
)or die(mysql_error());}
html code >
<form action="" method="post" >
<div class="form-group">
<label for="email">Option 1</label>
<input type="text" class="form-control" name="option[]">
</div>
<div class="form-group">
<label for="email">Option 2</label>
<input type="text" class="form-control" name="option[]">
</div>
<div class="form-group">
<label for="email">Option 3</label>
<input type="text" class="form-control" name="option[]">
</div>
<div class="form-group">
<label for="email">Option 4</label>
<input type="text" class="form-control" name="option[]">
</div><div class="form-group">
<label for="email">Option 5</label>
<input type="text" class="form-control" name="option[]">
</div>
<div class="form-group">
<label for="email">Survey name</label>
<select class="form-control" name="survey_name" >
<option value="1" >1</option>
</select>
</div>
<button type="submit" name="sendv" class="btn btn-default">Add</button>
</form>
When I post it, this error appears..
strip_tags() expects parameter 1 to be string, array given in

Categories