After a bit of help and learning, I have managed to change a target via JScript, but I can't seem to figure out how to make a variable or make it do a calculation.
My code so far is (these are three Form Boxes + a Submit Button):
NUMBER
<input name="contact_build"
type="text"
id="contact_build"
value="<?php echo $row_contact['contact_build']; ?>" />
TYPE
<select id="contact_type"
name="contact_type"
onchange="targ.value=this.value" />
<?php
$specresult = mysql_query("SELECT * FROM `types` ORDER BY type_value");
while ($specrow = mysql_fetch_array($specresult)) {
echo '<option value="' . $specrow['type_name'] . '">' . $specrow['type_name'] . '</option>';
}
?>
</select>
VALUE
<input name="contact_value" id="targ" value="<?php
$var1 = $row_contact['contact_build'];
$var2 = $row_value['type_value'];
$total = round($var2 * $var1);
echo "" . $total . "";
?>" />
<input type="submit" name="Submit2" value="Update" />
What I want to do is make "TARG"
the "$VAR2"
variable, so that it can work out the VALUE calculation then echo (or what ever command is required) the answer in the VALUE box, as soon as the user changes the option in the TYPE box. Hope this makes sense.
At the moment I have managed to get it to show the "TYPE"
value in the "VALUE"
box. It ignores any "echo" command above.
Am I looking at this incorrectly and it should be done another way?
PS: I know it's MYSQL; I am working on learning PDO as we speak to change it over. :-) But for the moment, I just want to figure this out using the language that I have basic knowledge of.
Go through the code below.
<body>
<input name="contact_build"
type="text"
id="contact_build"
value="<?php echo $row_contact['contact_build']; ?>" />
<select onchange="change(this.value)" >
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
</select>
<input type="text" id="targ" value="2"/>
<script>
function change(val) {
var cnt_bld = document.getElementById("contact_build").value;
var total_val = Math.round(parseInt(val) * parseInt(cnt_bld));
document.getElementById("targ").value = total_val;
}
</script>
</body>
In the above code i have called a function change(this.value)
which will accept the select box value. Then in the function I have used that value to change the value of input field. Look at document.getElementById
. With your code your were directly using a id
instead. Hope this helps.
EDIT this line
echo '<option value="' . $specrow['type_name'] . '">' . $specrow['type_name'] . '</option>';
can be done like this if you have those name and value.
echo '<option value="' . $specrow['type_value'] . '">' . $specrow['type_name'] . '</option>';
assuming your another column name as type_value where values are stored like 100,200and so on and type_name is the name like good, better or best.
Hope this is what you may want
<html>
<body>
<input name="contact_build"
type="text"
id="contact_build"
value="10" />
<select onchange="change(this.value)" >
<option value="0">Select</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
</select>
<input type="text" id="targ" value="0"/>
<script>
function change(val) {
document.getElementById("targ").value = document.getElementById("contact_build").value*val;
}
</script>
</body>
</html>