Jump to content

Getting an echo to disappear after 10 seconds


Strac72

Recommended Posts

After much fizzling of brain and some help on here, I managed to get this script to do what I wanted:

 

<td>

      <form name="input" action="motw.php" method="post">

<h4>Want a specific film?  <input type="text" width="300" name="requests" />

<input type="submit" value="Request" /></h4>

      </form> 

<?php

$requests = $_POST['requests'];

 

$data = "$requests\n";

 

 

//open the file and choose the mode

$fh = fopen("../filmoftheweek/users.txt", "a");

 

fwrite($fh, $data);

 

fclose($fh);

 

if (!empty ($_POST))

 

print "Ok, I'll see what I can do.";

 

?>

</td>

   

I would now like to make the echo/print disappear again after 10 seconds. Is there any way to do this?

 

Thanks in advance,

 

Paul

Link to comment
Share on other sites

Yes, it's only a single text field, within a table, and the message comes up beneath the field on clicking the request button. It also writes to a text file but it would be nice if the message disappeared again after 10 seconds as there's plenty of other stuff on the page to look at and do. I just know that if you submit a form and get no message, users are wondering if anything has happened.

Link to comment
Share on other sites

You would need to use JavaScript to dynamically remove the element after a period of time:

 

<script type="text/javascript">
    window.onload = function()
    {
        timedHide(document.getElementById('id_of_element'), 10);
    }

    function timedHide(element, seconds)
    {
        if (element) {
            setTimeout(function() {
                element.style.display = 'none';
            }, seconds*1000);
        }
    }
</script>

 

If you have already defined an onload event handler, you'll need to merge this with the existing one.

 

Moving to JS forum by the way.

Link to comment
Share on other sites

There isn't an onload handler, that's the whole of the script here. I have no idea how to implement the javascript within it. It's a maybe a simple little thing but it took me about 8 hours to get that working from what I could glean from tutorials and help composing the php if statement from kind folk at the php forum. That's my level of expertise, I'm afraid! I am learning. This isn't for a public site.

Link to comment
Share on other sites

Not to sound thick, but, how do I assign an ID to the element? it only exists as a print command:

 

print "Ok, I'll see what I can do.";

 

I did try adding it to the form, but I know that's not the right place on thinking a bit harder:

 

<form name="input" action="motw.php" method="post">

<h4>Want a specific film?  <input type="text" width="300" name="requests" id="message" />

<input type="submit" value="Request" /></h4>

      </form> 

 

I really don't know how to assign it Adam. I did try looking around on the net so as not to just have it all done for me, but to no avail!

Link to comment
Share on other sites

I have ended up with this:

 

<script type="text/javascript">

    window.onload = function()

    {

        timedHide(document.getElementById('message'), 10);

    }

 

    function timedHide(element, seconds)

    {

        if (element) {

            setTimeout(function() {

                element.style.display = 'none';

            }, seconds*1000);

        }

    }

</script>

</head>

 

<body onload="MM_preloadImages('../images/buttons/downloadNeg.png')">

 

<div align="center"><img src="../cinema/motw_images/movie_download.png" width="378" height="182" /></div>

 

<h3 align="center"><strong>This week's film is:</strong></h3>

 

<h1 align="center"><strong>"Source Code"</strong></h1>

 

<div align="center">

  <a href="http://db.tt/XdFJgqM" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('downmotw','','../images/buttons/downloadNeg.png',1)"><img src="../images/buttons/download.png" name="downmotw" width="150" height="75" border="0" id="downmotw" /></a></div>

 

<p align="center"><strong>If the file starts to play in your media player and you want to watch it later, right click the button and choose 'save as' or 'save link as'.</strong></p>

<div align="center">

  <table width="100%" border="0" cellspacing="10">

  <tr>

        <td><h2 align="center"><strong><u>Description</u></strong></h2></td>

    <td>

      <form name="input" action="motw.php" method="post">

<h4>Want a specific film?  <input type="text" width="300" name="requests" />

<input type="submit" value="Request" /></h4>

      </form> 

<?php

$requests = $_POST['requests'];

 

$data = "$requests\n";

 

$fh = fopen("../filmoftheweek/users.txt", "a");

 

fwrite($fh, $data);

 

fclose($fh);

 

if (!empty ($_POST))

 

print '<span id="message">Ok, I\'ll see what I can do.</span>';

 

?>

</td>

 

Which leaves the message on the screen in Firefox, Chrome and IE. Haven't tested it on other browsers but I don't think it's that.  Anybody got any ideas?

Link to comment
Share on other sites

You already have an onload event defined:

 

onload="MM_preloadImages('../images/buttons/downloadNeg.png')"

 

Move the function call into the anonymous function:

 

    window.onload = function()
    {
        MM_preloadImages('../images/buttons/downloadNeg.png');
        timedHide(document.getElementById('message'), 10);
    }

Link to comment
Share on other sites

You deserve the guru bit Adam! Thanks very much indeed.  8) 8) I suppose I've learned 3 things today:

 

1. Always give out an entire script if asking for help.

 

2. Don't trust Fireworks generated code.

 

3. There are good folk in the online community who do it because they love it.

 

Actually, make that 4; my distinction in Designing and creating advanced websites isn't worth the paper it's printed on. Cheers City & Guilds and Trowbridge college.  :wtf:

 

Thanks again Adam.

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.