Ajax in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • niths
    New Member
    • Mar 2010
    • 18

    Ajax in PHP

    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.
    Code:
    <?php
    ob_start();
    @session_start();
    require_once ("check.php");
    createsessions($username,$password,$userid,$projectid,$projectname,$filename,$size,$allocatedmemory,$answer,$usedmemory,$Remainingmemory,$result,$data3);
        include 'connection.php';
        $sql="SELECT projectname from projects where createdby='".mysql_real_escape_string($_SESSION['username'])."'";
        mysql_error();
        $result=mysql_query($sql);
        $options=""; 
        while ($row=mysql_fetch_array($result)) 
        {
            $id=$row["projectid"];
            $projectname=$row["projectname"];
            $options.="<option value=\"$projectname\">".$projectname.'</option>';
        }
        $num=mysql_num_rows($result);
        $sql2="SELECT * FROM users where reportingto='".mysql_real_escape_string($_SESSION['username'])."' and role='2'";
        mysql_error();
        $result2=mysql_query($sql2);
        $num=mysql_num_rows($result2); 
        $sql3="select userid from projectassign where projectassign='$projectassign'";
         mysql_error();
        $result3=mysql_query($sql3);
        $num1=mysql_num_rows($result3);
        $i=0;
        while ($i < $num1)
        {
            $userid=mysql_result($result3,$i,"userid");
            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.
            $i++; 
        }
    
        ?>
         
    <head>
    
    <link rel="stylesheet" type="text/css" href="style.css"/> 
    <script language="javascript" type="text/javascript" src="back.js" ></script>
     <body onLoad="backButtonOverride()">
    <script language="javascript" type="text/javascript">
    		function check()
    		{
                
    			if(document.getElementById('prolist').selectedIndex == 0)
                {
                    alert('Please select a Project');
                    return false;
                    
                }
               /* var txt=document.getElementsByName("checkbox[]");
              var chk=false;
               for(i=0;i<txt.length;i++)
               {
                 if(txt[i].checked==true)
                 {
                     chk=true;
                 }
               }
               if(chk==false)
               {
               alert("please select the checkbox.");
               return false;
               } */                   
                
    		}
    		</script>                                 
    <form action="projectassign.php" method="post">
    <table width="100%">
    <tr><td> <img src="Logofinalcopy.gif">
    </td></tr></table><br><hr style="color: red;">
    <table align="right"> 
        <tr>
            <td align="right" style="width: 50px">   
                <td align="right" style="color: navy;"><strong><?php echo "Welcome ".$_SESSION[username];?>,</strong></td><td><a href="logout.php">Logout</a></td>
            </td>
            </tr></table>
            <table><tr>
            <td><a href="Admin.php">Home</a></td>
            <td>&nbsp&nbsp&nbsp&nbsp 
            <a href="Projectmgmt.php">Projects</a></td>
            <td>&nbsp&nbsp&nbsp&nbsp<a href="projectassign.php">Project Assign</a></td>
            </tr>
            </table>  
    
        <table align="center"><td style="color: navy;"><h2>Project assign</h2></td>
        </tr></table>
        <table align="center">
        <tr>
            <td style="color: navy;">Projectassign</td>
            <td><select id="prolist" name="projectassign">
            <option  value="<?php echo '$projectassign';?>" >----select project----</option><?php echo $options ?> 
            </select>
            </td>
        </tr>
        </table>  
        <br><br><br>  
        <table border="1" cellspacing="0" cellpadding="0" align="center" style="width: 500px" bordercolor="red">
        <tr>
            <th></th>
            <th style="color: navy;">Userid</th>
            <th style="color: navy;">Username</th>
               
        </tr>
        <?php
        $sno=1;
        while($row=mysql_fetch_array($result2, MYSQL_ASSOC)){
        ?>
         <tr>
    <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['userid']; ?>"></td>
    <td><? echo $row['userid']; ?></td>
    <td><? echo $row['username'];?></td>
    </tr>
    <?php
        $sno=$sno+1;
    }
    ?>
        </table>
        
        <?php
       
    if(isset($_POST['Submit']))
        
        {  
            $checkbox=$_POST['checkbox'];  
            for($i=0;$i<count($checkbox);$i++)
            {
            $del_id = mysql_real_escape_string($checkbox[$i]); 
            $query="select projectassign from projectassign where projectassign='$_POST[projectassign]' and userid='$del_id' ";
            mysql_error();  
            $result=mysql_query($query);
            $ans=mysql_num_rows($result);        
            if($ans==0)
            {        
                $sql3 = "INSERT INTO projectassign(projectassign,userid)values('".mysql_real_escape_string($_POST['projectassign'])."','$del_id')";
                mysql_error();
                $result = mysql_query($sql3);
                echo "<script language='javascript'>
                alert('Project assigned');
                </script>";
            }
            else{
                echo "<script language='javascript'>
                alert('Project already assigned');
                </script>";
            }
            
           }
            
        }
                               
        ?> 
        <br><table align="center"><tr><td><input type="submit" name="Submit"  value="Submit" onclick="return check();"></td></tr></table> 
        </form>
        </body>
        </head>
    Last edited by Niheel; Jun 8 '10, 07:57 AM. Reason: it is better to show your code in the question, so experts can see it in one place
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      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

      Comment

      Working...