I have a dynamic table that allows users to add/edit and delete records from a database with ajax. The table is actually originally from here
It works fine except now I want to be able to add a session variable (username) to the database along with the other records. For all my searching and reading I just can't figure out how to do it...unfortunately my jquery and javascript knowledge is very limited. I would greatly appreciate any help.
This is the 'add' part of script.js and I have been adding a username field to the table.
var data='';
var action = '';
var savebutton = "<input type='button' class='ajaxsave' value='Save'>";
var updatebutton = "<input type='button' class='ajaxupdate' value='Update'>";
var cancel = "<input type='button' class='ajaxcancel' value='Cancel'>";
var pre_tds;
var field_arr = new Array('text','text','text','text', 'text');
var field_pre_text= new Array('Enter production','Enter channel','Enter role','Enter company', 'username');
var field_name = new Array('production','channel','role','company', 'username');
$(function(){
$.ajax({
url:"DbManipulate.php",
type:"POST",
data:"actionfunction=showData",
cache: false,
success: function(response){
$('#demoajax').html(response);
createInput();
}
});
$('#demoajax').on('click','.ajaxsave',function(){
var production = $("input[name='"+field_name[0]+"']");
var channel = $("input[name='"+field_name[1]+"']");
var role =$("input[name='"+field_name[2]+"']");
var company = $("input[name='"+field_name[3]+"']");
var username = SOMETHING LIKE THIS.....$_SESSION['username'];
if(validate(production,channel,role,company)){
data = "production="+production.val()+"&channel="+channel.val()+"&role="+role.val()+"&company="+company.val()+"&username="+username.val()+"&actionfunction=saveData";
$.ajax({
url:"DbManipulate.php",
type:"POST",
data:data,
cache: false,
success: function(response){
if(response!='error'){
$('#demoajax').html(response);
createInput();
}
}
});
}
else{
return;
}
});**strong text**
And this is the 'save and show' part of DbManipulate.php where I have a added username field to the table.
<?php
include('db.php');
if(isset($_REQUEST['actionfunction']) && $_REQUEST['actionfunction']!=''){
$actionfunction = $_REQUEST['actionfunction'];
call_user_func($actionfunction,$_REQUEST,$con);
}
function saveData($data,$con){
$username = $con->real_escape_string($data['username']);
$production = $con->real_escape_string($data['production']);
$channel = $con->real_escape_string($data['channel']);
$role = $con->real_escape_string($data['role']);
$company = $con->real_escape_string($data['company']);
$sql = "insert into credits (production,channel,role,company, username) values('$production','$channel','$role','$company', '$username')";
if($con->query($sql)){
showData($data,$con);
}
else{
echo "error";
}
}
function showData($data,$con){
$sql = "select * from credits order by id asc";
$data = $con->query($sql);
$str='<tr class="head"><td>production</td><td>channel</td><td>role</td><td>company</td><td></td><td>username</td><td>id</td></tr>';
if($data->num_rows>0){
while( $row = $data->fetch_array(MYSQLI_ASSOC)){
$str.="<tr id='".$row['id']."'><td>".$row['production']."</td><td>".$row['channel']."</td><td>".$row['role']."</td><td>".$row['company']."</td><td><input type='button' class='ajaxedit' value='Edit'/> <input type='button' class='ajaxdelete' value='Delete'></td><td>".$row['username']."</td><td>".$row['id']."</td></tr>";
}
}else{
$str .= "<td colspan='5'>No Data Available</td>";
}