...

View Full Version : Input from a form gets included in text on website?



jmcewan99
01-26-2012, 05:26 AM
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!

Old Pedant
01-26-2012, 06:23 AM
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.

jmcewan99
01-26-2012, 06:35 AM
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.

Philip M
01-26-2012, 09:32 AM
Not quite as easy as that, when you look into it in depth.


<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 :eek: This might help (depending on other words in the text):-


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

jmcewan99
01-27-2012, 04:35 AM
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...?

Philip M
01-27-2012, 09:05 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum