-1

i have a dynamic table that the enables to perform CRUD operations on a database. after logging in the user is directed to index.php, here a table is displayed with values stored in the database table "ajaxtable". i have joined "ajaxtable" table and "members" table using a foreign key. How can i modify my code further so that the js file can get the email of the user from session variable instead of posting it from an input field in the dynamic table. my index.php:

<?php
session_start();
$fname=$_SESSION['mail'];
?>


<html>
  <title>Addressbook</title>
<head>
 <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js">    </script>
<link rel="stylesheet" type="text/css" href="crudstyle.css" />

</head>
<body bg color="">
<div id="mhead"><h2>Your Adressbook</h2></div>
 <div id="note"> <span> your addressbook is connected to our servers :) </span></div>
   <?php
   echo $fname; // it echoes the login email correctly here
   ?>
<table id='demoajax' cellspacing="0">
</table>
<script type="text/javascript" src="script.js"></script>

 </body>
</html>

A function in my script.js file:

    $('#demoajax').on('click','.ajaxsave',function(){

   var fname =  $("input[name='"+field_name[0]+"']");

// i want this field to be obtained from the session variable

   var lname = $("input[name='"+field_name[1]+"']");
   var domain =$("input[name='"+field_name[2]+"']");
   var email = $("input[name='"+field_name[3]+"']");
   if(validate(fname,lname,domain,email)){
   data = "fname="+fname.val()+"&lname="+lname.val()+"&domain="+domain.val()+"&email="+email.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;
  }   
   });

part of my DbManipulate.php file:

  function saveData($data,$con){
  $fname = $con->real_escape_string($data['fname']);

//the above field should also be obtained from session variable during this operation too

 $lname = $con->real_escape_string($data['lname']);
   $domain = $con->real_escape_string($data['domain']);
   $email = $con->real_escape_string($data['email']);
   $sql = "insert into ajaxtable(firstname,lastname,domain,email) values('$fname','$lname','$domain','$email')";
   if($con->query($sql)){
   showData($data,$con);
  }
else{
 echo "error";
 }

 } 
function showData($data,$con){
   $sql = "select * from ajaxtable JOIN member ON member.email=ajaxtable.firstname WHERE member.email=$fname";
   $data = $con->query($sql);
   $str='<tr class="head"><td>user email</td><td>contact name</td><td>contact number</td><td>contact Email</td><td></td></tr>';
   if($data->num_rows>0){
   while( $row = $data->fetch_array(MYSQLI_ASSOC)){
    $str.="<tr id='".$row['id']."'><td>".$row['firstname']."</td><td>".$row['lastname']."</td><td>".$row['domain']."</td><td>".$row['email']."</td><td>    
  <input type='button' class='ajaxedit' value='Edit'/> <input type='button' class='ajaxdelete' value='Delete'></td></tr>";
  } 
   }else{
    $str .= "<td colspan='5'>No Data Available</td>";
  }

echo $str;   
  }
Akash
  • 220
  • 3
  • 12
  • you can rename your `script.js` to `script.js.php` and load that. ``. Then, you can use `var fname = ''` if i understood your question – vaso123 Oct 28 '14 at 10:54
  • is that a valid extension? there are few changes required in all 3 files i want you to suggest those. – Akash Oct 28 '14 at 10:57
  • the "fname" should not even be visible to the user to be able to make any changes. it should post the email stored in the session variable "mail" by itself. in my current code its a user editable field. – Akash Oct 28 '14 at 10:59
  • no, it's not recommended to use, but this is the laziest way. use ajax, if you do not want to show it. you can check some techniques here: http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript – vaso123 Oct 28 '14 at 11:05

1 Answers1

0

I would suggest to refactor your code a little bit. If i were you, i would not create table in your function showData($data,$con); Instead, i would return result as jSON and then in javascript you will have access to it as an object. So,

function getData($data, $conn)
    {
         $sql = "select * from ajaxtable JOIN member ON member.email=ajaxtable.firstname WHERE member.email=$fname";
         $data = $con->query($sql);             
         if($data->num_rows>0){
              while( $row = $data->fetch_array(MYSQLI_ASSOC)){
                 $result[] = $row;
          }
          return json_encode($result);
      }

In your javascript:

$.getJSON("YOUR_URL", {
        dir: dir,
        tagmode: "any",
        format: "json"
    }).done(function (data)
    {
        var items = [];
        $.each(data.items, function (key, val)
        {
          console.log(val); // here you can see in console values of val and create table rows with those values
          var tmp_item = "" HERE CREATE YOUR TABLE ROWS val.id, val.first_name
            items.push(tmp_item);...
         }

Once you get all data you need in data.items object you will be able to easy append them on page with jQuery (append , appendTo...)

Hope that helps.

Vladd
  • 124
  • 5