Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post change link text after click

    I want to change the text after i click using ajax

    get the response text from ajax file and paste instead of the link

    Also the div id in while loop is given in document.getElementById
    PHP Code:
    <?php 
        
    require 'connect.php';
    ?>
    <html>
    <head><title>Admin</title>
    <link rel="stylesheet" type="text/css" href="tab_style.css">
    <script type="text/javascript">
    function getXMLHTTP() {
            var xmlhttp=false;    
            try{
                xmlhttp=new XMLHttpRequest();
            }
            catch(e)    {        
                try{            
                    xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e){
                    try{
                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                    }
                    catch(e1){
                        xmlhttp=false;
                    }
                }
            }
                 
            return xmlhttp;
        }
        
    /*function approve(e) {
        var req = getXMLHTTP();
        if (req) {
                
                req.onreadystatechange = function() {
                    if (req.readyState == 4) {
                        // only if "OK"
                        if (req.status == 200) {
                            var getajax = req.responseText;
                            <!--document.myform.linkloc.value = req.responseText;-->
                            document.getElementById("getajax").innerHTML=req.responseText;
                        } else {
                            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                        }
                    }                
                }            
                req.open("GET", "approve.php?stu="+e, true);
                req.send(null);
            }        
        }*/

    function block(b) {
        var req = getXMLHTTP();
        if (req) {
                
                req.onreadystatechange = function() {
                    if (req.readyState == 4) {
                        // only if "OK"
                        if (req.status == 200) {                        
                            document.getElementById("blk"+b).innerHTML=req.responseText;                    
                        } else {
                            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                        }
                    }                
                }            
                req.open("GET", "block.php?blo="+b, true);
                req.send(null);
            }        
        }
    </script>
    <script type="text/javascript">
                    function approve(e) {
                            var req = getXMLHTTP();
                            if (req) {
                                    
                                    req.onreadystatechange = function() {
                                        if (req.readyState == 4) {
                                            // only if "OK"
                                            if (req.status == 200) {
                                                
                                                <!--document.myform.linkloc.value = req.responseText;-->
                                                document.getElementById("get"+e).innerHTML=req.responseText;
                                            } else {
                                                alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                                            }
                                        }                
                                    }            
                                    req.open("GET", "approve.php?stu="+e, true);
                                    req.send(null);
                                }        
                            }
                        </script>
    </head>
    <body>
    <?php
        $admin_err
    ='';
        
    $pass_err='';
    if (isset (
    $_REQUEST['submit'])) {
        require 
    'connect.php';

        
    $admin $_REQUEST['admin'];
        
    $pass $_REQUEST['password'];
        
    $err=0;
        
        if(
    $admin=="") {
              
    $admin_err="enter username";
              
    $err=1;
        }
        if(
    $pass=="") {
              
    $pass_err="enter password";
              
    $err=1;
        }
        if (
    $err == 0) {
            
            
    $check mysql_query("SELECT admin FROM admin_login WHERE admin='".$admin."' and password='".$pass."' ");
            
    $exi mysql_num_rows ($check);

            if(
    $exi == 0$err_msg "Username/password combination incorrect.";
            else {
                
    $sel mysql_query("SELECT * FROM stud");
                echo 
    "<h4 class='table-caption'>Students</h4>";
                while (
    $fet mysql_fetch_array($sel)) {        
                    
    ?>
                    <div class="base-layer">
                    <div class="table-row">
                    
                        <div class="c_one"><p class="text"> <? echo $fet['name']; ?></p></div>
                        
                        <div class="c_two"><p class="text"> <? echo $fet['email']; ?></p></div>
                        
                        <div class="c_three"><p class="text"> <? echo $fet['school']; ?></p></div>
                        
                        <div class="c_four"><p class="text"> <? echo $fet['subject']; ?></p></div>
                        
                        <div class="c_five"><p class="text"> <? echo $fet['class']; ?></p></div>
                        
                        <div class="c_six"><p class="text"> <? echo $fet['teacher']; ?></p></div>
                    
                        <div class="c_seven" id="get<?php echo $fet['s_id']; ?>"><p class="text">
                        
                        <?php if ($fet['approve'] == '1' ) {
                                echo 
    "Approved";
                        }
                              elseif(
    $fet['approve'] == NULL || $fet['approve'] == '0' ) {
                        
    ?>                    
                        <a href="#" onClick="approve(<?php echo $fet['s_id']; ?>)" 
                        style="text-decoration:none">    
                        Approve</a>
                        <?php ?>    
                        </p>
                        </div>
                        
                        <div class="c_eight" id="blk<?php echo $fet['s_id']; ?>"><p class="text">
                        <?php if ($fet['approve'] == '0' ) {
                                echo 
    "Blocked"
                        }
                                elseif(
    $fet['approve'] == '1') {
                        
    ?>
                        <a href="#" onClick="block(<?php echo $fet['s_id']; ?>);" style="text-decoration:none">
                        Block
                        </a>
                        <?php ?>
                        </p>
                        </div>
                        <div class="space-line"> </div>
                    </div>
                    </div>
                    
                    <?php    
                
    }
                die(
    mysql_error());
            }
        }
    }
    ?>
    <form id="formid" method="post" name="myform" action="">
    <p>
    <label for="username">Admin:</label>
    <input name="admin" type="text" size="20" value="<?php if(isset($admin)) echo $admin?>" />
    <?php echo $admin_err?>
    </p>

    <p>
    <label for="pass">Password:</label>
    <input name="password" type="password" value="<?php if(isset($pass)) echo $pass;?>" />
    <?php echo $pass_err?>
    </p>

    <p>

    <input name="submit" type="submit" value="Log in" >
    </p>

    <?php if(isset($err_msg)) echo $err_msg?>
    </form>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Please give us the final HTML (as seen in your browser) instead of PHP ... it will make life easier for us and you :-)

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,694
    Thanks
    25
    Thanked 657 Times in 656 Posts
    To get and display text you just need this code: And the getXMLHTTP() function.
    Code:
    <script type="text/javascript">
    
    function approve(e) {
    var req = getXMLHTTP();
    if (req) {
            
            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    if (req.status == 200) {
                        var getajax = req.responseText;
                        document.getElementById("getajax").innerHTML=req.responseText;
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }                
            }            
            req.open("GET", "approve.php?stu="+e, true);
            req.send(null);
        }        
    }
    </script>
    You send 'e' as a code to select what text you what. Use a SWITCH in the approve.php file to do the selecting.

    If you want the text to be displayed in different places on you HTML page add a second parameter to the function and replace the getElementById("getajax") with that variable.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •