Send multiple data using checkbox via ajax - php

I have created a form which contains a table, the purpose of this is table is to select the desired fees to manage
<form method="POST" action="manage_fees.php" id="manage_form">
<table class="table table-striped table-bordered table-hover" id="table">
<thead>
<tr class="p-4">
<th scope="col">Select</th>
<th scope="col">School fees</th>
<th scope="col">Amount</th>
<th scope="col">type</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input check_amount" name="local_fees">
<label class="custom-control-label" for="check_amount"></label>
</div>
</td>
<td name="selected_fees">Local fees</td>
<td name="amount">200</td>
<td>University fees</td>
</tr>
</tbody>
</table>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" name="submit">Create</button>
</div>
</form>
Now I want to record it all data("fee name and total payment") that has checked in checkbox table and save in database using ajax.
Once the fees is selected it will display on the on input just like this
<div class="form-group">
label for="fs">Fees selected</label>
<input type="text" class="form-control" id="fs" name="fs" required disabled>
</div>
<div class="form-group">
<label for="tp">Total payment</label>
<input type="number" class="form-control" id="tp" name="tp" required disabled>
</div>
Now, the problem is that im having trouble saving the data that has been checked in table seems there is no data that has been pass in the input.
Sumbit.php
if(isset($_POST['submit'])){
$fs = $_POST['fs'];
$tp = $_POST['tp'];
$result = $connect->query("INSERT INTO manage_fees (fs,tp) VALUES ('$fs','$tp')") or die($connect->error());
}
Instead of this way how will send the data using ajax?

Related

PHP/MYSQL: Saving array data in table rows to mysql database using checkbox

I have an order table in my project. I am pulling the products from the database to this table using foreach. I show the quantity and unit price calculation information on the table instantly with jquery. I want to save the data in this table from the row selected with the checkbox to the database. I tried a few things with foreach but it looped so it created a new record for each row in the database. I want to print arrays with implode using commas between them. For example, the data in the rows entered in the quantity field in the table should be entered in the quantity field in the database as 1,2,3,4. If I have to explain briefly, I want to make an insert in one go.
Table:
Table & Form Code:
<form action="" method="POST">
<table class="table table-sm mb-3 text-center align-middle">
<thead>
<tr>
<th>Choose</th>
<th>Product Name</th>
<th width="137px">Quantity</th>
<th>Unit Price</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
<?php foreach($ProductTbl as $product){ ?>
<tr>
<th scope="row">
<div class="form-check form-check-success">
<input class="form-check-input" name="check[]" type="checkbox" value="
<?= $product-> productid">
</div>
</th>
<td>
<?= $product->productname ?>
</td>
<td>
<input class=" w-25 text-center quantity" type="text" name="quantity[]" value="0">
</td>
<td>
<input class="w-25 text-center unitprice" type="text" name="unitprice[]" value="
<?= $product->unitprice ?>" disabled="disabled"> €
</td>
<td>
<input class="w-25 text-center totalprice" type="text" name="totalprice[]" value="" disabled="disabled"> €
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="text-center">
<button class="btn btn-success col-md-2" type="submit" name="add">Offer Preview</button>
<button class="btn btn-warning col-md-1" type="reset">Reset</button>
<a href="#">
<button class="btn btn-danger col-md-1" type="button">Cancel</button>
</a>
</div>
</form>
//The code structure I tried
if(isset($_POST['add'])){
$check = $_POST['check'];
$quantity = implode(',', $_POST['quantity']);
$totalprice = implode(',', $_POST['totalprice']);
if (!empty($check)) {
foreach ($check as $chk => $value){
// I printed it for testing.
echo $value.' - product id in the checked checkbox<br>';
print_r($quantity);
print_r($totalprice);
}
}
}
How does this code work the way I want?

Collect the values of checkboxes with jQuery

I have a website to predict football results. Each soccer match has three checkboxes.
1-First team win(1checkbox)
2-Second team win(1checkbox)
3-Equal(1checkbox)
There are 10 soccer matches on one page. So, on a page, I have 30 checkboxes. The user can select all three checkboxes for each football match.
I need jQuery code to work like this website:
https://web.archive.org/web/20100529040418/http://www.bwin90.com/
if you select two or three checkboxes in soccer match, the numbers are exponentially rising.
My html and php codes:
<form method="POST">
<div class="form-group mt-4">
<div class="w-100 p-3">
<table class="table">
<thead class="thead-dark">
<tr>
<th class="text-center" scope="col"></th>
<th class="text-center" scope="col">team1</th>
<th class="text-center" scope="col">equal</th>
<th class="text-center" scope="col">team2</th>
<th class="text-center" scope="col"></th>
</tr>
</thead>
<tbody>
<form>
<?php
$get_teams1="SELECT * FROM teams WHERE show_match=1 ORDER BY team_id DESC";
$get_teams2 = mysqli_query($conn, $get_teams1);
while($get_teams3=mysqli_fetch_assoc($get_teams2))
{
echo'
<tr>
<td class="text-right"><input type="checkbox" class="qty1 form-check-input" value="20" id="exampleCheck1"></td><td class="text-center">'.$get_teams3["mizban"].'</td>
<td class="text-center"><input type="checkbox" class="qty1 form-check-input" id="exampleCheck1"></td>
<td class="text-center">'.$get_teams3["mihman"].'</td><td class="text-right"><input type="checkbox" class="qty1 form-check-input" id="exampleCheck1"></td>
</tr>
';
}
?>
<input type="text" class="total" value="" />
</form>
</tbody>
</table>
<button type="button" class="btn btn-dark mt-2 w-100" name="register3" />
<h5>ثبت فرم</h5>
</button>
</div>
</div>
</form>
My jquery codes:
<script>
$(document).ready(function(){
$(document).on("change", ".qty1", function() {
var sum = 0;
$(".qty1").each(function(){
sum += +$('.qty1').val();
});
$(".total").val(sum);
});
});
</script>
At the end, I want to display the cost amount instantly by selecting checkboxes.
you May try something like this
Html Code
<table class="table table-bordered" id="filltable">
<tr>
<td>demo <input type="checkbox" id="chk1" value="10" onchange="add(1,10)"/></td>
<td>demo1 <input type="checkbox" id="chk2" value="10" onchange="add(2,10)" /></td>
<td>demo2 <input type="checkbox" id="chk3" value="10" onchange="add(3,10)"/></td>
<td>demo3 <input type="checkbox" id="chk4" value="10" onchange="add(4,10)"/></td>
</tr>
</table>
and Jquery Code:
var sum = 0;
function add(id,value) {
if ($('#chk' + id).prop('checked') == true) {
sum= sum + value
}
else {
sum= sum - value
}
alert(sum);
}

Responsinve Form which will initially display data from an PHP variable in a Table

my requirement form link
I have made an html form that displays data from an PHP variable. The Form contains a table and that table has number of rows based on a PHP variable.
but problem is that form is not responsive can you please give some idea how can i made a responsive form with table that will load data from an PHP variable. my current code is below:
<div class="row">
<div class="col-md-8">
<form method="POST" action="/updateInfo" enctype="multipart/form-data" name="InfoForm">
<div style ="width:100%; height:250px;">
<table style = "width:100%;border:2px;height:60%;">
<tr style = "border-bottom: thin solid;background-color:#9a004b;color:White">
<th></th>
<th>Register Date</th>
<th>Reconciliation Date</th>
<th>Amount</th>
<th>IBAN</th>
<th>Withhold</th>
</tr>
<?php foreach($overpayment as $key=>$value){
if(empty($value['ReconciliationDate'])){
?> <tr style="border:1px solid #9a004b;background-color: White">
<div class="row">
<td><input type="hidden" name=OverpaymentNr[] value="<?php echo $value['OverpaymentNr']?>"</td>
<td><label name=Paydate[] value=""/><?php echo $value['RegisterDate']?></td>
<td><label name=ReconciliationDate[] value=""/><?php echo $value['ReconciliationDate']?></td>
<td><label name=Amount[] value=""/><?php echo $value['Amount']?></td>
<td><input type="text" style="width:186px; height:35px;" name=IBAN[] value="<?php echo $value['IBAN']?>"/> </td>
<td><label name=Withhold[] value=""/><?php echo $value['Withhold']?></td>
</div>
</tr>
<?php }
}
?>
<tr style="border-top:1px solid #9a004b;">
<td></td>
<td></td>
<td></td>
<td align="center"><input type="Submit" class="btn btn-danger pull-right" name= Submit value = Submit /> </td>
<td></td>
</tr>
</table>
</div>
</form>
</div>
</div>

How to insert values into MySQL database from a dynamic html table?

I have this form in which user can add multiple rows depends on his/her preference. The name of each input tags were generated using jquery; meals1...amount1, meals2...amount2 and so on.
My question is how do I insert all those values into my MySQL database?
So far I have this function:
function dbRowInsert($table_name, $form_data)
{
// retrieve the keys of the array (column titles)
$fields = array_keys($form_data);
// build the query
$sql = "INSERT INTO ".$table_name."
(`".implode('`,`', $fields)."`)
VALUES('".implode("','", $form_data)."')";
// run and return the query result resource
return mysql_query($sql);
}
HTML:
<div id ="ca_meals"><!--start of ca_meals-->
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<h3>Meals Form</h3>
<div class="form-group">
<label for="ca_meals_date">Date:</label>
<input type="text" class="form-control" id="ca_meals_date" name="ca_meals_date" placeholder="Date">
</div>
<div class="form-group">
<label for="ca_meals">Purpose:</label>
<input type="text" class="form-control" id="ca_meals_purpose" placeholder="Purpose">
</div>
<table class="table table-bordered table-hover" id="tab_logic_meals">
<thead>
<tr >
<th class="text-center">
Meals
</th>
<th class="text-center">
Number of PAX
</th>
<th class="text-center">
Alloted Budget Per PAX
</th>
<th class="text-center">
Amount
</th>
</tr>
</thead>
<tbody>
<tr id='meals0'>
<td>
<input type="text" name='ca_accommodate' placeholder='Meals' class="form-control"/>
</td>
<td>
<input type="text" name='ca_meals_numberpax' placeholder='Number of PAX' class="form-control"/>
</td>
<td>
<input type="text" name='ca_meals_budgetpax' placeholder='Alloted Budget Per PAX' class="form-control"/>
</td>
<td>
<input type="text" name='ca_meals_amount' placeholder='Amount' class="form-control"/>
</td>
</tr>
<tr id='meals1'></tr>
</tbody>
</table>
<div class="form-group">
<label for="ca_total">Total:</label>
<input type="text" class="form-control" id="ca_total" disabled>
</div>
</div>
</div>
<a id="add_row_meals" class="btn btn-primary pull-left">Add Row</a><a id='delete_row_meals' class="pull-right btn btn-danger">Delete Row</a>
</div>
</div><!--end of ca_meals-->
This is my JQuery:
$("#add_row_meals").click(function(){
$('#meals'+l).html("<td><input name='ca_meal"+l+"' type='text' placeholder='Meals' class='form-control input-md' /> </td><td><input name='ca_meals_numberpax"+l+"' type='text' placeholder='Number of PAX' class='form-control input-md'></td><td><input name='ca_meals_budgetpax"+l+"' type='text' placeholder='Alloted Budget Per PAX' class='form-control input-md'></td><td><input name='ca_meals_amount"+l+"' type='text' placeholder='Amount' class='form-control input-md'></td>");
$('#tab_logic_meals').append('<tr id="meals'+(l+1)+'"></tr>');
l++;
});
$("#delete_row_meals").click(function(){
if(l>1){
$("#meals"+(l-1)).html('');
l--;
}
});
Usually it's done by passing arrays or indexes with the fields names.
Say, in JavaScript when you add a DHTML field for the name, call it meals[]
In PHP the $_POST['meals'] will contain an array then.
The other way - indexes. Say, when you create a new field with Javascript, give it a new name like meals_1, meals_2, etc. And then loop on them in PHP.
For the first case, with meals[], the POST request will be like:
&meals[]=aaa&meals[]=bbb&meals[]=ccc&amount[]=1&amount[]=2&amount[]=3
An example of PHP code working with meals[] would be:
for($i=0;$i<count($_POST['meals']);$i++)
{
$sql = "INSERT INTO ... SET
`meals` = ". $_POST['meals'][$i].",
`amount` = ". $_POST['amount'][$i].",
// etc
}

How do I submit a Web page with PHP?

I've created a three step order form and I need help with the third step.
1. form is filled out by the business and clicks on "preview order"
2. business views their order and clicks "confirm" (should be on "bizform.php" but I haven't tried it because I have no clue on how to do it.)
3. by clicking "confirm" the "Web page" or data from "bizform.php" is sent to the business and myself.
Questions: Is it possible? If so, can you point me in the right direction? TYVM
My form:
<div class="span4 diff">
<h3>Business Information</h3>
<br/>
<form action="bizform.php" method="post">
<label for="bizName" class="control-label">
Business:</label>
<input type="text" id="bizName" name="bizName" class="input-large">
<label for="bizAddress" class="control-label">
Business Address:</label>
<input type="text" id="bizAddress" name="bizAddress" class="input-large">
<label for="bizCity" class="control-label">
City:</label>
<input type="text" id="bizCity" name="bizCity" class="input-large">
<label for="bizState" class="control-label">
State:</label>
<input type="text" id="bizState" name="bizState" class="input-large">
<label for="bizZip" class="control-label">
Zip code:</label>
<input type="text" id="bizZip" name="bizZip" class="input-large">
<label for="fullName" class="control-label">
Full Name:</label>
<input type="text" id="fullName" name="fullName" class="input-large">
<label for="bizEmail" class="control-label">
E-mail:</label>
<input type="text" id="bizEmail" name="bizEmail" class="input-large">
</div>
<div class="span7">
<h3>Choose Products</h3>
<br/>
<table class="table table-bordered table-striped">
<thead>
<tr class="diffhead">
<th>Product</th>
<th>Price</th>
<th>Training</th>
<th>Total</th>
<th>Qty</th>
</tr>
</thead>
<tbody>
<tr>
<td>Product One</td>
<td>$1000.00</td>
<td>$100.00</td>
<td>$1100.00</td>
<td>
<label class="input" for="productOne"></label>
<input type="text" class="input-mini" value="0" id="productOne" name="productOne">
</td>
</tr>
<tr>
<td>Product Two</td>
<td>$1000.00</td>
<td>$100.00</td>
<td>$1100.00</td>
<td>
<label class="input" for="productTwo"></label>
<input type="text" class="input-mini" value="0" id="productTwo" name="productTwo">
</td>
</tr>
<tr>
<td>Product Three</td>
<td>$1000.00</td>
<td>$100.00</td>
<td>$1100.00</td>
<td>
<label class="input" for="productThree"></label>
<input type="text" class="input-mini" value="0" id="productThree" name="productThree">
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="form-actions">
<button class="btn btn-danger" type="reset" style="float:left">
<i class="icon-remove-sign icon-white"></i> Cancel Order</button>
<button class="btn btn-primary" type="submit" style="float:right">Preview Order
<i class="icon-circle-arrow-right icon-white"></i></button>
</form>
<?php
include '_inc/footer.html';
?>
My form processor:
<?php
include '_inc/header.html';
?>
<?php
$bizName = $_POST['bizName'];
$bizAddress = $_POST['bizAddress'];
$bizCity = $_POST['bizCity'];
$bizState = $_POST['bizState'];
$bizZip = $_POST['bizZip'];
$fullName = $_POST['fullName'];
$bizEmail = $_POST['bizEmail'];
$productOne = $_POST['productOne'];
$productTwo = $_POST['productTwo'];
$productThree = $_POST['productThree'];
$moneyOff = '';
$totalPro = $productOne + $productTwo + $productThree;
define('PRODPRICE', 1100);
$totalMoney = $productOne * PRODPRICE
+ $productTwo * PRODPRICE
+ $productThree * PRODPRICE;
if ( $totalMoney == 2200 )
{
echo '<div class="alert alert-success">Go Back! You can get a product for free!</div>';
}
if ( $totalMoney == 3300 )
{
$moneyOff = 1100;
}
else
{
$moneyOff = 0;
}
define('STOCKFEE', 10);
$stockFee = $productOne * STOCKFEE
+ $productTwo * STOCKFEE
+ $productThree * STOCKFEE;
?>
<h1 align="center">Order Agreement</h1>
<br/>
<?php
echo '<strong>' .$bizName. '</strong> is purchasing the following products and services from CBE:</p>';
echo '<div class="well">
<table cellspacing="0" cellpadding="0" width="800px">
<thead>
<th>Qty</th>
<th>Product</th>
<th>Price</th>
</thead>
<tbody>
<tr>
<td align="center">'. $productOne .'</td>
<td align="center">Product One</td>
<td align="center">$1,100.00</td>
</tr>
<tr>
<td align="center">'. $productTwo .'</td>
<td align="center">Product Two</td>
<td align="center">$1,100.00</td>
</tr>
<tr>
<td align="center">'. $productThree .'</td>
<td align="center">Product Three</td>
<td align="center">$1,100.00</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="right">Sub Total:</td>
<td align="center">'.number_format($totalMoney,2).'</td>
</tr>
<tr>
<td> </td>
<td align="right">Discount:</td>
<td align="center">'.number_format($moneyOff,2).'</td>
</tr>
<tr>
<td> </td>
<td align="right">Grand Total:</td>
<td align="center">'.number_format($totalMoney - $moneyOff,2).'</td>
</tr>
</tbody>
</table>
</div>';
echo '<p>Business agrees to pay <strong> '.number_format($totalMoney - $moneyOff,2).' </strong>for these products and services. In addition, Business will pay an additional <strong> '.number_format($stockFee,2).' </strong>to cover stock fee.</p>';
?>
<div class="row"> </div>
<div class="row">
<div align="left">
<table cellspacing="10" cellpadding="10" width="850px">
<tbody>
<tr><td> </td></tr>
<tr>
<td valign="top">
<?php
echo '<strong>' .$bizName. '</strong> <br/>';
echo $bizAddress.'<br/>';
echo $bizCity.', ';
echo $bizState.' ';
echo $bizZip.'';
?>
</td>
<td valign="top">
<strong>CBE</strong><br/>
Corporate Headquarters<br/>
555 Main Street<br/>
PHPLAND, DB 78987-3849<br/>
888-098-3049
</td>
</tr>
</tbody>
</table>
</div>
</div>
<?php
include '_inc/footer.html';
?>
To store the the data across multiple forms:
<?php
session_start(); // Initiates the session, and loads the _SESSION variable
var_dump($_SESSION); // Show the data in the _SESSION variable
// Load data into the sessions
$_SESSION['a'] = 1;
$_SESSION['b'] = 'hello world';
if (!isset($_SESSION['c'])) $_SESSION['c'] = 0;
else $_SESSION['c'] += 2;
At first, you should see an empty array. Then, refresh and you will see your session data.
http://php.net/manual/en/features.sessions.php - for reference and further education...
Since you can easily access the data via $_POST array, to send all this information to the respective recipients use the mail() function of php to create an email and send it over to whoever you want.
When you submit the form to bizform.php, store you post variables in hidden input fields, and submit those to your form processor on step 3.
EDIT:
In other words, have your Confirm process post your hidden fields to another processor that creates your email content from the POST, and send TO the business, and yourself as a CC or BCC header.
<input name="AID" type="hidden" id="AID" value="<?php echo $_POST['AID']; ?>" size="32" />

Categories