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 09-29-2012, 08:48 AM   PM User | #1
Hydrian
New Coder

 
Join Date: Aug 2012
Posts: 40
Thanks: 5
Thanked 0 Times in 0 Posts
Hydrian is an unknown quantity at this point
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?

Last edited by Hydrian; 09-29-2012 at 01:29 PM..
Hydrian is offline   Reply With Quote
Old 09-29-2012, 01:28 PM   PM User | #2
Hydrian
New Coder

 
Join Date: Aug 2012
Posts: 40
Thanks: 5
Thanked 0 Times in 0 Posts
Hydrian is an unknown quantity at this point
help please
Hydrian is offline   Reply With Quote
Old 09-29-2012, 04:12 PM   PM User | #3
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,364
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
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.
sunfighter is offline   Reply With Quote
Users who have thanked sunfighter for this post:
Hydrian (09-30-2012)
Old 09-30-2012, 03:02 AM   PM User | #4
Hydrian
New Coder

 
Join Date: Aug 2012
Posts: 40
Thanks: 5
Thanked 0 Times in 0 Posts
Hydrian is an unknown quantity at this point
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?
Hydrian is offline   Reply With Quote
Old 09-30-2012, 04:49 PM   PM User | #5
sunfighter
Senior Coder

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

Bookmarks

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 07:04 AM.


Advertisement
Log in to turn off these ads.