duanli9930 2015-01-03 15:06
浏览 61

使用ajax将会话变量插入数据库

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>";
   }
  • 写回答

1条回答 默认 最新

  • duanou9739 2015-01-03 15:41
    关注

    I have'nt really read all of your details. But reading your question alone, I think you can simply create a session in the server with this pseudocode:

    1. In JavaScript, send session name and session value to the server using Ajax (GET or POST)
    2. In the PHP server, get session name and session value
    3. Create the session using gathered values

    The PHP script would be like:

    <?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']];
       }
    ?>
    

    With this script, the only thing you need to do now is to send it the data it needs to create the session. I think you have that already.

    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗