CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   Making an AJAX announcement (http://www.codingforums.com/showthread.php?t=274666)

Hydrian 09-29-2012 08:48 AM

Making an AJAX announcement
 
I want to know how to write a bit of script that will allow me to post a announcement on the top of a page with a form, so that it changes for everyone.

Code:

//this is my form

<input type='text' id='userInput1' />
<input type='button' onclick='announcement()' value='Set Announcement' size='30'>


Code:

//this is the javascript for the change of annoucnement

function announcement(){
        var userInput = document.getElementById('userInput1').value;
        document.getElementById('boldStuff1').innerHTML = userInput;
}

So what it dose is, when you type something into the input box and hit submit, it changes the announcement for everybody. How can i do this?

Hydrian 09-29-2012 01:28 PM

help please

sunfighter 09-29-2012 04:12 PM

You do not need ajax for this. here is a method using php.

Code:

<?php
$do = 'yes';
$ann = '';
$file = @fopen("announcement.txt","r") or exit("announcement file does not exist!");
while(!feof($file))
  {
  $ann .= fgets($file);
  if($ann != '') $ann = $ann.'<br />';
  }
if($ann == '')
        $do = 'no';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>New document</title>
<style type="text/css">
#announcement{
        width: 300px;
        height: 100px;
        border: black solid 1px;
}
</style>
</head>
<body>
<?php
$mystring = <<<EOT
<div id="announcement">
$ann
</div>
EOT;
if($do == 'yes') echo $mystring;
?>
Give me an announcement
</body>
</html>

The text file "announcement.txt" is your announcement and looks like this:
Code:

Now is the time
for all good men
to come to the aid
of their country

Test this with the given text file. Then empty the text file of everything and lastly test it by deleting the file altogether.

If you like this approach and want further explanation of the code just LMK.

Hydrian 09-30-2012 03:02 AM

Thanks for that, it works. Now, what i wanted to do is, when i type into a input box say, Test, and then hit the button, it will set the announcement.

How will i do this?

sunfighter 09-30-2012 04:49 PM

When the button is hit you take what is written in the textarea and overwrite a text file called announcement.txt with it. This should be easy to do so I'm not coding it. If you have trouble with it post the code you have and we'll correct it.


All times are GMT +1. The time now is 02:24 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.