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>
Related
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);
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'];
}
For some reason every once in a while my code will enter data into the database twice. I have changed my code and made some tweaks but have not had any luck. I know I must be over looking something I just don't know what.
<?php
$con = new mysqli("localhost", "******", "******", "******");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$conpo = new mysqli("localhost", "******", "******", "******");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$resultpo = mysqli_query($conpo, "SELECT * FROM fpo ORDER BY ponum DESC LIMIT 1");
while ($rowp = mysqli_fetch_array($resultpo)) {
$po = $rowp['ponum'];
$ponum = ++$po;
}
$date = $_POST['date'];
$to = $_POST['to'];
$time = $_POST['time'];
$vin = $_POST['stk_vin'];
$reason = $_POST['reason'];
$amount = $_POST['amount'];
$empnum = $_POST['emp'];
$mananger = $_POST['mananger'];
$accnum = $_POST['acc'];
$store_location = $_POST['store'];
$borr = $_POST['borrowed'];
$oldid = $_POST['oldid'];
mysqli_query($con,
"INSERT INTO fpo (" .
"`ponum`, `date`, `to`, `time`, `vin`, `reason`, `amount`, `empnum`, `mananger`, `accnum`, `store_location`, `borr`" .
") VALUES (" .
"'$ponum', '$date', '$to', '$time', '$vin', '$reason', '$amount', '$empnum', '$mananger', '$accnum', '$store_location', '$borr'" .
")");
?>
Here is the html forum page.
<form action="print.php" method="POST" name="form1" id="form1">
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" class="table" style="width:700px">
<tbody>
<tr>
<td height="19" colspan="2" valign="middle" class="to" style="width:450px" ><div class="logo"></div></td>
<td width="95" rowspan="2" valign="bottom"><div class="date">Date:
<label>
<input name="date" type="text" readonly id="date" value="07/15/2015" size="10" />
</label>
</div></td>
<td width="106" rowspan="2" valign="bottom" class="po" style="width:100px" ><div class="date">PO: <span id="sprytextfield3">
<label>
<input name="po" readonly type="text" value="14739" />
<input type="hidden" name="oldid" value="11810">
</label>
<span class="textfieldRequiredMsg">A value is required.</span></span></div></td>
</tr>
<tr>
<td height="20" colspan="2" valign="middle" class="to" style="width:450px" ><div class="toheader">To: <span id="spryselect1">
<label>
<select name="to" id="to">
<option selected="selected" disabled="disabled">Select One</option>
<option value="1">Kum-N-Go</option>
</select>
</label>
<span class="selectRequiredMsg">Please select an item.</span></span>
<input name="time" type="hidden" id="time" value="
11:13:33 ">
</div></td>
</tr>
<tr>
<td colspan="2" style="height:125px" ><b> Reason For Gas Ticket: </b> <br>
<span id="spryselect2">
<label>
<select name="reason" id="reason">
<option selected="selected" disabled="disabled">Select One</option>
<option value="Advertising">Advertising</option>
<option value="Auction">Auction</option>
</select>
</label>
<span class="selectRequiredMsg">Required</span></span><br>
<br>
Car Borrowed From Another Dealership? <br>
<span id="spryselect3">
<label>
<select name="borrowed" id="borrowed">
<option selected="selected" disabled="disabled">Select One</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</label>
<span class="selectRequiredMsg">Please select an item.</span></span><br></td>
<td><b>stk/vin/RO#:</b><br>
<span id="sprytextfield2">
<label>
<input name="stk_vin" type="text" id="stk_vin" onclick="return stk()" size="12" />
</label>
<span class="textfieldRequiredMsg"><br />
Stock or Vin .</span></span></td>
<td align="center"><b> Amount<br>
<div class="money"><span id="spryselect5">
<label><b>
<select name="amount" id="amount">
<option value="5.00" selected="selected">5.00</option>
<option value="1.00">1.00</option>
<option value="2.00">2.00</option>
<option value="3.00">3.00</option>
<option value="4.00">4.00</option>
<option value="5.00">5.00</option>
<option value="6.00">6.00</option>
<option value="7.00">7.00</option>
<option value="8.00">8.00</option>
<option value="9.00">9.00</option>
<option value="10.00">10.00</option>
</select>
</b></label>
<span class="selectRequiredMsg">Please select an item.</span></span></div>
</b></td>
</tr>
<tr>
<td width="257">Employee Requesting Po:<span id="spryselect9">
<label>
<select name="emp" id="acc">
<option selected="selected" disabled="disabled">Select One</option>
<option value="5223">5223</option>
</optgroup>
</select>
</label>
<span class="textfieldRequiredMsg">Required</span><span class="textfieldInvalidFormatMsg">Required</span></span></td>
<td width="219">Acc #:<span id="spryselect4">
<label>
<select name="acc" id="acc">
<option selected="selected" disabled="disabled">Select One</option>
<option value="51">Sales</option>
<option value="52">Services</option>
<option value="53">Parts</option>
<option value="FI">F&I</option>
</select>
</label>
<span class="selectRequiredMsg">Required</span></span></td>
<td colspan="2" align="right"><label> Mananger:
<input name="mananger" onclick="return getfoc()" type="text" id="mananger" size="12" readonly />
</label></td>
</tr>
<tr>
<td></td>
<td>Store: <span id="spryselect6">
<label>
<select name="store" id="store">
<option disabled="disabled" selected="selected">Select One</option>
<option value="chevy">Chevy</option>
<option value="Ford">Ford</option>
<option value="Dodge">Dodge</option>
</select>
</label>
<span class="selectRequiredMsg">Required</span></span></td>
<td colspan="2" align="right"><div id="btn"> </div>
<script language='javascript' type='text/javascript'>
function GetValueFromChild(myVal)
{
document.getElementById('mananger').value = myVal;
document.getElementById('btn').innerHTML = '<input type="submit" id="register" value="Print Ticket" />';
}
</script></td>
</tr>
</tbody>
</table>
<p> </p>
</form>
Because you are not doing atomic operation. Lets assume the below flow:
SELECT1 -> SELECT2-> INSERT1-> INSERT2
How are avoid this - no synchronization or locking. You can avoid this with few options.
Use a lock before entering this block and free once insert is done( in PHP). This can be based on file lock or memcache lock
You can add an index on the column you want to keep unique say, (ponum, from, to). Instead of using INSERT, you can use INSERT IGNORE
I am pretty sure your users are clicking the submit button twice I had this issue before I fixed it using jquery's validator such as following.
$(document).ready(function(){
$("#form1").validate({submitHandler: function(form) {if (!this.beenSubmitted) {this.beenSubmitted = true;form.submit();} }});
});
Hi i'm creating students marks entry list using php and mysql. Let say students subjects as "english,maths,social,science and computers" in database and Now i need to display these values from dropdown list as each row will need to show different value automatically.
<?php
$user = $_SESSION['loginUserId'];
$commonQuery = mysql_query("select sno from users where userId = '$user' and status='Active'");
$count = mysql_num_rows($commonQuery);
if($count == 1)
{
$commonQueryRes = mysql_fetch_array($commonQuery);
$uId=$commonQueryRes['sno'];
$subjectsCount = mysql_query("select COUNT(subjectName) as sub_count from subjects where userId='$uId' and status='Active' ");
$subjectsCountRes = mysql_fetch_array($subjectsCount);
$scount=$subjectsCount['sub_count'];
}
$bg = "light";
for($i=1;$i<=$scount;$i++)
{
if ($bg == "light") $bg = "dark";
else $bg = "light";
?>
<tr class="<?php echo $bg; ?>">
<td>
<select name="subName[]">
<option value="none">--select Subject--</option>
<?php
$subjectOptions = mysql_query("select subjectName from subjects where userId='$uId' and status='Active' ");
while($subjectOptionsRes = mysql_fetch_array($subjectOptions))
{
$sub = $subjectOptionsRes['subjectName'];
$sub_Sub_Options = mysql_query("select sno from subjects where userId='$uId' and subjectName='$sub'");
$sub_Sub_OptionsRes = mysql_fetch_array($sub_Sub_Options);
?>
<option value="<?php echo $sub_Sub_OptionsRes['sno']; ?>" selected="selected"><?php echo $subjectOptionsRes['subjectName']; ?></option>';
<?php
}
?>
</select>
</td>
<td>
<select name="result[]">
<option value="none">--select Result--</option>
<option value="pass"><?php echo "Pass";?></option>
<option value="fail"><?php echo "Fail";?></option>
</select>
</td>
<td><input type="text" name="quartely[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="halfearly[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="anually[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="rank[]" required size="5"/>(Kgs)</td>
<?php
}
?>
And Expected OUTPUT Should Be like this and It should be dynamic:
<table>
<thead>
<tr>
<th style="background: #5792C8;" rowspan="2">subject Name</th>
<th style="background: #5792C8;" rowspan="2">Result</th>
<th style="background: #5792C8;" colspan="4"> Progress </th>
</tr>
<tr>
<th>Quarterly</th>
<th>Halfearly</th>
<th>Annually</th>
<th>Rank</th>
</tr>
</thead>
<tbody>
<tr class="dark">
<td>
<select name="subjectName[]">
<option value="none">--select Subject--</option>
<option value="6" selected="selected">English</option>';
<option value="7" >Maths</option>';
<option value="8" >Science</option>';
<option value="9" >Social</option>';
<option value="10" >Computers</option>';
</select>
</td>
<td>
<select name="result[]">
<option value="none">--select Result--</option>
<option value="pass">Pass</option>
<option value="fail">Fail</option>
</select>
</td>
<td><input type="text" name="qua[]" required size="5"/></td>
<td><input type="text" name="half[]" required size="5"/></td>
<td><input type="text" name="anual[]" required size="5"/></td>
<td><input type="text" name="rank[]" required size="5"/></td>
<tr class="light">
<td>
<select name="subjectName[]">
<option value="none">--select Subject--</option>
<option value="6" >English</option>';
<option value="7" selected="selected" >Maths</option>';
<option value="8" >Science</option>';
<option value="9" >Social</option>';
<option value="10" >Computers</option>';
</select>
</td>
<td>
<select name="result[]">
<option value="none">--select Result--</option>
<option value="pass">Pass</option>
<option value="fail">Fail</option>
</select>
</td>
<td><input type="text" name="qua[]" required size="5"/></td>
<td><input type="text" name="half[]" required size="5"/></td>
<td><input type="text" name="anual[]" required size="5"/></td>
<td><input type="text" name="rank[]" required size="5"/></td>
<tr class="dark">
<td>
<select name="subjectName[]">
<option value="none">--select Subject--</option>
<option value="6" >English</option>';
<option value="7" >Maths</option>';
<option value="8" selected="selected" >Science</option>';
<option value="9" >Social</option>';
<option value="10" >Computers</option>';
</select>
</td>
<td>
<select name="result[]">
<option value="none">--select Result--</option>
<option value="pass">Pass</option>
<option value="fail">Fail</option>
</select>
</td>
<td><input type="text" name="qua[]" required size="5"/></td>
<td><input type="text" name="half[]" required size="5"/></td>
<td><input type="text" name="anual[]" required size="5"/></td>
<td><input type="text" name="rank[]" required size="5"/></td>
</tr>
</tbody>
</table>
I got solution,here my solution is:
I took $count=0 and i'm increasing $count+=1 for each row adding dynamically. And i took subjuct name in one array as $sub_array[] and doing if($sub_array[$count]==$subjectOptionsRes['subjectName']){echo 'selected="selected"';}
<?php
$user = $_SESSION['loginUserId'];
$commonQuery = mysql_query("select sno from users where userId = '$user' and status='Active'");
$count = mysql_num_rows($commonQuery);
if($count == 1)
{
$commonQueryRes = mysql_fetch_array($commonQuery);
$uId=$commonQueryRes['sno'];
$subjectsCount = mysql_query("select COUNT(subjectName) as sub_count from subjects where userId='$uId' and status='Active' ");
$subjectsCountRes = mysql_fetch_array($subjectsCount);
$scount=$subjectsCount['sub_count'];
}
$bg = "light";
$count=0;
for($i=1;$i<=$scount;$i++)
{
if ($bg == "light") $bg = "dark";
else $bg = "light";
?>
<tr class="<?php echo $bg; ?>">
<td>
<select name="subName[]">
<option value="none">--select Subject--</option>
<?php
$subjectOptions = mysql_query("select subjectName from subjects where userId='$uId' and status='Active' ");
while($subjectOptionsRes = mysql_fetch_array($subjectOptions))
{
$sub = $subjectOptionsRes['subjectName'];
$sub_array[] = $subjectOptionsRes['subjectName'];
$sub_Sub_Options = mysql_query("select sno from subjects where userId='$uId' and subjectName='$sub'");
$sub_Sub_OptionsRes = mysql_fetch_array($sub_Sub_Options);
?>
<option value="<?php echo $sub_Sub_OptionsRes['sno']; ?>" if($sub_array[$count]==$subjectOptionsRes['subjectName']){echo 'selected="selected"';}><?php echo $subjectOptionsRes['subjectName']; ?></option>';
<?php
}
?>
</select>
</td>
<td>
<select name="result[]">
<option value="none">--select Result--</option>
<option value="pass"><?php echo "Pass";?></option>
<option value="fail"><?php echo "Fail";?></option>
</select>
</td>
<td><input type="text" name="quartely[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="halfearly[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="anually[]" required size="5"/>(Kgs)</td>
<td><input type="text" name="rank[]" required size="5"/>(Kgs)</td>
<?php
$count+=1;
}
?>
The very first field named Forte ID posts a value of 0 no matter what selection I choose. Here are my two files:
Index.php(Form)
</head>
<h1> Customer Service Log Form </h1>
<form method="post" action="insert.php">
<table width="625" border="0">
<tr>
<td> Forte ID:</td>
<td><select id="ForteID" name="ForteID">
<option value="0">Select Your ID</option>
<option value="as7326">as7326</option>
<option value="aw8743">aw8743</option>
<option value="bj0920">bj0920</option>
<option value="bs1441">bs1441</option>
<option value="dk7017">dk7017</option>
<option value="dl7686">dl7686</option>
<option value="dm2940">dm2940</option>
<option value="jn2468">jn2468</option>
<option value="jw9598">jw9598</option>
<option value="kp4945">kp4945</option>
<option value="nl2589">nl2589</option>
<option value="rp7021">rp7021</option>
<option value="sh1346">sh1346</option>
</select></td>
</tr>
<tr>
<td> Disposition</td>
<td><select id="disposition" name="disposition">
<option value="0">Select a Disposition</option>
<option value="Save">Save</option>
<option value="Sale">Sale</option>
<option value="LOC">LOC</option>
<option value="Backout">Backout</option>
<option value="Revision">Revision</option>
<option value="Revision">Revision/Save</option>
</select>
</td>
</tr>
</table>
<hr />
<br />
<table width="400" border="0">
<tr>
<td>App Number:</td>
<td></td>
<td><input name="appnumber" type="text" required="required"></td>
</tr>
<tr>
<td>Finance Number:</td>
<td></td>
<td><input type="text" name = "Finance_Num"></td>
</tr>
<tr>
<td>Phone Number:</td>
<td></td>
<td><input type="text" name = "Phone_Num"></td>
</tr>
<tr>
<td># of Payments Collected:</td>
<td></td>
<td><input type="text" name = "num_payments"></td>
</tr>
<tr>
<td>ACH/CC</td>
<td></td>
<td><select id="ForteID" name="ForteID">
<option value="0">Select Payment</option>
<option value="ach">Checking</option>
<option value="cc">Credit Card</option>
</select></td>
</tr>
<tr>
<td>Date:</td>
<td></td>
<td><input name = "date" type="text" id="datepicker" autocomplete="off" required="required"></td>
</tr>
</table>
<br />
Notes:
<br />
<textarea name="notes" id="notes" cols="45" rows="5"></textarea>
</fieldset>
<hr />
<input type="Submit" name="formSubmit" value="Submit">
<input type="Reset" name="formReset" value="Reset">
<input type="button" value="View Logs" onClick="window.location.href='logs.php';">
</form>
</head>
Insert.php (PHP file to insert data from form into SQL Server Database):
$serverName = 'Server\SQLEXPRESS';
$connectionInfo = array('Database'=>'database', 'UID'=>'username', 'PWD'=>'password');
$connection = sqlsrv_connect($serverName, $connectionInfo);
if( $connection === false )
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "INSERT INTO logs(ForteID, disposition, appnumber, Finance_Num, num_payments, ach_cc, date, notes, Phone_Num) VALUES (?,?,?,?,?,?,?,?,?)";
$parameters = array( $_POST[ForteID], $_POST[disposition], $_POST[appnumber], $_POST[Finance_Num], $_POST[num_payments], $_POST[ach_cc], $_POST[date], $_POST[notes], $_POST[Phone_Num]);
$stmt = sqlsrv_query($connection, $tsql, $parameters);
if( $stmt === false ){
echo "Statement could not be executed.\n";
die( print_r( sqlsrv_errors(), true));
} else {
echo "Rows affected: ".sqlsrv_rows_affected( $stmt )."\n";
}
No matter what option is selected in the index.php field Forte ID, it posts a value of 0. What is wrong. It was working before I added a field named Phone Number. But doesnt make sense why that would screw up the selections.
Let me know if I need to clarify anything and thanks for the help in advance!
You have to fields called ForteID
<select id="ForteID" name="ForteID">
<option value="0">Select Payment</option>
<option value="ach">Checking</option>
<option value="cc">Credit Card</option>
is the 2nd one