0
votes

I have a form that im working on, im trying to reset a field when the no value is selected from the dropdown. My form layout is as follows: [Dropdown - name : gasoiluser - values = yes/no] [Text form field, name = gasoilmargin]

So someone comes along and selects yes from the drop down, enters a value into the text box and hits submit. If they then come back later and set the gasoiluser dropdown to no and hit submit the value in the text box stays, what I would like is when no is selected on the dropdown the form resets the value of the text field to 0.00.

I have tried doing this with php without much success, so I am not sure if there is a easier more elegant solution with javascript ?.

Thanks :-)

The code I have tried is :

<?php
$query = mysql_query("SELECT * FROM hqfjt_chronoforms_data_dashboard WHERE cf_id = '1'") or die(mysql_error());
$oilprice = mysql_fetch_object($query);

$oilpricederv = $oilprice->oilpricederv;

$dervmargin = $form->data['dervmargin'];
$dervuser = $form->data['dervuser'];

if ($dervuser=="" or $dervuser=="no")
  $calculateddervprice = '0.00';
else
  $calculateddervprice = $dervmargin + $oilpricederv;
?>
1
show us the code you have tried. - user557846
If you're using jQuery or some other JS framework, you can just attach a listener to your select: $('select#gasoiluser').change(function(e){ if ($(this).val()=='NO') { $('input#yourTextBox').val(''); } } - lucifurious
Well, regardless of any answer you may see here, ALWAYS validate the form on the PHP side. JavaScript can be altered and broken easily by a malicious user. - Madara's Ghost
echo $dervuser; does it return what you expect ? - user557846

1 Answers

1
votes

Here is the javascript version.

But make sure to dbl check that with php incase the browser messes up. here is a sample php code:

$dropMenu = $_POST["gasoiluser"];
$textBox = (($dropMenu == "no")?"0.00":$_POST["gasoilmargin"]);
// Do stuff w/ variables here (like mysql)