Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2012, 10:54 AM   PM User | #1
mdviky87
New to the CF scene

 
Join Date: Feb 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
mdviky87 is an unknown quantity at this point
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>
mdviky87 is offline   Reply With Quote
Old 02-21-2012, 12:46 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Please give us the final HTML (as seen in your browser) instead of PHP ... it will make life easier for us and you :-)
devnull69 is offline   Reply With Quote
Old 02-21-2012, 04:11 PM   PM User | #3
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,498
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
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.
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:24 PM.


Advertisement
Log in to turn off these ads.