Part 2; Validating form data: Make a copy of the order form from lab exercise 4
ID: 3813033 • Letter: P
Question
Part 2; Validating form data: Make a copy of the order form from lab exercise 4 and name it l6p2.php. Remove the JavaScript code to calculate the total price. If you used anything other than a plain text box for quantities, change the quantity inputs to type="text". (The goal of this part of the assignment is for you to use regular expressions to validate text.)
Add JavaScript code to produce an error message and suppress submission of the form if any quantity field contains non-numeric data. (It's OK for a quantity to be empty, but if it's non-empty, it must have only numbers.) Add an action= attribute to your <form> tag to submit the form to http://weblab.kennesaw.edu/formtest.php. Test that the form is submitted correctly when the quantities are numeric or empty, and that an error message is produced otherwise. (Regular expressions are your friend. Anything that's not a digit is bad. What is the predefined class for non-digits?)
Add to your index page a link to your new order form.
As with the previous assignment, the goal is to learn to validate with JavaScript. The use of an input element with type "number" or anything similar will earn a zero on this part of the assignment. You must use JavaScript for the validation.
Part 3; Using PHP to generate forms: To your order form from Part 2, add input elements for customer name and address if they're not already there. The "state" part of the address must be a select element(also called a "pulldown menu") to allow the person placing the order to choose his or her state. Don't type all fifty states, but supply at least Georgia, Alabama and Florida. Put the names of the states in an PHP array and use PHP code to create the drop-down dynamically. (This will still be named l6p2.php; you're just adding to something you've already tested.)
You will find an example of creating a select element for states in the class slides covering HTML forms. You are going to create the drop-down dynamically, and not by hard-coding it. This part of the assignment is a fair example of something you might do with PHP in "real life." Instead of an array, you would use a database call, so if we suddenly add another state to the U.S. (or your company suddenly adds another state to its service area) a change to the database automatically (and immediately) changes what people see in the form. For this assignment, you're loading states from an array. We'll get them from a database in the next assignment.
You do not need a special link on your index page for Part 3; I will test Part 3 at the same time I test Part 2.
<----- This is my order form ----->
<!DOCTYPE html>
<title>values
</title>
<meta charset="UTF-8" />
</head>
<body>
<script>
function orderFormJS()
{
var total = 0;
var tax = 0;
var hoe = document.getElementById("hoe").value;
var c_clamp = document.getElementById("c_clamp").value;
var hedge_clippers = document.getElementById("hedge_clippers").value;
plums = (hoe == "") ? parseInt("0") : parseInt(hoe);
c_clamp = (c_clamp == "") ? parseInt("0") : parseInt(c_clamp);
hedge_clippers = (hedge_clippers == "") ? parseInt("0") : parseInt(hedge_clippers);
total = (hedge clippers* 9.50) + (c clamps * 8.50) + (hoe* 10.50);
tax = total * 1.07;
return confirm("Your total = $" + (total + tax).toFixed(2));
}
</script>
<form action="http://weblab.kennesaw.edu/formtest.php" method = "post">
<br/>
<label>Name:
<input type="text" name="Name"/>
</label>
<br>
<label>Shipping Address:
<input type="text" name="Address"/>
</label>
<br>
<p>
<label> Number of hoes you would like to order? <input type="text" name="hoe" id="hoe"/>
</label>
<label> Number of c clamps you would like to order? <input type="text" name="c_clamp" id="c_clamp"/>
</label>
<label>Number of hedge clippers you would you like to order? <input type="text" name="hedge_clippers" id="hedge_clippers"/>
</label>
<br>
<br>
<label> Payment Method:
</label>
<br>
<label>Visa Card
<input type="radio" name="pay"
id="pay_visa" value="visa"
checked />
</label><br />
<label>Master Card
<input type="radio" name="pay"
id="pay_master" value="master" />
</label><br />
<label>American Express
<input type="radio" name="pay"
id="pay_american" value="american" />
</label><br />
<input type="submit" value="Submit">
</form>
</body>
</html>
Explanation / Answer
import java.applet.*;
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;
/**
* <i>Standard audio</i>. This category provides a basic capability for
* making, reading, and saving audio.
* <p>
* The audio format uses a rate of forty four,100 (CD quality audio), 16-bit, monaural.
*
* <p>
* for added documentation, see <a href="http://introcs.cs.princeton.edu/15inout">Section one.5</a> of
* <i>Introduction to Programming in Java: AN knowledge base Approach</i> by Henry M. Robert Sedgewick and Kevin Wayne.
*/
public final category StdAudio {
/**
* The sample rate - forty four,100 cycles/second for CD quality audio.
*/
public static final int SAMPLE_RATE = 44100;
personal static final int BYTES_PER_SAMPLE = 2; // 16-bit audio
personal static final int BITS_PER_SAMPLE = 16; // 16-bit audio
personal static final double MAX_16_BIT = Short.MAX_VALUE; // thirty two,767
personal static final int SAMPLE_BUFFER_SIZE = 4096;
personal static SourceDataLine line; // to play the sound
personal static byte[] buffer; // our internal buffer
personal static int bufferSize = 0; // variety of samples presently in internal buffer
// don't instantiate
personal StdAudio()
// static initializer
static
// open up AN audio stream
personal static void init() very little Endian
AudioFormat format = new AudioFormat(SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false);
DataLine.Info data = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);
// the inner buffer could be a fraction of the particular buffer size, this alternative is bigoted
// it gets divided as a result of we will not expect the buffered knowledge to line up specifically with once
// the sound card decides to push its samples.
buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE/3];
} catch (Exception e)
// no sound gets created before this decision
line.start();
}
/**
* shut commonplace audio.
*/
public static void close()
/**
* Write one sample (between -1.0 and +1.0) to plain audio. If the sample
* is outside the vary, it'll be clipped.
*/
public static void play(double in) very little Endian
// send to sound card if buffer is full
if (bufferSize >= buffer.length)
}
/**
* Write AN array of samples (between -1.0 and +1.0) to plain audio. If a sample
* is outside the vary, it'll be clipped.
*/
public static void play(double[] input)
}
/**
* scan audio samples from a file (in .wav or .au format) and come them as a double array
* with values between -1.0 and +1.0.
*/
public static double[] read(String filename) {
byte[] knowledge = readByte(filename);
int N = knowledge.length;
double[] d = new double[N/2];
for (int i = 0; i < N/2; i++)
return d;
}
/**
* Play a sound file (in .wav, .mid, or .au format) during a background thread.
*/
public static void play(String filename) computer address computer address = null;
try {
File file = new File(filename);
if (file.canRead()) computer address = file.toURI().toURL();
}
catch (MalformedURLException e)
// computer address computer address = StdAudio.class.getResource(filename);
if (url == null) throw new RuntimeException("audio " + computer file name + " not found");
AudioClip clip = application.newAudioClip(url);
clip.play();
}
/**
* Loop a sound file (in .wav, .mid, or .au format) during a background thread.
*/
public static void loop(String filename) computer address computer address = null;
try {
File file = new File(filename);
if (file.canRead()) computer address = file.toURI().toURL();
}
catch (MalformedURLException e)
// computer address computer address = StdAudio.class.getResource(filename);
if (url == null) throw new RuntimeException("audio " + computer file name + " not found");
AudioClip clip = application.newAudioClip(url);
clip.loop();
}
// come knowledge as a computer memory unit array
personal static byte[] readByte(String filename) {
byte[] knowledge = null;
AudioInputStream ais = null;
try attempt to scan from file
File file = new File(filename);
if (file.exists()) {
ais = AudioSystem.getAudioInputStream(file);
knowledge = new byte[ais.available()];
ais.read(data);
}
// try and scan from computer address
else computer address computer address = StdAudio.class.getResource(filename);
ais = AudioSystem.getAudioInputStream(url);
knowledge = new byte[ais.available()];
ais.read(data);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
throw new RuntimeException("Could not scan " + filename);
}
come data;
}
/**
* Save the double array as a sound file (using .wav or .au format).
*/
public static void save(String computer file name, double[] input) {
// assumes forty four,100 samples per second
// use 16-bit audio, mono, signed PCM, very little Endian
AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
byte[] knowledge = new byte[2 * input.length];
for (int i = 0; i < input.length; i++) temporary worker = (short) (input[i] * MAX_16_BIT);
data[2*i + 0] = (byte) temp;
data[2*i + 1] = (byte) (temp >> 8);
}
// currently save the file
try {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
AudioInputStream ais = new AudioInputStream(bais, format, input.length);
if (filename.endsWith(".wav") || computer file name.endsWith(".WAV"))
else if (filename.endsWith(".au") || computer file name.endsWith(".AU"))
else
}
/* *********************************************************************
* sample take a look at consumer
***********************************************************************/
// produce a note (sine wave) of the given frequency (Hz), for the given
// period (seconds) scaled to the given volume (amplitude)
personal static double[] note(double cycles/second, double period, double amplitude) maths.sin(2 * scientific discipline.PI * i * cycles/second / StdAudio.SAMPLE_RATE);
return a;
}
/**
* take a look at consumer - play AN a serious scale to plain audio.
*/
public static void main(String[] args) {
// 440 cycles/second for one sec
double freq = 440.0;
for (int i = 0; i <= StdAudio.SAMPLE_RATE; i++)
// scale increments
int[] steps = zero, 2, 4, 5, 7, 9, 11, 12 };
for (int i = 0; i < steps.length; i++) {
double cycles/second = 440.0 * Math.pow(2, steps[i] / twelve.0);
StdAudio.play(note(hz, 1.0, 0.5));
}
// have to be compelled to decision this in non-interactive stuff therefore the program does not terminate
// till all the sound leaves the speaker.
StdAudio.close();
// have to be compelled to terminate a Java program with sound
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.