Dynamically generated form does not recognise different rows - php

I'm building a dynamically generated table where every row is a form with some inputs and selects and has its own submit.
Here is my code so far:
<script>
$(function () {
$('form').on('submit', function (e) {
var princ = $('#principal').val();
window.princ = princ;
e.preventDefault();
$.ajax({
url: 'post.php',
method: 'GET',
data: {"princ":princ},
success: function (response) {
alert("success");
} }); }); });
</script>
<table id="assets_table">
<thead style=" cursor: pointer;">
<tr style="text-align: left; color: #fff;">
<th>Lender </th>
<th>Principal </th>
<th>Int. % </th>
<th>Term (mo.) </th>
<th>Collateral </th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$competitions_id = $uscom['id'];
$users_query = mysqli_query($con, "SELECT * FROM competitions WHERE competition='$compe'");
$users_query_r = mysqli_fetch_array($users_query);
foreach($users_query as $users_query_r) {
$usn = $users_query_r['Name'];
$usn_query = mysqli_query($con, "SELECT * FROM users WHERE username='$usn'");
$usn_query_r = mysqli_fetch_array($usn_query);
?>
<form>
<tr>
<td> <img src="<?= $punch_query_r['profile_pic'] ?>" alt="user"> <?= $users_query_r['Name'] ?></td>
<td><input id="principal" type="text" value="0" ></td>
<td><input id="rate" type="text" value="0" ></td>
<td><input id="term" class = "term_input" type="text" value="0" ></td>
<td><input id="collateral" type="text" value="0">
<select id="colselect" >
<option selected disabled>Select asset</option>
</select></td>
<td> <input type="submit"></td>
</tr>
<?php
} ?>
</tbody>
</table>
</form>
However I cannot make it work properly because when I click on submit it does not really submit the corresponding row's form but the first one that was submitted.
I've also tried rewritting variables with input paste keyup but it does not solve the problem either because it just gets the last input whch was filled regardless of the row.
Any idea on how to solve it? Many thanks in advance.

Related

How can I add a jQuery autocomplete handler to a form created dynamically in PHP?

This is the first time I'm asking a question on here. I have a decent amount of experience with PHP, but I'm relatively new to jQuery.
I have a table that I generate via PHP that contains data for Yoga classes. When a user clicks on one of the table rows, the data for that row is sent to an Ajax call to a PHP routine that produces a dialog box. In that dialog box, I want the "teacher" field to have an autocomplete attached to it, so that if the user wants to change the teacher they can start typing and the system will find the current teachers and return close matches.
The dialog box works, but the autocomplete does not. I believe the issue is that the form in the dialog box is generated dynamically, so it doesn't exist at load so the bind fails. I tried changing the jQuery to use the .on("keyup") handler, but that doesn't seem to work either.
How can I get this to work?
jQuery code:
$(function () {
$(document).on("click", "#scheduleTable tbody tr", function(e) {
var pos = { my: "center center", at: "center top+150", of: window };
var rowid = $(this).closest("tr").attr("id");
var rowdata = rowid.substring(3);
var request = $.ajax({
url: "./log_getTemplateClassData.php",
method: "POST",
data: { rowdata:rowdata }
});
request.done(function(response){
$("#edit_template_results").append(response);
$("#edit_template_dialog").dialog("open");
});
request.fail(function(jqXHR, textStatus, errorThrown){
alert(textStatus+': '+errorThrown);
});
request.always(function(){
// do nothing
});
});
$(document).on("keyup", "#teacher", function(){
$("#teacher").autocomplete({
source: "./log_getAllTeachers.php"
});
});
});
$(document).ready(function() {
var pos = { my: "center center", at: "center top+150", of: window };
$(function() {
$("#edit_template_dialog").dialog({
position: pos,
width: "auto",
autoOpen: false,
modal: true,
buttons: {
"Save ": function() {
$( this ).dialog( "close" );
$("#saveForm").val("save");
location.reload();
},
"Cancel ": function(){
$( this ).dialog( "close" );
$("#saveForm").val("cancel");
location.reload();
}
}
});
});
$("#edit_template_dialog").on("dialogclose", function(e){
location.reload();
})
});
The PHP-generated dialog box:
<div id="edit_template_dialog" title="Edit Template Class" class="ui-helper-hidden" >
<form id="edit_template_dialog_form">
<table id="edit_template_results" class="data" align="center">
<tr class="odd">
<th>
<label for="weekDay">Weekday</label>
</th>
<td colspan="2">
<select name="weekDay id="weekDay">
<option selected="selected" value="Sunday">Sunday</option>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
</select>
</td>
</tr>
<tr class="even">
<th>
Class Start Time
</th>
<td colspan="2">
<select name="classHour" id="classHour"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option selected="selected" value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option></select><select name="classMinute" id="classMinute"><option selected="selected" value="00">00</option><option value="15">15</option><option value="30">30</option><option value="45">45</option></select><select name="classAMPM" id="classAMPM"><option value="AM">AM</option><option selected="selected" value="PM">PM</option></select>
</td>
</tr>
<tr class="odd">
<th>
Class Name
</th>
<td colspan="2">
<input class="input3" name="className" type="text" id="className" size="50" value="Yoga Nidra" maxlength="125">
</td>
</tr>
<tr class="even">
<th>
Teacher
</th>
<td colspan="2">
<input class="input3" name="teacher" type="text" id="teacher" size="50" value="Kathi Szabo" maxlength="125">
</td>
</tr>
<tr class="odd">
<th>
Meeting ID
</th>
<td colspan="2">
<input class="input3" name="meetingID" type="text" id="meetingID" size="50" value="942893906123" maxlength="15">
</td>
</tr>
<tr class="even">
<th>
<label for="nationalYN">National Schedule?</label>
</th>
<td colspan="2">
<input type="checkbox" name="nationalYN" id="nationalYN" >
</td>
</tr>
</table>
</form>
</div>
The PHP to do the autocomplete:
<?php
require_once('conn.php');
$searchTerm = $_GET['term'];
$getAllTeachers_query = "SELECT DISTINCT(tplTeacher) FROM tblTemplateClasses WHERE tplTeacher LIKE '%".$searchTerm."%' ORDER BY tplTeacher ASC";
$getAllTeachers_result = $conn->query($getAllTeachers_query);
if ($getAllTeachers_result->num_rows > 0)
{
while ($getAllTeachers_row = $getAllTeachers_result->fetch_assoc())
{
$data['id'] = $getAllTeachers_row['id'];
$data['value'] = $getAllTeachers_row['tplTeacher'];
array_push($teacherList, $data);
}
}
echo json_encode($teacherList);
?>
Thoughts?

Sql INNER JOIN is not working propertly and how do i show uploaded files on the html form using Jquery/Ajax

I want to get results from 2 table using SQL INNER JOIN but it's only showing 1st result not others one. If i select other data then it's showing following error message:
Warning: Invalid argument supplied for foreach() in D:\software installed\xampp\htdocs\contact-management\getContactDetails.php on line 14[]
Description of my functionality:
I have 2 table in my database.
contact_details:
cdid, family_name, given_name, work_phone, mobile_phone, email, email_private, cid
contact_docs:
docid, cdid, file_name, file_des
I'm showing all contacts (only family_name) from database. After select each contacts then it's showing/loading all contact details using Jquery/ajax which is showing on a HTML FORM. So now it's showing contact_details data on the html input type = text field. But I also need show contact_docs data which is actually files.
So when I send a request to the server using jQuery/ajax I've to use SQL inner join query to get the result from both table based on id (cdid). But unfortunately my inner join query is not working properly. It's not showing all contact details data on the html form if I select different contacts. Only showing 1st result.
My questions are:
How do I fix this inner join query?
How do I show the uploaded file on the form (file link would be better) when whole form is load with all data from 2 table ?
Note: I can successfully edit/insert the data to database but issue about showing the data with file.
This is my code:
Html page:
<div id="showContactsDetails">
<h2>Individual Record Details</h2>
<div style=" visibility:hidden;" id="visiable">
<span class="mandatory"><sub>* Required</sub></span>
<!--success update -->
<div id="success"></div>
<form action="" method="post" enctype="multipart/form-data" id="all_contact_details">
<table width="450" border="0" cellspacing="0" cellpadding="0">
<input type="hidden" name="cdid" id="cdid"/>
</tr>
<tr>
<td>Company Name</td>
<td><input type="text" name="company_name" id="company_name" disabled="disabled"/></td>
</tr>
<tr>
<td>Family name</td>
<td><input type="text" name="family_name" id="family_name"/></td>
</tr>
<tr>
<td>Given name</td>
<td><input type="text" name="given_name" id="given_name"/></td>
</tr>
<tr>
<td>Work Phone</td>
<td><input type="text" name="work_phone" id="work_phone"/></td>
</tr>
<tr>
<td>Mobile Phone</td>
<td><input type="text" name="mobile_phone" id="mobile_phone"/></td>
</tr>
<tr>
<td>Email address</td>
<td><input type="text" name="email" id="email"/></td>
</tr>
<tr>
<td>Private email address</td>
<td><input type="text" name="email_private" id="email_private"/></td>
</tr>
<tr>
<td>Upload your document</td>
<td><input type="text" name="file_des_1" id="file_des1" placeholder="short description of your document" class="shor"/><span class="mandatory"><sup>*</sup></span></td>
<tr>
<td></td>
<td align="left"><input name="file1" type="file" id="file" class="file"/></td>
<span class="file_des_1"></span>
</tr>
<tr>
<td></td>
<td><input type="text" name="file_des_2" id="file_des2" placeholder="short description of your document" class="shor"/><span class="mandatory"><sup>*</sup></span></td>
<tr>
<td></td>
<td align="left"><input type="file" name="file2" id="file_2" class="file"/></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="file_des_3" id="file_des3" placeholder="short description of your document" class="shor"/><span class="mandatory"><sup>*</sup></span></td>
<tr>
<td></td>
<td align="left"><input type="file" name="file3" id="file_3" class="file"/></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="file_des_4" id="file_des4" placeholder="short description of your document" class="shor"/><span class="mandatory"><sup>*</sup></span></td>
<tr>
<td></td>
<td align="left"><input type="file" name="file4" id="file_4" class="file"/></td>
</tr>
<tr>
<td> </td>
<td><input type="button" name="submit" value="Update Details" class="submit" id="upload"/></td>
</tr>
</table>
</form>
</div>
</div><!--showContactsDetails-->
jQuery/Ajax page:
//edit the form....................
<script>
$('body').on('click', '#upload', function(e){
e.preventDefault();
var formData = new FormData($(this).parents('form')[0]);
$.ajax({
url: 'editContactDetails.php',
type: 'POST',
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
success: function(data){
$("#success").html(data);
document.getElementById("all_contact_details").reset();
},
data: formData,
cache: false,
contentType: false,
processData: false
});
});
//load the html form with all contact details..................................................
function getDetails(id) {
id = id;
$.ajax({
url: 'getContactDetails.php',
type: 'post',
data: {'id' : id},
dataType : 'json',
success: function( res ) {
document.getElementById("visiable").style.visibility = "visible";
console.log(res);
$.each( res, function( key, value ) {
console.log(key, value);
$('input[type=text][name='+key+']').val(value);
$('input[type=hidden][name='+key+']').val(value);
$('textarea[name='+key+']').val(value);
});
}
});
}
</script>
Php page (getContactDetails.php):
<?php
ob_start();
require_once("config.php");
$id = (int) $_POST['id'];
$sql = mysql_query("SELECT contact_details.cdid, contact_details.family_name, contact_docs.file_name FROM contact_details INNER JOIN contact_docs ON contact_details.cdid = contact_docs.cdid WHERE contact_docs.cdid = '$id'");
$res = mysql_fetch_array($sql);
$data = array();
foreach( $res as $key => $value ) {
$data[$key] = $value;
}
echo json_encode($data);
?>
I'm a new in web development field. So may be my code is wrong that's why your help is greatly appreciated :)
I think you want something like this... (using deprecated API)
<?php
require_once("config.php");
$id = (int) $_GET['id'];
$query = "
"SELECT c.cdid
, c.family_name
, d.file_name
FROM contact_details c
JOIN contact_docs d
ON c.cdid = d.cdid
WHERE d.cdid = $id
";
$result = mysql_query($query);
$data = array();
while ($res = mysql_fetch_array($result)){
foreach( $res as $key => $value ) {
$data[$key][] = $value;
}
}
echo json_encode($data);
?>

Need HTML Form to Submit Multiple Rows into SQL Database

So I just got done with figuring out some javascript issues with my form in this topic: How Can I Insert Multiple Rows Into a DB from my HTML Form with Multiple Rows Dynamically?
But since I had such a multi-part question I have been asked to create a new topic. Below is my new code that I am using where I am stuck and wanting to be able to submit my form, which has the capability if adding multiple rows, to my database and add those HTML rows to rows in the SQL database, respectively.
PREVIEW: http://cl.ly/image/0K0Z202O1Q3e/Screen%20Shot%202013-03-14%20at%203.00.19%20PM.png
HTML
<html>
<header>
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript" language="javascript" src="/jquery/js/jquery-1.9.1.js">
</script>
<script src="http://www.mapquestapi.com/sdk/js/v7.0.s/mqa.toolkit.js?
key=Gmjtd%7Cluua2q6bn9%2C8g%3Do5-lzbsh"></script>
<script type="text/javascript" src="js/scripts.js"></script>
<title>Central Office Company Survey</title>
</header>
<body onload="get_company_name();">
<h1>Central Office Company Survey</h1>
<div id='map' style='width:0px; height:0px; position:absolute'></div>
<input type="hidden" id="co_city" name="co_city">
<input type="hidden" id="co_state" name="co_state">
<input type="hidden" id="co_zipcode" name="co_zipcode">
<table>
<th>Company</th>
<th>CO Name</th>
<th>Get Current Location</th>
<th>Lat</th>
<th>Long</th>
<th>Address</th>
<tr>
<td><select id="company_name" name="company_name" /></select></td>
<td><input name="co_name" type="text"></td>
<td><input type="submit" value="Get GPS" onclick="gpslookup();" /></td>
<td><input id="co_lat" name="co_lat" type="text" /></td>
<td><input id="co_long" name="co_long" type="text" /></td>
<td><input id="co_address" name="co_address" type="text" /></td>
</tr>
</table>
<table id="tabledata">
<thead>
<th>Select</th>
<th>Border Location Name</th>
<th>Cable Location</th>
<th>Direction of Vault Wall</th>
<th>Cable Type</th>
<th>Cable Size (pairs)</th>
<th>Cable Gauge</th>
<th>Vertical(s) appeared on Verticals</th>
<th>Approximate Number of Jumpers</th>
<th>Are Protectors Still In?</th>
<th>Metered Distance</th>
<th class="comments">Central Office Comments</th>
</thead>
<tbody id="input"></tbody>
<tbody id="template">
<tr>
<td><input type="checkbox" /></td>
<td><input name="border_location" type="text" /></td>
<td><input name="cable_location" type="text" /></td>
<td><input name="vault_direction" type="text" /></td>
<td><input name="cable_type" type="text" /></td>
<td><input name="cable_size" type="text" /></td>
<td><input name="cable_gauge" type="text" /></td>
<td><input name="vertical" type="text" /></td>
<td><input name="jumpers" type="text" /></td>
<td><input name="protectors" type="text" /></td>
<td><input name="metered_dist" type="text" /></td>
<td><input name="comments" type="text" /></td>
</tr>
</tbody>
</table>
<button id="ActionAddRow">Add Row</button>
<button onclick="deleteRow(); return false;">Delete Row</button>
<button id="ActionSubmit">Submit</button>
</body>
</html>
scripts.js
//Button Functions
$(function () {
var addInputRow = function () {
$('#input').append($('#template').html());
};
addInputRow();
$('#ActionAddRow').on('click', addInputRow);
$('#ActionSubmit').on('click', function () {
var data = $('#input tr').map(function () {
var values = {};
$('input', $(this)).each(function () {
values[this.name] = this.value;
});
return values;
}).get();
$.post('./php/upload_survey.php', {
json: JSON.stringify(data),
delay: 1
}).done(function (response) {
alert("Thank you. Your form has been submitted.");
console.log(response);
});
});
});
//Delete Selected Rows
function deleteRow() {
try {
var table = document.getElementById("tabledata");
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 3) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}
catch(e) {
alert(e);
}
};
upload_survey.php
//Assign passed parameters
$values = json_decode($_POST['json']);
$stringLogInfo = "INFO: Location: $co_address CO Name = $co_name !!!\n\n";
log_audit($logAuditFile, $logModule, $stringLogInfo);
//Parse and store the ini file, this will return an associative array
ini_set("display_errors", "1");
error_reporting(E_ALL);
//Insert Survey Form Information into the database
$sql="INSERT INTO table_name (company_name,...,...)
VALUES ($values)";
mysql_query($sql) or die ("Unable to Make Query:" . mysql_error());
**So far every time I try to get this working the JS fires successfully, but I get an error in Chrome's Developer Toolbox: Unable to Make Query:Column count doesn't match value count at row 1
This refers to this function in js file: console.log(response);
I think there is no such thing as batch inserts in PHP. There is no equivalent for JAVA's addBatch & executeBatch.
So the right way to do it is simply iteratation over array and insert single row with prepared statement.

Placement issues in adding input rows dynamically using jQuery

Here is the script to add dynamic rows. It works completely fine with only one error. When I click on Add link, it adds the new row. However, the initial row (only select input tag) comes below, and the new row while other remains at there only :(
Please see the HTML code, also below.
Please see this image for more description:
<script type="text/javascript" src="http://cachefile.net/scripts/jquery/1.2.3/jquery-1.2.3.min.js"></script>
<script type="text/javascript">
$(function(){ var newRowNum = 0;
$('#addnew').click(function(){
newRowNum += 1;
var addRow = $(this).parent().parent();
var newRow = addRow.clone();
$('input', addRow).val('');
$('td:first-child', newRow).html(newRowNum);
$(':input', newRow).each(function(i){
var newID = newRowNum + '_' + i;
$(this).attr('id',newID).attr('name',newID);
});
addRow.before(newRow);
$('a.remove', newRow).click(function(){
$(this).parent().parent().remove();
newRowNum -= 1;
return false;
});
$('#go').click(function(){
var numRows=$('#tabdata tbody tr').length;
$('#myHiddenInput').val(numRows);
});
return false; }); });
</script>
Here is the HTML part
<form action="issue-item.php" method="POST">
<fieldset style="width: 1714px;">
<legend style="font-family: fantasy; font-style: !important; color: teal; font-size:30px ;">Issue</legend><br /><br />
<table id="tabdata" align="left" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<td>S.No.</td>
<td align="center">Date</td>
<td align="center">Name of Person</td>
<td align="center">Name Of Item</td>
<td align="center">Quantity</td>
<td align="center">Purpose</td>
</tr>
</thead>
<tbody>
<tr>
<td><a id="addnew" href="">Add</a></td>
<td><input type="text" name="0_0" style="width: 120px;"/></td>
<td><input type="text" name="0_1" style="width: 170px;"/></td>
<td>
<select name="0_2" size="1">
<option>Choose item</option>
<?php
require('blogic.php');
$obj = new blogic();
$result = $obj->select("select id,name_item from store_item");
$count=$obj->select("SELECT COUNT(*) FROM store_item");
$obj->rows = mysql_num_rows($count);
while($objres = mysql_fetch_row($result))
{
$str=<<<here
<option value="$objres[0]">$objres[1]</option>
here;
echo $str;
}
?>
</select>
</td>
<td><input type="text" name="0_3" style="width: 80px;"/></td>
<td><input type="text" name="0_4" style="width: 280px;"/></td>
</tr>
<tr>
<td>
<input type="hidden" id="myHiddenInput" name="myHiddenInput" value="1"/>
</td>
</tr>
<tr><td><input id="go" name="go" type="submit" /></td></tr>
</tbody>
</table>
</fieldset>
</form>
Add following lines after $('input', addRow).val('');:
$('select', newRow).val($('select', addRow).val());
$('select', addRow).val('');
Also see my example.
=== UPDATE ===
Or replace $('input', addRow).val(''); with:
$('select', newRow).val($('select', addRow).val());
$(':input', addRow).val('');
Also see my updated example.

Save value of check-box with in 5 columns to database

How to Save value of check-box within 5 columns to database (PHP and JQUERY) ?
PHP code are dispay 6 columns. there 5 columns contain check-box that loop from database.
---PHP-----------------------------------
<table id="tblISP" border="0">
<tr>
<th style="width:20px">
</th>
<th>
MenuID
</th>
<th>
Menu
</th>
<th>
Delete
</th>
<th>
Upddate
</th>
<th>
View
</th>
<th>
Save
</th>
</tr>
<tr>
<td>ALL <input type="checkbox" class="menuiddd" name="checkmenuid" id="checkmenuid"/></td>
<td> </td>
<td> </td>
<td>ALL<input type="checkbox" class="menuidddel" name="checkdelete" id="checkdelete"/></td>
<td>ALL<input type="checkbox" class="menuiddupdate" name="checkupdate" id="checkupdate"/></td>
<td>ALL<input type="checkbox" class="menuiddview" name="checkview" id="checkview"/></td>
<td>ALL<input type="checkbox" class="menuiddsave" name="checksave" id="checksave"/></td>
</tr>
<tr>
<td>
<input type="checkbox" class="menuiddd" name="idd[]" id="idd[]" value="<?php echo $row["mid"]; ?>"/>
</td>
<td>
<?php echo $row["mid"]; ?>
</td>
<td>
<?php echo $row["mname"]; ?>
</td>
<td>
<input type="checkbox" class="menuidddel" name="del[]" id="del[]" value="chkdel" />
</td>
<td>
<input type="checkbox" class="menuiddupdate" name="upd[]" id="upd[]" value="chkupd" />
</td>
<td>
<input type="checkbox" class="menuiddview" name="vie[]" id="vie[]" value="chkvie" />
</td>
<td>
<input type="checkbox" class="menuiddsave" name="sav[]" id="sav[]" value="chksav" />
</td>
</tr>
<tr>
<td colspan="3">
<!--<a id="hlsa" href="javascript:exit(0);">Select All</a>
<a id="hldsa" href="javascript:exit(0);">DeSelect All</a>-->
</td>
</tr>
<tr>
<td colspan="3" align="center">
<hr/>
<input type="button" value="Save" id="btSave" name="btSave" style="cursor:pointer;float:none" class="allbutton">
</td>
</tr>
<tr>
<td colspan="3" id="dvProc1" align="center">
</td>
</tr>
</table>Save check-box value within 5 columns to database
------------------Jquery------------
Jquery reaction when click button save then get information from check box save to database.
$(document).ready(function (){
$("#btSave").unbind();
$("#btSave").click(function(){
var idd = [];
var del = [];
var upd = [];
var vie = [];
var sav = [];
$("input[name='idd[]']:checked").each(function() {
idd.push($(this).val());
});
$("input[name='del[]']:checked").each(function() {
del.push($(this).val());
});
$("input[name='upd[]']:checked").each(function() {
upd.push($(this).val());
});
$("input[name='vie[]']:checked").each(function() {
vie.push($(this).val());
});
$("input[name='sav[]']:checked").each(function() {
sav.push($(this).val());
});
var mainarray=[idd,del,upd,vie,sav];
var transposed = mainarray.transpose();
//-----------------------alert(str);--note--need return-------------
alert(transposed.join(';'));
})
$("#checkmenuid").click(function(){
$('.menuiddd').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
});
$("#checkdelete").click(function(){
$('.menuidddel').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
});
$("#checkupdate").click(function(){
$('.menuiddupdate').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
});
$("#checkview").click(function(){
$('.menuiddview').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
});
$("#checksave").click(function(){
$('.menuiddsave').attr('checked', $( this ).is( ':checked' ) ? 'checked' : "");
});
});
</script>
I'm not entirely sure I've identified your question, but before you deal with any jQuery you should build a functioning system without any javascript. Rather than defining a non-standard method for dealing with the form's submission, you can simply write it normally before adding a clean layer of javascript to handle asynchronous submission.
Firstly, you need to wrap your input elements in a form element, and then write a PHP script to analyse the global $_POST array and to execute the various database commands as required. You can use this PHP layer for the logic of identifying what calls should be made to the database, based on what the user has selected on the form. Note that checkboxes that are unchecked are not actually sent in the POST request, so you'll have to use isset() in PHP to know if they were unchecked.
Once that's arranged, you can use jQuery to submit the form asynchronously, and you won't cause a page refresh:
$(function() {
$('form#yourformid').submit(function(event) {
event.preventDefault(); // stop the page from redirecting
// serialise your form's data, and send it in a POST request
// to the form's action attribute
$.post(
$(this).attr('action'),
$(this).serialize(),
function() {
alert('Data successfully saved.');
});
});
});

Categories