Laravel multiple foreach for inserting data into database [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a table with form input inside it. Whenever I click the submit button, it will save all of the data into the database like this.
What should I do inside my Laravel Controller? Thanks in advance. Here's my code example
UPDATE
Result:
UPDATE
HTML Form Input:
<form action="/test-submit" method="post">
#csrf
<table id="example2" class="presence-table table table-bordered table-hover">
<thead>
<tr>
<th class="align-middle" rowspan="2">#</th>
<th class="align-middle" rowspan="2">Name</th>
<th class="align-middle" colspan="6">Date</th>
</tr>
<tr>
<th class="align-middle">1 Jan</th>
<th class="align-middle">2 Jan</th>
<th class="align-middle">3 Jan</th>
<th class="align-middle">4 Jan</th>
<th class="align-middle">5 Jan</th>
<th class="align-middle">6 Jan</th>
</tr>
</thead>
<tbody>
#php
$employees=["Gunawan","Roy"];
$dates = ["1 Jan", "2 Jan", "3 Jan", "4 Jan", "5 Jan", "6 Jan"];
#endphp
#foreach($employees as $key=>$value)
<tr class="data-row">
<td class="align-middle iteration"></td>
<input type="hidden" name="row[{{$key}}][employee_name]" value="{{$value}}">
<td>{{ $value }}</td>
#foreach ($dates as $date)
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][{{ $loop->iteration }}][presenceType]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<input type="hidden" name="row[{{$key}}][presence][{{ $loop->iteration }}][presencedate]" value="{{ $date }}">
#endforeach
</tr>
#endforeach
</tbody>
</table>
<button type="submit" class="float-right btn btn-info">Save</button>
</form>
UPDATE
Controller:
$data= collect($request->row)->map(function ($row,$key)use($request){
return collect($row['presence'])->map(function ($presence)use($row){
return ['name'=>$row['employee_name'],'value'=>$presence['presenceType'],'date'=>$presence['presencedate']];
});
})->flatten(1)->toArray();
dd($data);

Not sure from where date will get but for other data first change form design
<form action="{{route("test")}}" method="post">
#csrf
<table id="example2" class="presence-table table table-bordered table-hover">
<thead>
<tr>
<th class="align-middle" rowspan="2">#</th>
<th class="align-middle" rowspan="2">Name</th>
<th class="align-middle" colspan="6">Date</th>
</tr>
<tr>
<th class="align-middle">1 Jan</th>
<th class="align-middle">2 Jan</th>
<th class="align-middle">3 Jan</th>
<th class="align-middle">4 Jan</th>
<th class="align-middle">5 Jan</th>
<th class="align-middle">6 Jan</th>
</tr>
</thead>
<tbody>
#php
$employee=["Gunawan","Roy"];
#endphp
#foreach($employee as $key=>$value)
<tr class="data-row">
<td class="align-middle iteration"></td>
<input type="hidden" name="row[{{$key}}][employee_name]" value="{{$value}}">
<td>Gunawan</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][1]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][2]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][3]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][4]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][5]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][6]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
<button type="submit" class="float-right btn btn-info">Save</button>
</form>
and in controller
$data= collect($request->row)->map(function ($row,$key)use($request){
return collect($row['presence'])->map(function ($presence)use($row){
return ['name'=>$row['employee_name'],'value'=>$presence];
});
})->flatten(1)->toArray();
and for insert to database
ModelName::insert($data);
Updated
You can do somethink like below for each date
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][1]['presenceType']" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<input type="text" name="row[{{$key}}][presence][1]['presencedate']" value="">
</div>
</td>
and in controller
return ['name'=>$row['employee_name'],'value'=>$presence['presenceType'],'dae'=>$presence['presencedate']];
Update2
<form action="{{route("test")}}" method="post">
#csrf
<table id="example2" class="presence-table table table-bordered table-hover">
<thead>
<tr>
<th class="align-middle" rowspan="2">#</th>
<th class="align-middle" rowspan="2">Name</th>
<th class="align-middle" colspan="6">Date</th>
</tr>
<tr>
<th class="align-middle">1 Jan</th>
<th class="align-middle">date</th>
<th class="align-middle">2 Jan</th>
<th class="align-middle">3 Jan</th>
<th class="align-middle">4 Jan</th>
<th class="align-middle">5 Jan</th>
<th class="align-middle">6 Jan</th>
</tr>
</thead>
<tbody>
#php
$employee=["Gunawan","Roy"];
$dates = ["1 Jan", "2 Jan", "3 Jan", "4 Jan", "5 Jan", "6 Jan"];
#endphp
#foreach($employee as $key=>$value)
<tr class="data-row">
<td class="align-middle iteration"></td>
<input type="hidden" name="row[{{$key}}][employee_name]" value="{{$value}}">
<td>Gunawan</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][1][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][1][presenceDate]" required>
<option value="">-- Select --</option>
#foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
#endforeach
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][2][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][2][presenceDate]" required>
<option value="">-- Select --</option>
#foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
#endforeach
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][3][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][3][presenceDate]" required>
<option value="">-- Select --</option>
#foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
#endforeach
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][4][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][4][presenceDate]" required>
<option value="">-- Select --</option>
#foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
#endforeach
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][5][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][5][presenceDate]" required>
<option value="">-- Select --</option>
#foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
#endforeach
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][6][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][6][presenceDate]" required>
<option value="">-- Select --</option>
#foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
#endforeach
</select>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
<button type="submit" class="float-right btn btn-info">Save</button>
</form>
and in controller
$data= collect($request->row)->map(function ($row,$key)use($request){
return collect($row['presence'])->map(function ($presence)use($row){
return ['name'=>$row['employee_name'],'value'=>$presence['presenceValue'],'date'=>$presence['presenceDate']];
});
})->flatten(1)->toArray();
dd($data);

Related

Two tables next to each other

so I have this problem.
I need to put second table next (right) to the first table. Or more like to put it like 10px next to it but have the top of the tables on the same 'y' px; Also I dont the tables to be solid, because tables are in for() and there should be more tables then just two (now it is 1+1, then 1+1 and under it anoter 1+1 tables) Now the second table is under the first one.
I tried few things but still i can't figure out the right answer. I am not css master, i'm just learning. Can anybody help?
<div style="width: 400px">
#foreach ($reminders as $reminder)
<table style="margin-left: 550px; width: 100%; position: center; margin-top: 50px;">
<tr style="">
<th colspan="4">{{$reminder->title}}</th>
</tr>
<tr>
<th colspan="4" style="height: 150px; text-align: left">{{$reminder->text}}</th></tr>
<tr>
<th>
<form method="post" action="/donereminder" >
#csrf
<input type="hidden" name="rem" value="{{$reminder->id}}" readonly style="width: 0%">
<button type="submit" style="width: 100%">Done</button>
</form></a></th>
<th><button class="button" style="width: 100%">Report</button></th>
<th><button class="button" style="width: 100%">Edit</button></th>
<th>
<form method="POST" action="/reminders/{{ $reminder->id}}">
#method('DELETE')
#csrf
<div class="field">
<div class="control">
<button type="submit" class="button" style="width: 100%;">🗑 </button>
</div>
</div>
</form>
</th></tr>
</table>
<table style="margin-left: 550px; width: 100%; position: center; margin-top: 50px">
<form method="POST" action="/reminders/{{$reminder->id}}">
#method('PATCH')
#csrf
<tr>
<th>
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="title" placeholder="Name of reminder" value="{{$reminder->title}}">
</div>
</th>
</tr>
<tr>
<th>
<div class="form-group">
<label>Text</label>
<input type="text" class="form-control" name="text" placeholder="Description of your reminder" value="{{$reminder->text}}">
</div>
</th>
</tr>
<tr>
<th> <div class="form-group">
<label>Typ</label>
<select class="form-control" name="typ" value="{{$reminder->typ}}">
<option name="typ" value="0">Práce</option>
<option name="typ" value="1">Narozeniny</option>
<option name="typ" value="2">Sport</option>
<option name="typ" value="3">Jiné</option>\
</select>
</div></th>
</tr>
<tr>
<th> <div class="form-group">
<label>Priorita</label>
<select class="form-control" name="priority" value="{{$reminder->priority}}">
<option name="priority" value="{{$reminder->priority}}">{{$reminder->priority}}</option>
<option name="priority" value="10">10</option>
<option name="priority" value="9">9</option>
<option name="priority" value="8">8</option>
<option name="priority" value="7">7</option>
<option name="priority" value="6">6</option>
<option name="priority" value="5">5</option>
<option name="priority" value="4">4</option>
<option name="priority" value="3">3</option>
<option name="priority" value="2">2</option>
<option name="priority" value="1">1</option>
</select>
</div></th>
</tr>
<tr>
<th>
<div class="form-group">
<label>Done time</label>
<input class="datepicker" data-date-format="mm/dd/yyyy" type="date" placeholder="MM/DD/YYYY" name="donetime" value="{{$reminder->donetime}}">
</div>
</th>
</tr>
<tr>
<th>
<div class="field">
<div class="control">
<button type="submit" class="button is-link" style="width: 25%;">Upravit reminder</button>
</div>
</div>
</th>
</tr>
</table>
</form>
There are many ways to do that. I suggest you this one for example.
Considerations:
Use display:inline-block in the child elements. This way, all the elements will go at the same "horizontal line".
vertical-align is set by default as baseline.So set vertical-align: top to avoid this.
The number of tables that fit will depend on their width and the width of their parent.
You can adjust margin-right on each table for the distance between them.
Notice that I deleted your php code and added background to the tables
due to show the result properly.
<div>
<table style="display:inline-block;vertical-align:top;margin-right: 10px;background-color:lightgreen;">
<tr style="">
<th colspan="4">{{$reminder->title}}</th>
</tr>
<tr>
<th colspan="4" style="height: 150px; text-align: left">{{$reminder->text}}</th></tr>
<tr>
<th>
<form method="post" action="/donereminder" >
#csrf
<input type="hidden" name="rem" value="{{$reminder->id}}" readonly style="width: 0%">
<button type="submit" style="width: 100%">Done</button>
</form></a></th>
<th><button class="button" style="width: 100%">Report</button></th>
<th><button class="button" style="width: 100%">Edit</button></th>
<th>
<form method="POST" action="/reminders/{{ $reminder->id}}">
<div class="field">
<div class="control">
<button type="submit" class="button" style="width: 100%;">🗑 </button>
</div>
</div>
</form>
</th></tr>
</table>
<table style="display:inline-block;vertical-align:top;background-color:lightgray;">
<form method="POST" action="/reminders/{{$reminder->id}}">
<tr>
<th>
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="title" placeholder="Name of reminder" value="{{$reminder->title}}">
</div>
</th>
</tr>
<tr>
<th>
<div class="form-group">
<label>Text</label>
<input type="text" class="form-control" name="text" placeholder="Description of your reminder" value="{{$reminder->text}}">
</div>
</th>
</tr>
<tr>
<th> <div class="form-group">
<label>Typ</label>
<select class="form-control" name="typ" value="{{$reminder->typ}}">
<option name="typ" value="0">Práce</option>
<option name="typ" value="1">Narozeniny</option>
<option name="typ" value="2">Sport</option>
<option name="typ" value="3">Jiné</option>\
</select>
</div></th>
</tr>
<tr>
<th> <div class="form-group">
<label>Priorita</label>
<select class="form-control" name="priority" value="{{$reminder->priority}}">
<option name="priority" value="{{$reminder->priority}}">{{$reminder->priority}}</option>
<option name="priority" value="10">10</option>
<option name="priority" value="9">9</option>
<option name="priority" value="8">8</option>
<option name="priority" value="7">7</option>
<option name="priority" value="6">6</option>
<option name="priority" value="5">5</option>
<option name="priority" value="4">4</option>
<option name="priority" value="3">3</option>
<option name="priority" value="2">2</option>
<option name="priority" value="1">1</option>
</select>
</div></th>
</tr>
<tr>
<th>
<div class="form-group">
<label>Done time</label>
<input class="datepicker" data-date-format="mm/dd/yyyy" type="date" placeholder="MM/DD/YYYY" name="donetime" value="{{$reminder->donetime}}">
</div>
</th>
</tr>
<tr>
<th>
<div class="field">
<div class="control">
<button type="submit" class="button is-link" style="width: 25%;">Upravit reminder</button>
</div>
</div>
</th>
</tr>
</table>
</form>

php mysql can't multiply a mysql result and a variable

I can't for the life of me figure out why i keep coming up with 0 on this ... i'm pulling a price from one of my tables and multiplying it by the quantity they are putting in in the form ... for some reason it when i try and do the math, it keeps coming up as 0 ... any help?
code:
$get_code = "SELECT * FROM products WHERE p_code LIKE '%$po_code%'";
$run_code = mysqli_query($con, $get_code);
$row_code = mysqli_fetch_array($run_code);
$count = $_POST['ord_amt'];
$price = $row_code['p_price'];
$sum_total = $price * $count;
form code:
<form action="" method="post">
<?php
include('includes/connection.php');
global $con;
$cust = "SELECT * FROM users WHERE u_id = '".$_SESSION['u_id']."'";
$r_cs = mysqli_query($con, $cust);
$rw_c = mysqli_fetch_array($r_cs);
echo "<input type='hidden' name='c_id' value='".$rw_c['c_id']."'>";
?>
<table id="datatable1" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Product</th>
<th>Distal Opening</th>
<th>Material</th>
<th>Ply</th>
<th>Size</th>
<th>Length</th>
<th>Options</th>
<th>Or Enter Code (If Known)</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select class="form-control" name="prod" id="selectProd" onchange="get_prod(this.value)" required="required">
<?php get_prod_kind() ?>
</select>
</td>
<td>
<select class="form-control" name="opening" id="distOpen" required="required">
<option value="">Select Opening</option>
<option value="1">Rubberized Reinforced</option>
<option value="2">Standard Opening</option>
<option value="">None</option>
</select>
</td>
<td>
<select class="form-control" name='mat' id="selectMat" onchange="get_ply(this.value)" required="required">
</select>
</td>
<td>
<select class="form-control" name="ply" id="plySelect" onchange="get_size(this.value)" required="required">
<option value=""></option>
</select>
</td>
<td>
<select class="form-control" name="size" id="sizeSelect" onchange="get_len(this.value)" required="required">
<option value=""></option>
</select>
</td>
<td>
<select class="form-control" name="length" id="selectLength" required="required">
<option value=""></option>
</select>
</td>
<td>
<select class="form-control" name="opts" required="required">
<option value="">Select Options</option>
<option value="1">Sewn Top (S)</option>
<option value="2">Seal-In-Liner (C)</option>
<option value="">None</option>
</select>
</td>
<td><input class="form-control" type='text' name='cust_code'></td>
<td><input class="form-control" type='text' name='ord_amt' required="required"></td>
</tr>
</tbody>
</table>
<button class="button button-3d button-rounded button-aqua" name="p_order"><i class="icon-repeat"></i>Next Item</button>
<button class="button button-3d button-rounded button-green"><i class="icon-ok"></i>Submit Order</button>
</form>

Post table rows which are checked with other select boxes

This may be a duplicate question,
But still, I did not find the answer.
My requirement is :
How to get the table rows which are CHECKED.
Here is my issue's screenshot. enter image description here
My checkbox name is 'check[]'
and the name of the select box is 'class[]',
and the form is posted to process.php.
How to get the value of 'class[]' where the checked boxes are checked so that I can then process with PHP-MYSQL?
My code is here:
<div class="col-md-5">
<form action="process.php" method="post">
<table class="table" id="table">
<tr><th>Opt</th><th>Name</th><th>Next Class</th></tr>
<? $q = $sdb->where('ac_CurClass',4)->get('tbl_accounts');
foreach ($q as $r){ ?>
<tr>
<td><input type="checkbox" name="sel[]" class="checkbox small" value="<?=$r['ac_Id'];?>"></td>
<td><?=ac_details($r['ac_Id'])->ac_Name;?></td>
<td>
<select class="form-control small" name="class[]">
<? $c = $sdb->where('c_Id',ac_details($r['ac_Id'])->ac_CurClass,">")->get("tbl_classes");
foreach($c as $d) {?>
<option value="<?=$d['c_Id'];?>"><?=$d['c_Name'];?></option>
<? } ?></select>
</td>
</tr>
<? } ?>
</table>
<button type="submit" class="btn btn-danger">Update</button>
</form>
</div>
Hope this code is help full for you
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<div class="col-md-5">
<form action="#" method="post">
<table class="table" id="table">
<thead><tr><th>Opt</th><th>Name</th><th>Next Class</th></tr></thead>
<tbody id="cont">
<tr>
<td><input type="checkbox" name="sel[]" class="checkbox small" value="1"></td>
<td>asass</td>
<td>
<select class="form-control small" name="class[]">
<option value="">Select option</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</td>
</tr>
<tr>
<td><input type="checkbox" name="sel[]" class="checkbox small" value="2"></td>
<td>asass</td>
<td>
<select class="form-control small" name="class[]">
<option value="">Select option</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</td>
</tr>
<tr>
<td><input type="checkbox" name="sel[]" class="checkbox small" value="3"></td>
<td>asass</td>
<td>
<select class="form-control small" name="class[]">
<option value="">Select option</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-danger" id="submit">Update</button>
</form>
</div>
<script>
$(document).ready(function(){
$('#submit').click(function(){
$("#cont input:checkbox:checked").map(function(){
alert('Checkbox value - '+$(this).val()+' / Select Box value - '+$(this).parent().next().next('td').find('select').val());
//alert('Select Box value - '+$(this).parent().next().next('td').find('select').val());
});
});
});
</script>
After that you can pass a Ajax so that only checked rows post.
Try as follows
Post as form
<form action="process.php" method="post">
In process.php file
if($check[0]===true){
$nextclass1 = $_POST['list_box'];
}
if($check[1]===true){
$nextclass2= $_POST['list_box'];
}

select option selected in loop

I am trying to get the value from a selectbox. The form is placed multiple times on my page with a php loop (that's why I use classes instead of id's).
<html>
<body>
<table>
<form class="lesmaker">
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info">Informatie</option>
<option value="discussie">Klassikaal</option>
<option value="meerkeuze" selected="selected">Meerkeuze</option>
</select>
</td>
</tr>
<tr>
<td colspan="5">
<select name="media" class="media">
<?php foreach($images as $image){if(substr($image,0,1) != '.'){ ?>
<option style="background-image:url(<?=$d_images.$image?>)" value="<?=$image?>" <?php if($r_media== $image) echo 'selected="selected"'; ?>><?=$image?></option>
<?php }} ?>
</select>
</td>
</tr>
</form>
</table>
<table>
<form class="lesmaker">
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info">Informatie</option>
<option value="discussie">Klassikaal</option>
<option value="meerkeuze" selected="selected">Meerkeuze</option>
</select>
</td>
</tr>
<tr>
<td colspan="5">
<select name="media" class="media">
<?php foreach($images as $image){if(substr($image,0,1) != '.'){ ?>
<option style="background-image:url(<?=$d_images.$image?>)" value="<?=$image?>" <?php if($r_media== $image) echo 'selected="selected"'; ?>><?=$image?></option>
<?php }} ?>
</select>
</td>
</tr>
</form>
</table>
</body>
</html>
With jQuery I am trying to get the selected option.
$(function(){
$('.lesmaker').each(function() {
console.log($(this).find('.type').val());
});
});
The code above is not working. How do I need to do this?
$(function() {
$('.lesmaker').each(function() {
console.log($(this).find('table tr td').find('.type').val());
});
});
You need to change your HTML to following:
<form class="lesmaker">
<table>
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info">Informatie</option>
<option value="discussie">Klassikaal</option>
<option value="meerkeuze" selected="selected">Meerkeuze</option>
</select>
</td>
</tr>
</table>
</form>
form needs to be either outside of table or inside of td :)
$(function() {
$('.type').each(function() {
console.log($('option:selected', this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<form class="lesmaker">
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info">Informatie</option>
<option value="discussie">Klassikaal</option>
<option value="meerkeuze" selected="selected">Meerkeuze</option>
</select>
</td>
</tr>
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info">Informatie</option>
<option value="discussie" selected="selected">Klassikaal</option>
<option value="meerkeuze" >Meerkeuze</option>
</select>
</td>
</tr>
<tr>
<td colspan="5">
<select name="type" class="type">
<option value="open">Open vraag</option>
<option value="info" selected="selected">Informatie</option>
<option value="discussie" >Klassikaal</option>
<option value="meerkeuze" >Meerkeuze</option>
</select>
</td>
</tr>
</form>
</table>
Just use the class of select and use $('option:selected', this).val()

How to generate a query string (url) from a form, php or jquery?

I'm trying to generate a specific url from the form below.
There will be a calendar and the date will go into the url.
Also a value from 9 ticket types and quantities of each of those tickets.
So my url will look something like this
https://sales.site.com/?action=quicksale&venueid=1&businessdate=2012-10-05&ticketids=6,8&quantities=1,1
With these values:
action: always "quicksale",
venueid: always "1",
businessdate: the sales date requested; can be either mm-dd-yyyy or yyyy-mm-dd,
ticketids: see chart below,
quantities: in the order the ticketid's are listed.
How would I approach this, and should I use php or jquery? Post or Get.
Should the calendar be Jquery?
Looking for advice, thanks
<form method="post" action="">-->
<div class="quick-book-form-elements">
<table width="255" class="quick-book-table" cellpadding="4">
<tbody>
<tr class="quick-book-days">
<th> </th>
<th><img src="../public/img/1day-small.png" alt="" /></th>
<th><img src="../public/img/2day-small.png" alt="" /></th>
<th class="quick-book-last"><img src="../public/img/3day-small.png" alt="" /></th>
</tr>
<tr class="quick-book-adults">
<th class="quick-book-labels">
Adult
</th>
<td>
<div class="ticket-style-quick-book">
<select class="adult-ticket-select">
<option value="0">0</option>
<option value="1">1</option>
</select>
<span class="quick-book-price">$40/ea</span>
</div>
</td>
<td>
<div class="ticket-style-quick-book">
<select class="adult-ticket-select">
<option value="0">0</option>
<option value="1">1</option>
</select>
<span class="quick-book-price">$50/ea</span>
</div>
</td>
<td class="quick-book-last">
<div class="ticket-style-quick-book">
<select class="adult-ticket-select">
<option value="0">0</option>
<option value="1">1</option>
</select>
<span class="quick-book-price">$60/ea</span>
</div>
</td>
</tr>
<tr class="quick-book-child">
<th class="quick-book-labels">Child</th>
<td>
<div class="ticket-style-quick-book">
<select class="adult-ticket-select">
<option value="0">0</option>
<option value="1">1</option>
</select>
<span class="quick-book-price">$30/ea</span>
</div>
</td>
<td>
<div class="ticket-style-quick-book">
<select class="adult-ticket-select">
<option value="0">0</option>
<option value="1">1</option>
</select>
<span class="quick-book-price">$40/ea</span>
</div>
</td>
<td class="quick-book-last">
<div class="ticket-style-quick-book">
<select class="adult-ticket-select">
<option value="0">0</option>
<option value="1">1</option>
</select>
<span class="quick-book-price">$50/ea</span>
</div>
</td>
</tr>
<tr class="quick-book-family">
<th class="quick-book-labels">Family</th>
<td>
<div class="ticket-style-quick-book">
<select class="adult-ticket-select">
<option value="0">0</option>
<option value="1">1</option>
</select>
<span class="quick-book-price">$125/ea</span>
</div>
</td>
<td>
<div class="ticket-style-quick-book">
<select class="adult-ticket-select">
<option value="0">0</option>
<option value="1">1</option>
</select>
<span class="quick-book-price">$155/ea</span>
</div>
</td>
<td class="quick-book-last">
<div class="ticket-style-quick-book">
<select class="adult-ticket-select">
<option value="0">0</option>
<option value="1">1</option>
</select>
<span class="quick-book-price">$185/ea</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<input type="submit" class="quick-book-submit-btn" name="quick-book-submit-btn" value="" />
</form>
</div><!-- end quick-book-form div -->
For the calendar, indeed you could use the jQuery-ui one. It is a good widget, easy to configure and very powerful.
http://jqueryui.com/demos/datepicker/
You can configure it to put the format you want in the input (YYYY-MM-DD for example)
Finally you should add a listener on the submit event, to perform your action modification:
// This function will be call before the submit
$("#YOUR_FORM_ID").submit(function(submitEvent) {
// Initialize the query param array
var queryParams = [];
// Fill the query param with values
queryParams.push("action=quicksale");
queryParams.push("venueid=1");
queryParams.push("businessdate=" + $("#YOUR_INPUT_DATE_ID").val());
// etc...
// Finally update the form action
$(this).attr('action', 'https://sales.site.com/?' + queryParams.join("&"));
// The action has been updated
// The submit will process
});

Categories