By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

Ajax in PHP

P: 18
Hi all,
I am having a page where there is a drop down box for projects.Below that i had a list of users with check boxes. so wen i select a project in the drop down and assign some users to that project by checking on checkbox.
so here is my problem. wen i select the same project from drop down i should see the users who are assigned to that project as checked. so wen i select another project then users assigned to that project should be checked.

on change of the project i need to get the users who are assigned to that project as checked and others as unchecked.

I came to know that this can be done by Ajax. so can any one help me please....


Below is the code i habe written.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start();
  3. @session_start();
  4. require_once ("check.php");
  5. createsessions($username,$password,$userid,$projectid,$projectname,$filename,$size,$allocatedmemory,$answer,$usedmemory,$Remainingmemory,$result,$data3);
  6.     include 'connection.php';
  7.     $sql="SELECT projectname from projects where createdby='".mysql_real_escape_string($_SESSION['username'])."'";
  8.     mysql_error();
  9.     $result=mysql_query($sql);
  10.     $options=""; 
  11.     while ($row=mysql_fetch_array($result)) 
  12.     {
  13.         $id=$row["projectid"];
  14.         $projectname=$row["projectname"];
  15.         $options.="<option value=\"$projectname\">".$projectname.'</option>';
  16.     }
  17.     $num=mysql_num_rows($result);
  18.     $sql2="SELECT * FROM users where reportingto='".mysql_real_escape_string($_SESSION['username'])."' and role='2'";
  19.     mysql_error();
  20.     $result2=mysql_query($sql2);
  21.     $num=mysql_num_rows($result2); 
  22.     $sql3="select userid from projectassign where projectassign='$projectassign'";
  23.      mysql_error();
  24.     $result3=mysql_query($sql3);
  25.     $num1=mysql_num_rows($result3);
  26.     $i=0;
  27.     while ($i < $num1)
  28.     {
  29.         $userid=mysql_result($result3,$i,"userid");
  30.         echo $userid;  // the userid's who are assigned to the project selected in drop down wil be printed here. Now i need those userid's check box should be checked.
  31.         $i++; 
  32.     }
  33.  
  34.     ?>
  35.  
  36. <head>
  37.  
  38. <link rel="stylesheet" type="text/css" href="style.css"/> 
  39. <script language="javascript" type="text/javascript" src="back.js" ></script>
  40.  <body onLoad="backButtonOverride()">
  41. <script language="javascript" type="text/javascript">
  42.         function check()
  43.         {
  44.  
  45.             if(document.getElementById('prolist').selectedIndex == 0)
  46.             {
  47.                 alert('Please select a Project');
  48.                 return false;
  49.  
  50.             }
  51.            /* var txt=document.getElementsByName("checkbox[]");
  52.           var chk=false;
  53.            for(i=0;i<txt.length;i++)
  54.            {
  55.              if(txt[i].checked==true)
  56.              {
  57.                  chk=true;
  58.              }
  59.            }
  60.            if(chk==false)
  61.            {
  62.            alert("please select the checkbox.");
  63.            return false;
  64.            } */                   
  65.  
  66.         }
  67.         </script>                                 
  68. <form action="projectassign.php" method="post">
  69. <table width="100%">
  70. <tr><td> <img src="Logofinalcopy.gif">
  71. </td></tr></table><br><hr style="color: red;">
  72. <table align="right"> 
  73.     <tr>
  74.         <td align="right" style="width: 50px">   
  75.             <td align="right" style="color: navy;"><strong><?php echo "Welcome ".$_SESSION[username];?>,</strong></td><td><a href="logout.php">Logout</a></td>
  76.         </td>
  77.         </tr></table>
  78.         <table><tr>
  79.         <td><a href="Admin.php">Home</a></td>
  80.         <td>&nbsp&nbsp&nbsp&nbsp 
  81.         <a href="Projectmgmt.php">Projects</a></td>
  82.         <td>&nbsp&nbsp&nbsp&nbsp<a href="projectassign.php">Project Assign</a></td>
  83.         </tr>
  84.         </table>  
  85.  
  86.     <table align="center"><td style="color: navy;"><h2>Project assign</h2></td>
  87.     </tr></table>
  88.     <table align="center">
  89.     <tr>
  90.         <td style="color: navy;">Projectassign</td>
  91.         <td><select id="prolist" name="projectassign">
  92.         <option  value="<?php echo '$projectassign';?>" >----select project----</option><?php echo $options ?> 
  93.         </select>
  94.         </td>
  95.     </tr>
  96.     </table>  
  97.     <br><br><br>  
  98.     <table border="1" cellspacing="0" cellpadding="0" align="center" style="width: 500px" bordercolor="red">
  99.     <tr>
  100.         <th></th>
  101.         <th style="color: navy;">Userid</th>
  102.         <th style="color: navy;">Username</th>
  103.  
  104.     </tr>
  105.     <?php
  106.     $sno=1;
  107.     while($row=mysql_fetch_array($result2, MYSQL_ASSOC)){
  108.     ?>
  109.      <tr>
  110. <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['userid']; ?>"></td>
  111. <td><? echo $row['userid']; ?></td>
  112. <td><? echo $row['username'];?></td>
  113. </tr>
  114. <?php
  115.     $sno=$sno+1;
  116. }
  117. ?>
  118.     </table>
  119.  
  120.     <?php
  121.  
  122. if(isset($_POST['Submit']))
  123.  
  124.     {  
  125.         $checkbox=$_POST['checkbox'];  
  126.         for($i=0;$i<count($checkbox);$i++)
  127.         {
  128.         $del_id = mysql_real_escape_string($checkbox[$i]); 
  129.         $query="select projectassign from projectassign where projectassign='$_POST[projectassign]' and userid='$del_id' ";
  130.         mysql_error();  
  131.         $result=mysql_query($query);
  132.         $ans=mysql_num_rows($result);        
  133.         if($ans==0)
  134.         {        
  135.             $sql3 = "INSERT INTO projectassign(projectassign,userid)values('".mysql_real_escape_string($_POST['projectassign'])."','$del_id')";
  136.             mysql_error();
  137.             $result = mysql_query($sql3);
  138.             echo "<script language='javascript'>
  139.             alert('Project assigned');
  140.             </script>";
  141.         }
  142.         else{
  143.             echo "<script language='javascript'>
  144.             alert('Project already assigned');
  145.             </script>";
  146.         }
  147.  
  148.        }
  149.  
  150.     }
  151.  
  152.     ?> 
  153.     <br><table align="center"><tr><td><input type="submit" name="Submit"  value="Submit" onclick="return check();"></td></tr></table> 
  154.     </form>
  155.     </body>
  156.     </head>
Jun 7 '10 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
Hey.

To do that you would have to use JavaScript. Use the onchange event of your <select> box to execute a JavaScript function. Have the function send the selected option to a PHP script, using AJAX, which should return a list of names assigned to that option. Then use JavaScript to manipulate the DOM to check/uncheck the appropriate boxes according to that list.

If you are new to AJAX, start by going through a basic tutorial. AJAX is simple enough to learn in a matter of hours (if not minutes), but it requires you to know JavaScript as well. -- AJAX is just an object, used in JavaScript code.
Jun 7 '10 #2

100+
P: 1,059
I Completely agree with Atli.

Please follow the link(i got it on google searching). Simplest and very much useful example
simple ajax example

only instead of cgi use your php script. Another issue Ajax is completely client side scripting nothing to do with php. Whatever request a server get it will process. It can be from Ajax or even if you design a desktop application with c/java, PHP will reply same.

best regards,
johny
Jun 8 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.