/    Sign up×
Community /Pin to ProfileBookmark

Updating form depending on checkbox value

I’m writing a PHP web site but need some Javascript to do a bit of extra stuff.

The main form I’m writing is a ‘wizard’ style form which allows users to enter information into the system bit by bit. Each sub-form has a ‘Next’ button to allow the user to move to the next step.

The first page contains a ‘registration/login’ page where users can enter their business details if they’ve never registered before. The next page in the chain allows them to type in details of the property they own (it’s a site for hotel owners). At the top of this second page there’s a checkbox that should allow users to select it and automatically fill in other details on the form if necessary.

I’ve got some test code in there to try it out but I get an ‘error on page’ at the bottom of the IE window. The test function I’m using is:

<script type=”text/javascript”>
function changemessage(box)
{
if (box.checked)
{
document.mainform.name.value= ‘test’;
document.mainform.address.value= ‘test2’;
document.mainform.district.value= ‘test3’;
document.mainform.town.value=’test4′;
}
}
</script>

The form itself is defined with:

<form action=”<? echo $PHP_SELF ?>” method=”post” name=”mainform”>

and the checkbox is defined with:

<input name=”setdetails” type=”checkbox” onclick=”changemessage(this)”>

I haven’t a clue about Javascript but getting this right would be a nice extra touch to the wizard.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@KorMar 05.2004 — It should work, at a first glance...

But, as [b]name[/b] is one of the reserved words in javascript, chanage the name="name" in that input, make it name="nm" or name="thisname" or something. See if works now

And... you want those values to changes when the checkbox is unchecked? This case use full boolean

if (box.checked==true) {

...}

else if (box.checked==false) {

...}
Copy linkTweet thisAlerts:
@oleragMar 05.2004 — I, too, can find nothing wrong with your syntax. I suggest

you turn on the JS Console offered by your browser and

capture the line number of the error thats causing the

problem. From there, you'll probably find a silly typo

that needs correction.
Copy linkTweet thisAlerts:
@chrizMar 05.2004 — You can try this source code

<script>

function changemessage() {

if(document.mainform.setdetails.checked) {

document.mainform.name.value= 'test';

document.mainform.address.value= 'test2';

document.mainform.district.value= 'test3';

document.mainform.town.value='test4';

}

else {

}

}

</script>

<!-- put this in your form tags -->

<input name="setdetails" type="checkbox" onclick="changemessage()">

That sghould work?
Copy linkTweet thisAlerts:
@Tickled_PinkauthorMar 05.2004 — Just tried Chriz's solution and it works - sort of. At least there's no error now.

But unfortunately the form fields are not being filled in.

Any ideas?
Copy linkTweet thisAlerts:
@steelersfan88Mar 05.2004 — I used chriz's solution ... and modified it a tad and it worked great:[code=php]<script>

function changemessage() {
if(document.mainform.setdetails.checked) {
document.mainform.name.value= 'test';
document.mainform.address.value= 'test2';
document.mainform.district.value= 'test3';
document.mainform.town.value='test4';
}
else {
document.mainform.name.value= '';
document.mainform.address.value= '';
document.mainform.district.value= '';
document.mainform.town.value='';
}
}

</script>

<form name="mainform">

<input name="setdetails" type="checkbox" onclick="changemessage()">
<input name="name">
<input name="address">
<input name="district">
<input name="town">
</form>[/code]
(NOTE: Script not validated)
Copy linkTweet thisAlerts:
@oleragMar 05.2004 — You've got something else wrong in your html then. This

sample works in IE6/NN7 and includes code to disable the

text objects if the checkbox is not checked.
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Ticled Pink Demo</title>
<script type="text/javascript">
<!--
function changeMessage(obj) {
form = document.mainForm;
if (obj.checked) {
form.name.value = "Name Test";
form.address.value = "Address Test";
form.district.value = "Distict Test";
form.town.value = "Town Test";
}
else {
for (var i=0; i<form.elements.length; i++) {
if (form.elements[i].type == "text") {
form.elements[i].value = "";
}
}
}
}
// -->
</script>

<div style="text-align: center;">
<strong>Tickled Pink Demo</strong>
</div>

<form action="#" name="mainForm">
<p style="text-align: center;">
<input type="checkbox" name="setDetails" onclick="changeMessage(this)" />
</p>

<p>
<input type="text" name="name" size="15" />
<input type="text" name="address" size="15" />
<input type="text" name="district" size="15" />
<input type="text" name="town" size="15" />
</p>
</form>
[/code]
Copy linkTweet thisAlerts:
@Tickled_PinkauthorMar 06.2004 — Finally got it to work (using Olerag's nice code) ?

Now I have another question. How do I change the hard coded output text values in the function to display the contents of a PHP variable?
Copy linkTweet thisAlerts:
@oleragMar 06.2004 — Visit that forum and/or search for embedding php variables

into html forms objects. I'm sure you'll find one or more

ways that will meet your requirements.
×

Success!

Help @Tickled_Pink spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...