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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Input from a form gets included in text on website?

    Hi all,

    I have this idea - and there might be something already out there that can do it for me - but I don't even really know how to phrase what I'm looking for.

    Basically, I want the user to be able to enter a word in a text box. When the form is submitted, that word is included in the text in the resulting page in several places. Maybe this example will work well:

    The text on the website might be this:

    Yesterday for lunch I ate a delicious _________. It was the first time in a while I had eaten a _________ and it was even better than I expected it would be. In fact, it was so good that I immediately ran to the store and purchased a whole box of ________s.
    A user would come to the site and be prompted for a type of food. If they entered banana in the text box and submitted the form, the resulting page would then read:

    Yesterday for lunch I ate a delicious banana. It was the first time in a while I had eaten a banana and it was even better than I expected it would be. In fact, it was so good that I immediately ran to the store and purchased a whole box of bananas.
    Is this even possible? And if so, how difficult would it be to do?

    Thanks a bunch for the help!
    Last edited by jmcewan99; 01-26-2012 at 05:34 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Doing that on the CURRENT WEB PAGE is easy, using JavaScript.

    And with cookies, we could even remember the answers for the computer and user where the data was entered.

    But if you want that information to be saved so that anybody else can see it, you will have to use some server-side coding. PHP, ASP, JSP, etc.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Doing that on the CURRENT WEB PAGE is easy, using JavaScript.

    And with cookies, we could even remember the answers for the computer and user where the data was entered.

    But if you want that information to be saved so that anybody else can see it, you will have to use some server-side coding. PHP, ASP, JSP, etc.
    Just for the current web page - how would that work with JavaScript?

    No idea where to begin.

    Thanks.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Not quite as easy as that, when you look into it in depth.

    Code:
    <div id = "mydiv">
    Yesterday for lunch I ate a delicious ______. It was the first time in a while I had eaten a ______  and it was even better than I expected it would be. In fact, it was so good that I immediately ran to the store and purchased a whole box of ______s.
    </div>
    <br>
    Other stuff here
    <br><br>
    
    Enter a word <input type = "text" id = "theWord" onfocus='this.value = "" '; onblur = "change('mydiv',  this.value)">
    
    <script type = "text/javascript">
    
    function change(IDS, change2){
    change2 = change2.replace(/[^a-z]/gi,"").toLowerCase();
    if (change2.length<1) {return false}
    document.getElementById("theWord").value = change2;
    var text = new String("Yesterday for lunch I ate a delicious xxx. It was the first time in a while I had eaten a xxx and it was even better than I expected it would be. In fact, it was so good that I immediately ran to the store and purchased a whole box of xxxs.");
    var replaceVal = new String("xxx"); 	 // value to be replaced
    var match = new RegExp(replaceVal, 'ig');	 // setting regex with replaceVal and case insensitive along with global
    var output = text.replace(match, change2); // replacing variable string with variable replacement value
    document.getElementById(IDS).innerHTML=output;
    }
    </script>
    But I have to say that it seems rather pointless. Not even amusing. And as Old Pedant has pointed out, only the user can see it.

    Note that the plural of banana will be bananas, and the plural of grape will be grapes, but without adjustment the plural of peach will be peachs and the plural of strawberry will be strawberrys This might help (depending on other words in the text):-

    Code:
    output = output.replace(/([^aeiou])ys\b/,"$1ies");  // strawberries, but not plays
    output = output.replace(/([^o])chs\b/,"$1ches");  // peaches (but lochs is correct)
    output = output.replace(/shs\b/,"shes");  // bushes
    output = output.replace(/xs\b/,"xes"); // boxes (but not oxen!)
    output = output.replace(/zs\b/,"zes"); // buzzes
    output = output.replace(/fs|fes\b/,"ves") // knives, elves
    output = output.replace(/os\b/,"oes");  // buffaloes
    output = output.replace(/childs\b/,"children");
    output = output.replace(/mouses\b/,"mice")
    But there are quite a few other irregular plurals or words that exist only in the plural that do not end in "s" such as people, men, teeth, geese, loaf, feet, fish and deer. The only way to cope with these is individually.


    Is it your sister or your brother who is his dad? - Victoria Derbyshire, Radio 5 Live
    Last edited by Philip M; 01-26-2012 at 12:12 PM. Reason: Improved

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    jmcewan99 (01-27-2012)

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That's excellent - thank you so much!

    Is there a way to just have the form on one page and when a user submits the form they are sent to a new page that has the text with the blanks filled in - or does that require something on the server side?

    I guess the point is that I'd prefer the user not be able to see the text until they have submitted the form - then the text appears (or they get re-directed to another page).

    The user could remain on the same page, but then all the text would need to appear after they submitted the form - again, not sure if this is possible...?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Send the word to the next page using a query string.

    http://www.mydomain.com/page2.html?word=theWord

    For more info see:- http://javascript.about.com/library/blqs.htm

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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