Reputation: 941
I'm trying to send the value of a data attribute of an input tag other than the value
attribute itself from view to controller through the http
request, i tried using hidden input
tag but the problem is the input tag i'm trying to send is an option
tag, so adding a hidden input tag after the option tag breaks my dropdown list.
this is the code:
<select class="form-control" name="parent">
<?php
$pdo = new PDO('mysql:host=dbhost.dev;dbname=sdi;charset=utf8', 'sdiuser', 'sdiuser');
if(Auth::user()->usertype=="super"){
$sql = "SELECT * FROM capteurs where type like 'groupe'";}
else {
$sql = "SELECT * FROM capteurs where type like 'groupe' and etab like ".Auth::user()->etab;
}
$stmt = $pdo->prepare($sql);
$stmt->execute();
$groups = $stmt->fetchAll();
foreach($groups as $group): ?>
<option id="parent" value="<?= $group['id']; ?>" data-etab="{{$group['etab']}}" name="parent">
<?= $group['code_capteur']; ?>
</option>
<input name="group-etab" type="hidden" value="{{$group['etab']}}"/>
<?php endforeach; ?>
</select>
Upvotes: 0
Views: 2485
Reputation: 10877
One way is to add it to the value attribute, then parse it in the handler, for example:
<option id="parent" value="<?= $group['id'] . '|' . $group['etab']; ?>" name="parent">
Then in php (this is just for brevity, you should still properly sanitize inputs):
$array = explode('|', $_REQUEST['parent']);
$id = $array[0];
$etab = $array[1];
Upvotes: 1
Reputation: 2610
You can try
@foreach($groups as $group): ?>
<option id="parent" value="{{json_encode(['id' => $group['id'], 'etab' => $group['etab']])}}" >
<?= $group['code_capteur']; ?>
</option>
@endforeach
then at the controller, you can just
$data = json_decode($request->input('parent'));
Upvotes: 1
Reputation: 1
I think the best way is to create an input hidden with javascript, you can set the hidden value with the selected value. Regards
Upvotes: 0