Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    40
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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 02:29 PM.

  • #2
    New Coder
    Join Date
    Aug 2012
    Posts
    40
    Thanks
    5
    Thanked 0 Times in 0 Posts
    help please

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,721
    Thanks
    25
    Thanked 662 Times in 661 Posts
    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.

  • Users who have thanked sunfighter for this post:

    Hydrian (09-30-2012)

  • #4
    New Coder
    Join Date
    Aug 2012
    Posts
    40
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,721
    Thanks
    25
    Thanked 662 Times in 661 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •