Perad Posted April 22, 2008 Share Posted April 22, 2008 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 https://forums.phpfreaks.com/topic/102309-working-with-forms/ Share on other sites More sharing options...
Northern Flame Posted April 22, 2008 Share Posted April 22, 2008 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 https://forums.phpfreaks.com/topic/102309-working-with-forms/#findComment-524489 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.