Jump to content

Working with forms?


Perad

Recommended Posts

I am trying to create a very simple script where I count the form entries and generate a total.

 

This is proving deceptively hard.

 

This is my form.

 

<div class="form_item">
<h3>Add a link to your website for $4</h3>
<input type="text" name="website" id="website" onchange="calculate()" value="<?php if (isset($_POST['website'])) { print $_POST['website']; } ?>"/>

<h3>Homepage</h3>
<p>Be on the homepage and be seen by hundreds of people</p>
<input type="checkbox" id="homepage" onchange="calculate()" name="homepage" <?php if (isset($_POST['homepage'])) { print 'CHECKED';} ?>> Home Page Ad (7 Days)


					<script type="text/javascript">
						var total = 0;
						function calculate()
						{
							document.write(document.calcform.website.value);		
						}

					</script>

	<h3>Category</h3>
	<p>Be the first thing users see when they enter your specified category.</p>
	<select name="duration" category onchange="calculate()">
		<option value="">None</option>
		<option value="3" <?php if (isset($_POST['duration']) == '3') { print 'selected="selected"'; } ?> >3 Days</option>
		<option value="7" <?php if (isset($_POST['duration']) == '7') { print 'selected="selected"'; } ?>>7 Days</option>
	</select>						
</div>

 

The forms name is called calcform.

 

Now what I am trying to do is as follows.

> If the website input has got someone thing in it then I want to set total variable to 4.

> If the homepage checkbox is ticked I want to add 20 to the total variable.

> If the category drop down is set I want I want to multiply 3 by the number of days in the select box.

 

I then need to be able to subtract from the total if the check box is unchecked. The category days are set to 0 and the website box is emptied.

 

Please.. can someone tell me how I can get the value for each of the 3 form fields. Once I know this I am sure that I can work out the rest myself.

Link to comment
Share on other sites

you can do something like:

 

<script type="text/javascript">
function checkFields(){
var website = document.forms['myForm'].website.value;
var homepage = document.forms['myForm'].homepage.value;
var duration = document.forms['myForm'].duration.value;

if(website == ""){
website = 0;
} else{
website = 4;
}

if(homepage == ""){
homepage = 0;
} else{
homepage = 20;
}

if(duration == ""){
duration = 0;
}

var total_value = website + homepage + duration;
}

// then on calculate() do something like this:
function calculate(){
var total = 100; // enter whatever you want the total to be
checkFields();
var new_total = total - total_value;
document.getElementById("showNewCalculations").innerHTML=new_total;
}
</script>
<form name="myForm">
<!--
put your form here
-->
Total: <span id="showNewCalculations"></span>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.