Jump to content

I'm using Ajax to dipslay message, but how do I clear them?


cdoyle

Recommended Posts

Hi,

I have another question related to my forms.

I'm using Ajax to display messages when a user click on each field.

 

 <input type="text" id="username" name="username" onfocus="javascript: MyAjaxRequest('namefield','registercheck.php?act=namefield')"
                <input type="password" name="password" value="<?=$_POST['password'];?>"
                       onfocus="javascript: MyAjaxRequest('passfield','registercheck.php?act=passfield')"/>

 

Then I have another div, where the messages are displayed.

<span id="namefield"></span>
<span id="passfield"></span>

 

That works fine,  but when they click on the next field.  The new message appears but the old one doesn't go away.  How would I clear the previous message from the div?

 

Link to comment
Share on other sites

Is it adding it to the dom?

 

like:

 

<div></div>

 

-focus-

 

<div>YO!</div>

 

-newfocus-

 

<div>YO!YO!</div>

 

because if that is the case your ajax function is prolly just concatnating (SP?) the content.

 

Like: document.getElementById('passField').innerHTML+= X  //notice the += instead of =

Link to comment
Share on other sites

Unless the problem is that the message is going in staying in other divs like this:

 

<div id="1"></div>

<div id="2"></div>

 

-focus-

 

<div id="1">Yo</div>

<div id="2"></div>

 

-focus-

 

<div id="1">Yo</div>

<div id="2">Yo</div>

 

in which case you'd just need to mod your ajax function to empty all the not needed ones.

 

for example, if you only have like two...

 


function ajaxCalled(){

document.getElementById('1').innerHTML='';
document.getElementById('2').innerHTML='';

//then assign the right message as normal

}

 

if however you have a bunch, you can just give them classes and use (i'm using jquery) something to empty them all, then assign:

 


<div id="1" class="holder"></div>
<div id="2" class="holder"></div>

function ajaxCalled(){

$(".holder").each(function(){

     $(this).val('');

});

//continue putting the right message in

}

 

Link to comment
Share on other sites

I'm using the technique from the sticky http://www.phpfreaks.com/forums/index.php/topic,115581.0.html

Looking at that script,  I see this.  I don't see a +=

MyHttpRequest.onreadystatechange = function ()
{
if(MyHttpRequest.readyState == 4) // done and responded
{
document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result
}
else
{
document.getElementById(target_div).innerHTML = MyHttpLoading; // still working
}
}
MyHttpRequest.send(null);
}
else
{
document.getElementById(target_div).innerHTML = ErrorMSG; // the browser was unable to create a httprequest
}
}

 

The messages are kept in my php file, with a switch statement

 

Looks something like this.

 

  case 'namefield': {
            echo "<h3>Select a username</h3>";
            echo "This will be the name you use to log into the game with.";
            break;
        }

    case 'passfield': {
            echo "<h3>Select a Password</h3>";
            echo "Enter a password, Your password may contain only alphanumerical characters.";
            break;
        }

 

any ideas on how to make this work the way I need?

Link to comment
Share on other sites

Ok, you're not just putting it in a div, you're putting it in individual spans. So in the ajax request.js, it never knows that the others should be empty.

 

right where this part is:

 


if(MyHttpRequest.readyState == 4) // done and responded
{
document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result
}

 

add something like this:

 


if(MyHttpRequest.readyState == 4) // done and responded
{
document.getElementById('namefield').innerHTML ='';
document.getElementById('passfield').innerHTML ='';
document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result
}

Link to comment
Share on other sites

Ok, you're not just putting it in a div, you're putting it in individual spans. So in the ajax request.js, it never knows that the others should be empty.

 

right where this part is:

 


if(MyHttpRequest.readyState == 4) // done and responded
{
document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result
}

 

add something like this:

 


if(MyHttpRequest.readyState == 4) // done and responded
{
document.getElementById('namefield').innerHTML ='';
document.getElementById('passfield').innerHTML ='';
document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result
}

 

If I were to use a div instead,  would I still need to add that for each field in my form?

Or would I be able to make it work without having to add a line of code for each field?

Link to comment
Share on other sites

What I would do is use the jquery library.

 

Just have ur html, then use the js to select all the divs by class, then update the one u need.

 


<div id="holder">
     <div id="name" class="item"></div>
     <div id="pass" class="item"></div>
</div>

js:

<script src="path_to_jquery"></script>

<script your js>
if(MyHttpRequest.readyState==4){

$(".item").each(function(){

     $(this).html('');

});

$("#"+target_div).html(MyHttpRequest.responseText);

}

 

that will empty each of your subdivs, and then display the one desired one's new content.

Link to comment
Share on other sites

What I would do is use the jquery library.

 

Just have ur html, then use the js to select all the divs by class, then update the one u need.

 

[quote author=seventheyejosh link=topic=286859.msg1362291#msg1362291 date=1265762165]
What I would do is use the jquery library.

Just have ur html, then use the js to select all the divs by class, then update the one u need.

[code]

<div id="holder">
     <div id="name" class="item"></div>
     <div id="pass" class="item"></div>
</div>

js:

<script src="path_to_jquery"></script>

<script your js>
if(MyHttpRequest.readyState==4){

$(".item").each(function(){

     $(this).html('');

});

$("#"+target_div).html(MyHttpRequest.responseText);

}

 

that will empty each of your subdivs, and then display the one desired one's new content.

 

<div id="holder">

    <div id="name" class="item"></div>

    <div id="pass" class="item"></div>

</div>

 

js:

 

<script src="path_to_jquery"></script>

 

<script your js>

if(MyHttpRequest.readyState==4){

 

$(".item").each(function(){

What I would do is use the jquery library.

 

Just have ur html, then use the js to select all the divs by class, then update the one u need.

 


<div id="holder">
     <div id="name" class="item"></div>
     <div id="pass" class="item"></div>
</div>

js:

<script src="path_to_jquery"></script>

<script your js>
if(MyHttpRequest.readyState==4){

$(".item").each(function(){

     $(this).html('');

});

$("#"+target_div).html(MyHttpRequest.responseText);

}

 

that will empty each of your subdivs, and then display the one desired one's new content.

 

    $(this).html('');

 

});

 

$("#"+target_div).html(MyHttpRequest.responseText);

 

}

 

[/code]

 

that will empty each of your subdivs, and then display the one desired one's new content.

 

OK,  going to give this a shot.

 

so I've added the path to jquery to my page,  so then I add this

<script your js>
if(MyHttpRequest.readyState==4){

$(".item").each(function(){

     $(this).html('');

});

$("#"+target_div).html(MyHttpRequest.responseText);

}

</script>

 

Do I still use the PHP page to get the message?  Or am I pulling the message I want to display from somewhere else?

 

 

Link to comment
Share on other sites

All i can tell is you made something simple complicated.  Find the Target Location By the fellowing

 

getElementById - Example <TD id=id_name>

getElementsByTagName - Example <div> - Additionally u got to add the [0-9] depending on what # div it is

getElementsByName - Same thing as above getElementsByTagName, except you assign name like this <TD name=name_like_time_stamp_1>

 

As for clearing a DIV field do that in the same location as changing the text of the other DIV field.

So add the

document.getElementById('chat_message').value=''; <INPUT>

document.getElementById('chat_message').firstChild.nodeValue=''; <TD>

document.getElementById('chat_message').innerHTML=''; <DIV>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.