我有一个动态表,允许用户使用ajax添加/编辑和删除数据库中的记录。该表实际上来自here
它工作正常,但现在我希望能够将会话变量(用户名)与其他记录一起添加到数据库中。对于我所有的搜索和阅读,我只是无法弄清楚如何做...不幸的是我的jquery和javascript知识是非常有限的。我非常感谢任何帮助。
这是script.js的'add'部分,我一直在为表添加一个用户名字段。
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**
这是DbManipulate.php的'save and show'部分,我在表中添加了一个用户名字段。
<?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>";
}
答案 0 :(得分:0)
我真的没有阅读你的所有细节。但是,单独阅读您的问题,我认为您可以使用此伪代码在服务器中创建一个会话:
PHP脚本就像:
<?php
// if the script is called via POST method.
if($_POST) {
$_SESSION[$POST['session_name'] = $_POST['session_value']];
}
// if the script is called via GET method
if($_GET) {
$_SESSION[$GET['session_name'] = $_GET['session_value']];
}
?>
使用此脚本,您现在唯一需要做的就是向其发送创建会话所需的数据。我想你已经有了。