...

View Full Version : hidden value based on checkbox



learning curve
03-26-2012, 08:20 PM
I have a form that has a checkbox that I want to base a hidden value on. If the checkbox is checked I want the hidden value to be 25. If not checked, I want the value to be 0. I am working with code someone else has written and I am fairly new to javascript and cannot get it to work.
Here is the code on the form:
<code>
<td><input type="checkbox" name="binderdiscount" value="Yes" <?php /*?><?php if ($pgdata['binderdiscount']=='Yes') echo 'checked'; ?><?php */?> />I do not need a book;</td>

<input type="hidden" name="binder_discount" value="0">
</code>

and then in a separate page is all the javascript to process the form. I have this, but it is not working.
<code>
if(getControlValue(form.binderdiscount)==="Yes") {form.binder_discount.value=25;}
</code>

As I said, i am really new to javascript and I am working with someone else's code. All the research I have done has just confused me more about why it is not working. Any help would be greatly appreciated.

Philip M
03-26-2012, 08:52 PM
<input type="checkbox" name="binderdiscount" id = "binderdiscount" onclick = "change()">
<input type="hidden" name="binder_discount" id = "binder_discount" value = 0>

<script type = "text/javascript">
function change() {
if (document.getElementById("binderdiscount").checked) {
document.getElementById("binder_discount").value = 25;
}
else {
document.getElementById("binder_discount").value = 0;
}

}
</script>

Many people would say that it is not a good idea to give two elements such similar names, binderdiscount and binder_discount.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Mishu
03-27-2012, 03:08 AM
Any help would be greatly appreciated.

What you are trying to do lends itself nicely to using a ternary to set the hidden value.


<input onclick="changeHiddenInput(this);" type="checkbox" name="chk1" value="Yes" <?php if ($pgdata['binderdiscount'] == 'Yes') {echo 'checked="checked"';} ?>/>I do not need a book
<input type="hidden" name="hid1" id = "hid1" value = "<?php echo ($pgdata['binderdiscount'] == 'Yes') ? 25 : 0 ?>" />
<script>
function changeHiddenInput(elem){
document.getElementById('hid1').value = (elem.checked)? 25 : 0;
}
</script>

It's inefficient to run document.getElementById() 3 times. It's more efficient to pass the checkbox object to the function using the this keyword and then with a ternary you need to call document.getElementById only once.

learning curve
03-27-2012, 08:11 PM
Sorry, the solution from Philip didn't seem to work. It correctly placed a yes or no based on the checkbox, but didn't change binder_discount from 0 to 25. and Mishu, I appreciate your help, but I am afraid I am just new enough it confused me more than helped. I will do some more investigating and see what I can come up with.

Philip M
03-27-2012, 09:08 PM
Sorry, the solution from Philip didn't seem to work. It correctly placed a yes or no based on the checkbox, but didn't change binder_discount from 0 to 25.

Sorry, it works for me. I do not post untested code which does not work. When I add the test line
alert (document.getElementById("binder_discount").value);
I get 25 or 0 as appropriate (checked/unchecked).

No idea what you mean by "placed a yes or no".

Mishu's suggestions re efficiency are bull****. Any difference in exeuction time is not measurable even in milliseconds, and as you have found his code (ternary operator) is harder to understand.

felgall
03-27-2012, 09:18 PM
If you only need the final value on the server then no JavaScript at all is required.



<input type="hidden" name="binderdiscount" value="0">
<input type="checkbox" name="binderdiscount" value="25"">

that will send 0 to the server if the checkbox isn't selected and will send 25 if it is selected.

Note that both fields must have the same name for this to work and be specified in this order since only the last field for each name is sent to the server and checkboxes and radio buttons are ignored unless they are selected.

Mishu
03-27-2012, 11:59 PM
and Mishu, I appreciate your help, but I am afraid I am just new enough it confused me more than helped. I will do some more investigating and see what I can come up with.

That's ok :) At least you have seen something new. A ternary can be a very useful and efficient operator and I use it a lot.

Here is a simple explanation and tutorial (http://www.gsdesign.ro/blog/how-to-use-ternary-operator-in-javascript/) on how it works.


Sorry, the solution from Philip didn't seem to work. It correctly placed a yes or no based on the checkbox, but didn't change binder_discount from 0 to 25.

That's because in your code you have php code that sets the checkbox to checked if the php variable == 'yes' and philip's code hard codes a value of 0 to the hidden input whether the checkbox is checked or not which is not correct and that's why the code doesn't work.

Philip saying
No idea what you mean by "placed a yes or no". appears to show he has no understanding of your php code and so his code is misleading for a start because of the above.

You just need to add a php ternary to your hidden input similarly to how I did and that should fix the problem.

Old Pedant
03-28-2012, 02:40 AM
If you only need the final value on the server then no JavaScript at all is required.



<input type="hidden" name="binderdiscount" value="0">
<input type="checkbox" name="binderdiscount" value="25"">

that will send 0 to the server if the checkbox isn't selected and will send 25 if it is selected.
[/code]
Ummm...only if you are using PHP.

If you are using ASP/ASP.NET/JSP, then it will send an *ARRAY* of values to the server.

Now, that's perfectly okay. You just have to be aware it might be an array and then pick up the first element of the array, if it is.

So Felgall's html-only solution is elegant, just adapt your server-side code according to what server-side system you are using.

Bur realistically, it's just as easy in the server-side code if you simply do


<input type="checkbox" name="binderdiscount" value="25"">
and nothing more. If you don't get any form field by the name "binderdiscount" then you can assume the value is zero, in your server-side code.

Mishu
03-28-2012, 03:37 AM
Bur realistically, it's just as easy in the server-side code if you simply do


<input type="checkbox" name="binderdiscount" value="25"">and nothing more. If you don't get any form field by the name "binderdiscount" then you can assume the value is zero, in your server-side code.

I like this the best but you would still need to add the php code in the input element to set the checkbox initially to checked or unchecked


<input type="checkbox" name="chk1" value="Yes" <?php if ($pgdata['binderdiscount'] == 'Yes') {echo 'checked="checked"';} ?>/>And this is a good example of why when something can be done both server and client side then it should always be done server side and not waste time with javascript.

In this case you don't need javascript at all to send a value of 0 or 25 to the server.

learning curve
03-28-2012, 07:32 PM
wow! Thanks for all the help. I think I need to clarify something I thought I mentioned but it appears I left out.
I need the initial checkbox to put a yes or no in a field in the mysql table for a field called binderdiscount. yes=check box is checked

then I need the hidden field to input either a 25 or a zero in a table field called binder_discount based on the yes/no check.

The first is working (Yes/No), but the second is not .


Philip,
I am sure your code is great and I really appreciate your help. I just don't know enough to make it work for me in the context of my documents. Any failure is strictly on my end.

Mishu,
I fee like I am catching on to what you are helping with but it still doesn't work. I think that is because of other javascript that is running and doing things.

I hate to post so much info, but it might be beneficial here to see exactly what I am dealing with. As I said, I am fairly new to Javascript and I think this page is just overloading my brain it is so full of code. Here is the entire form page:

learning curve
03-28-2012, 07:32 PM
<?php
$noYes = array('No'=>'No','Yes'=>'Yes');
$days = array(
'-'=>'Choose date...',
'Wednesday'=>'Wednesday, November 14',
'Thursday'=>'Thursday, November 15',
'Friday'=>'Friday, November 16',
'Saturday'=>'Saturday, November 17',
'Sunday'=>'Sunday, November 18'
);
$hours = array(''=>'Choose...');
for ($i=1; $i<=12; $i++) $hours[$i] = $i;
$minutes = array('00'=>'00','05'=>'05');
for ($i=10; $i<60; $i+=5) $minutes["$i"] = "$i";
$ampm = array('am'=>'am','pm'=>'pm');

require 'workshops.php';
require '../prices_etc.php';
$workshops = workshopOptions('html');

global $pgdata;

function makeInput($type,$name,$options=NULL,$default='') { // Quickly makes form inputs with optional default values taken from $pgdata; This function was more useful when we were prefilling more options
global $pgdata;
switch($type) {
case 'text':
if ($options!=NULL) $size = $options;
else $size = '30';
$output = "<input type='text' name='$name' size='$size'";
if (isset($pgdata[$name])) $output .= " value='${pgdata[$name]}'";
$output .= '>';
break;
case 'textarea':
if ($options!=NULL) $size = $options;
else $size = '30';
$output = "<textarea name='$name' rows='3' cols='30'>";
if (isset($pgdata[$name])) $output .= $pgdata[$name];
$output .= '</textarea>';
break;
case 'checkbox':
$output = '';
foreach ($options as $key=>$value) {
$output .= "<p class='dentin'><input type='checkbox' name='$name' value='$key'";
if (isset($pgdata[$name])) {
if ($pgdata[$name]==$key) $output .= ' checked';
} elseif ($key==$default) $output .= ' checked';
$output .= ">$value</p>";
}
break;
case 'radio':
$output = '';
foreach ($options as $key=>$value) {
$output .= "<p class='dentin'><input type='radio' name='$name' value='$key'";
if (isset($pgdata[$name])) {
if ($pgdata[$name]==$key) $output .= ' checked';
} elseif ($key==$default) $output .= ' checked';
$output .= ">$value</p>";
}
break;
case 'select':
$output = "<select name='$name'>";
foreach ($options as $key=>$value) {
$output .= "<option value='$key'";
if (isset($pgdata[$name])) {
if ($pgdata[$name]==$key) $output .= ' selected';
} elseif ($key==$default) $output .= ' selected';
$output .= ">$value</option>";
}
$output .= "</select>";
break;
}
return $output;
}
?>

<h3>General information</h3>
<p>Note: Items marked with a blue bullet will be included in a list of attendee information published in the conference program.</p>
<table>
<tr>
<th><span class="publishedInfo">&bull;</span>First name:</th>
<td><?php echo makeInput('text','firstname'); ?></td>
</tr>
<tr>
<th><span class="publishedInfo">&bull;</span>Last name:</th>
<td><?php echo makeInput('text','lastname'); ?></td>
</tr>
<tr>
<th><span class="publishedInfo">&bull;</span>Your institution:<br><span class="aside">(if retired, former institution)</span></th>
<td>
<?php echo makeInput('radio','sincere',array('Yes'=>'sincere University'),'No'); ?>
<div id="sincereSpecial" class="indented">
<?php
global $useyear;
$schoolYear = $useyear.'-'.($useyear+1);
echo makeInput('radio','sincere_special',array('Grad student/Retiree'=>'I am a graduate student or a retiree (No registration fee)', 'FLC/NFTEP/PEP/GSTEP'=>"I am a member of a CELT FLC, or a member of NFTEP, PEP, or GSTEP during $schoolYear (No registration fee)", 'No'=>'None of the above <strong>($50 registration fee)</strong>'),'No');
?></div>
<p class='dentin'><input type='radio' name='sincere' value='No' <?php if ($pgdata['sincere']!='Yes') echo 'checked'; ?>>Other:<input type='text' name='institution' size='30' value='<?php echo $pgdata["institution"]; ?>'></p>
<div id="otherSpecial" class="indented"><?php echo makeInput('checkbox','other_special',array('Grad student/Retiree'=>'I am a graduate student or a retiree (25% off conference fees)')); ?></div> </td>
</tr>
<tr>
<th><span class="publishedInfo">&bull;</span>Department:</th>
<td><?php echo makeInput('text','department'); ?></td>
</tr>
<tr>
<th>Office street address:<br><span class="aside">(if retired, home address)</span></th>
<td><?php echo makeInput('textarea','address'); ?></td>
</tr>
<tr>
<th>City:</th>
<td><?php echo makeInput('text','city'); ?></td>
</tr>
<tr>
<th>State/province:</th>
<td><?php echo makeInput('text','state','10'); ?></td>
</tr>
<tr>
<th>Postal/ZIP code:</th>
<td><?php echo makeInput('text','zip','10'); ?></td>
</tr>
<tr>
<th><span class="publishedInfo">&bull;</span>Country:</th>
<td><?php echo makeInput('radio','chooseCountry',array('United States'=>'United States', 'Canada'=>'Canada', 'Other:'=>'Other:'.makeInput('text','country')),'United States'); ?></td>
</tr>
<tr>
<th>Office phone:<br>
<span class="aside">(if retired, home phone)</span></th>
<td><?php echo makeInput('text','office_phone','20'); ?></td>
</tr>
<tr>
<th>Home/cell phone:</th>
<td><?php echo makeInput('text','cell_phone','20'); ?></td>
</tr>
<tr>
<th><span class="publishedInfo">&bull;</span>Email address:</th>
<td><?php echo makeInput('text','email','40'); ?></td>
</tr>
<tr>
<th><span class="publishedInfo">&bull;</span>Position/title:<br><span class="aside">(if not faculty)</span></th>
<td><?php echo makeInput('text','position','40'); ?></td>
</tr>
<tr>
<th>First name or nickname:<br><span class="aside">(for nametag)</span></th>
<td><?php echo makeInput('text','nickname','20'); ?> <img src="http://celt.sincere.edu/registration/nametag.png" alt="Illustration of how the nickname will appear prominently on the nametag, followed by the full name" title="Illustration of how the nickname will appear prominently on the nametag, followed by the full name"></td>
</tr>
</table>
<p>New this year in the Conference's continuing efforts to "go green," we are offering a $25.00 registration fee discount for those who chose to go paperless. If you opt not to receive the conference binder you will receive $25 off the registration price. The entire Lilly schedule will be available online for viewing electronically.</p>
<table>
<tr><th>Binder Discount:</th>
<td><input type="checkbox" name="binderdiscount" value="Yes"<?php if ($pgdata['binderdiscount']=='Yes') echo 'checked'; ?>/>I do not need a Lilly Conference binder ($25 discount);</td></tr></table>




<div id="regDiv">
<h3>Registration Options</h3>

<table id="regOption">
<tr>
<th>Conference Registration:</th>
<td><?php echo makeInput('radio','registration',array('Entire Conference'=>'Entire conference - $445 ($395 before October 1)','Thursday Workshop'=>'Thursday pre-conference workshop only')); ?></td></tr>

</table>

<div id="lillyReg">
<table>
<tr>
<th>Arrival date:</th>
<td>
<?php echo makeInput('select','arrivedate',$days); ?>
</td>
</tr>
<tr>
<th>Departure date:</th>
<td>
<?php echo makeInput('select','departdate',$days); ?>
</td>
</tr>
</table>
<table id="accommodationRow">
<tr>
<th>Accommodation:</th>
<td><?php echo makeInput('radio','accommodation',array('Yes'=>'I want lodging at the conference','No'=>'I will provide my own lodging')); ?></td>
</tr>
</table>
<p id="accommodationFull">Conference accommodation is now full. <a href="http://www.units.sincere.edu/lillycon/lodging.shtml" onclick="this.target='_blank';">See what other lodging is available in the area</a> (opens in a new window)</p>
</div>
</div>

<div id="fullAccomod">
<h3> Conference Accommodation</h3>
<p>If you would like a guest to attend the conference with you, please contact us at (513) 529-9266 or <a href="mailto:lillycon@sincere.edu">lillycon@sincere.edu</a> to make arrangements.</p>
<table>
<tr>
<th>Occupancy:</th>
<td>
<?php echo makeInput('radio','occupancy',array('Single'=>'Single occupancy - <span id="lodgingFeeNightly">$</span> per night','Double'=>'Double occupancy with a roommate (two-bed room) - <span id="lodgingFeeNightlyRoommate">$</span> per night, per person')); ?>
<div id="occupancyDouble" class="indented">
<?php echo makeInput('radio','roommate',array(
'Arranged'=>'I have arranged to share a room <span id="arranged_rm">with: '.makeInput('text','rmname',20).' who is planning to arrive on '.makeInput('select','rm_arrive',$days).'</span>',
'Assign'=>'Assign me a roommate <span id="roommateSex"> (I am '.makeInput('select','sex',array('-'=>'Choose...','Female'=>'Female','Male'=>'Male')).')</span>'
)); ?> </div> </td>
</tr>
<tr id="lodging">
<th>Your lodging choices, in order of preference:</th>
<td id="lodgingOptions">
<?php if (function_exists('showLodging')) showLodging(); /* For showing results */ ?> </td>
</tr>
<tr>
<th>Room Options:</th>
<td>
<?php
echo makeInput('checkbox','barrierfree',array('Yes'=>'Barrier-free room'));
echo makeInput('checkbox','tdd',array('Yes'=>'TDD'));
echo makeInput('checkbox','allergies',array('Yes'=>'Special room due to allergies'));
?> </td>
</tr>
</table>
</div>

<div id="thursWorkshops">
<h3>Thursday Pre-Conference Workshop Option:</h3>
<p class="dentin">The following <a href="http://celt.sincere.edu/lillycon/preconference.php" onclick="this.target='_blank';">Thursday pre-conference workshops</a> (info opens in new window) are available. Workshop fees includes materials.</p>
<p id="noWorkshop" class="dentin"><input type="radio" name="workshop" value="None" <?php if ($pgdata['workshop']=='No') {echo 'checked';} ?> >None (you can contact us later to add a workshop to your registration, if you wish)</p>
<?php echo makeInput('radio','workshop',$workshops); ?>
<p id="workshopsincere">sincere faculty and staff are elligible to recieve a small grant to cover the cost of this workshop. See the CELT page on <a href="http://www.units.sincere.edu/celt/smallgrants.php" onclick="this.target='_blank';">Small Grants to Improve Teaching</a> for more information (link opens in a new window).</p>
</div>

<div id="conMeals">
<h3>Conference meals</h3>
<div id="regMeals">
<p>Select the meals that you would like to eat at conference facilities. No changes will be accepted later than one week before the conference starts. <span id="sincereMeals">Because dining room seating is limited, please select <strong>only</strong> the meals you will attend.</span></p>
<p><strong>Meal deal:</strong> Purchase all meals from Friday lunch through Sunday lunch and save <span id="mealDealSavings">$</span>!</p>
<table id="meals">
<tr>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
<th>Cost:</th>
</tr>
<!-- Except for workshop lunches, these "included" DIVs aren't currently used. Just keeping them here in case they're needed for some later deal. -->
<tr id="breakfast">
<td>&nbsp;</td>
<td><div class="mealCheck" id="friBreakfastOpt"><?php echo makeInput('checkbox','friBreakfast',array('Yes'=>'Breakfast')); ?></div><div id="friBreakfastInc" class="mealInclude">Breakfast included</div></td>
<td><div class="mealCheck" id="satBreakfastOpt"><?php echo makeInput('checkbox','satBreakfast',array('Yes'=>'Breakfast')); ?></div><div id="satBreakfastInc" class="mealInclude">Breakfast included</div></td>
<td><div class="mealCheck" id="sunBreakfastOpt"><?php echo makeInput('checkbox','sunBreakfast',array('Yes'=>'Breakfast')); ?></div><div id="sunBreakfastInc" class="mealInclude">Breakfast included</div></td>
<td class="cost"><span id="mealFeeBreakfast">$</span> each</td>
</tr>
<tr id="lunch">
<td><div class="mealCheck" id="thursLunchOpt"><?php echo makeInput('checkbox','thursLunch',array('Yes'=>'Lunch')); ?></div><div id="thursLunchInc" class="mealInclude">Lunch included</div></td>
<td><div class="mealCheck" id="friLunchOpt"><?php echo makeInput('checkbox','friLunch',array('Yes'=>'Lunch')); ?></div><div id="friLunchInc" class="mealInclude">Lunch included</div></td>
<td><div class="mealCheck" id="satLunchOpt"><?php echo makeInput('checkbox','satLunch',array('Yes'=>'Lunch')); ?></div><div id="satLunchInc" class="mealInclude">Lunch included</div></td>
<td><div class="mealCheck" id="sunLunchOpt"><?php echo makeInput('checkbox','sunLunch',array('Yes'=>'Lunch')); ?></div><div id="sunLunchInc" class="mealInclude">Lunch included</div></td>
<td class="cost"><span id="mealFeeLunch">$</span> each</td>
</tr>
<tr id="dinner">
<td><div class="mealCheck" id="thursDinnerOpt"><?php echo makeInput('checkbox','thursDinner',array('Yes'=>'Dinner')); ?></div><div id="thursDinnerInc" class="mealInclude">Dinner included</div></td>
<td><div class="mealCheck" id="friDinnerOpt"><?php echo makeInput('checkbox','friDinner',array('Yes'=>'Dinner')); ?></div><div id="friDinnerInc" class="mealInclude">Dinner included</div></td>
<td><div class="mealCheck" id="satDinnerOpt"><?php echo makeInput('checkbox','satDinner',array('Yes'=>'Dinner')); ?></div><div id="satDinnerInc" class="mealInclude">Dinner included</div></td>
<td>&nbsp;</td>
<td class="cost"><span id="mealFeeDinner">$</span> each</td>
</tr>
</table>
</div>
<div id="mealReqs">
<p><strong>Meal Requirements:</strong>
<?php echo makeInput('select','specialmeal',array('No'=>'No special meal requirements','Vegan'=>'Vegan (no meat or dairy products)','Vegetarian'=>'Vegetarian (same as vegan but includes great desserts)','Lowfat'=>'Low fat (some dairy/some egg)','Diabetic'=>'Diabetic'),'No'); ?></p>
</div>
</div>


<div id="journalOffer">
<h3>Special Journal discounts</h3>
<p>Order a personal one-year (<?php echo date('Y') + 1; ?>) subscription to one of our <cite><a href="http://celt.sincere.edu/journals/" onclick="this.target='_blank';">international, peer-reviewed journals</a></cite> at the <strong>Conference special rate</strong> and save!</p>
<?php
foreach ($regOptions['journalNames'] as $short=>$long) {
echo '<div class="eachJournal">';
echo "<h3><img src='$short.jpg' alt='Cover of the $long'>$long</h3>";
echo "<div id='{$short}NorthAmerica'>";
echo makeInput('radio',"{$short}_subscription",array(
'Individual - Print'=>"Print ({$regOptions['journalIssuesYr'][$short]} iss./yr.): <span id='{$short}PriceIndividual'></span>",
'Individual - Combo'=>"Combination print and electronic: <span id='{$short}PriceCombo'></span>",
));
echo '</div>';
echo makeInput('radio',"{$short}_subscription",array(
'Individual - Electronic'=>"Electronic (includes access to all previous issues): <span id='{$short}PriceElectronic'></span>",
// Not doing inst subscriptions on Lilly form anymore
// 'Institutional - Electronic'=>"Institutional site license - electronic: <span id='{$short}PriceInstitutionElectronic'></span>",
// 'Institutional - Combo'=>"Institutional site license - combination print and electronic: <span id='{$short}PriceInstitutionCombo'></span>",
'No'=>'No thank you'
),'No');
echo '<div class="nofloat"></div></div>';
}
?>
</div>


<h3>Total cost and payment options</h3>
<table><tbody id="lineItems"><tr>
<td>We are very sorry, but there was an error calculating your total. Please reload this page and try again.</td>
</tr></tbody></table>
<p><strong>Total registration cost: $<input name="total" id="total" type="text" value="0" size="8" readonly> (USD)</strong></p>

<div id="payment">
<p>Please select one or more of the following methods to pay for your registration:</p>
<p class="dentin"><input type="checkbox" name="payment_credit" value="Yes" <?php if ($pgdata['payment_credit']=='Yes') echo 'checked'; ?> >Personal or institutional credit card</p>
<div id="payCredit" class="indented">
<p class="dentin"><input type="radio" name="payment_online" value="Yes" <?php if ($pgdata['payment_online']=='Yes') echo 'checked'; ?> >I want to pay the full amount online <strong>(Mastercard, Visa, Discover, or American Express)</strong></p>
<p class="dentin"><input type="radio" name="payment_online" value="No" <?php if ($pgdata['payment_online']=='No') echo 'checked'; ?> >I will call (513)&nbsp;529-8234 with my credit card information <strong>(Mastercard or Visa only)</strong></p>
</div>
<p class="dentin"><input type="checkbox" name="payment_personalCheck" value="Yes" <?php if ($pgdata['payment_personalCheck']=='Yes') echo 'checked'; ?> >Personal check</p>
<p class="dentin end"><input type="checkbox" name="payment_institutionCheck" value="Yes" <?php if ($pgdata['payment_institutionCheck']=='Yes') echo 'checked'; ?> >Institutional check</p>
<div id="payCheck" class="indented">
<p>Please make checks out to Sincere University and send it to:</p>
<address>sincere University<br>
Center for the Enhancement of Learning, Teaching, and University Assessment<br>
551 East High St.<br>
Sydney, Ohio 45241</address>
</div>
</div>



<h3>Print receipt, read refund policy, and submit your registration</h3>
<p>Before you submit this registration form, please read the refund policy below, review your information and <em><strong>print out this page as your registration receipt</strong></em>, and then mark the checkbox below. If you make any changes, this box will uncheck itself as a reminder to print out the final version.</p>

<div class="refund">
<h3>Refund Policy</h3>
<p>If you are unable to attend, someone else may attend in your stead. Otherwise the refund policy is as follows:</p>
<ul>
<li>By September 30, refund 100% minus $50 cancellation fee</li>
<li>Between October 1 and October 31, refund 75% of conference fees or conference fees minus $100, whichever is less</li>
<li>After October 31, refund 0%</li>
</ul>
<p>Make inquiries to:</p>
<address>Sincere University<br>
Center for the Enhancement of Learning, Teaching, and University Assessment</address>
<address>
551 East High St.<br>
Sydney, Ohio 45241<br>
Phone: 513-529-9266<br>
Email: <a href="mailto:lillycon@sincere.edu">lillycon@sincere.edu</a>
</address>
</div>

<p class="dentin end"><strong><input type="checkbox" id="printed" name="printed" value="Yes" <?php if ($pgdata['printed']=='Yes') echo 'checked'; ?> >I have reviewed my information, agree to the refund policy above, and printed this page as my receipt.</strong></p>
<p>
<input type="hidden" name="full_receipt">
<input type="hidden" name="ject_cost" value="0">
<input type="hidden" name="lcj_cost" value="0">
<input type="hidden" name="jctl_cost" value="0">
<input type="hidden" name="earlybird" value="<?php if (date('n')>=10 && date('j')>1) echo '0'; else echo $earlyBirdAmount; ?>">
<input type="hidden" name="breakdown_conference_fee" value="0">
<input type="hidden" name="breakdown_workshop" value="0">
<input type="hidden" name="binder_discount" id = "binder_discount" value = "0">
<input type="hidden" name="breakdown_wed_supplement" value="0">
<input type="hidden" name="breakdown_thurs_supplement" value="0">
<input type="hidden" name="breakdown_lodging" value="0">
<input type="hidden" name="breakdown_meals" value="0">
<input type="hidden" name="breakdown_subtotal" value="0">
<input type="hidden" name="breakdown_retiree_grad_discount" value="0">
</p>

learning curve
03-28-2012, 07:37 PM
and here is the javascript processing page attached as a txt file(due to size.)

learning curve
03-28-2012, 07:45 PM
Some how the php form didn't make it. Here it is:

Old Pedant
03-28-2012, 09:32 PM
Well, neither of those files is relevant to how you would modify the PHP code to do what you stated:


I need the initial checkbox to put a yes or no in a field in the mysql table for a field called binderdiscount. yes=check box is checked

then I need the hidden field to input either a 25 or a zero in a table field called binder_discount based on the yes/no check.

You don't show the code that process the <form> data to store it into the MySQL DB.

It would be trivial to modify the PHP code to do just that, but I'd want to see the relevant code first.

However...

Given that your page depends SO HEAVILY on JavaScript, it's clear that you don't really care about users who have JavaScript disabled: your page simply won't work for them.

So in that case, let's go back to your original question.

I don't see why your existing code doesn't work.



<input type="checkbox" name="binderdiscount" value="Yes" <?php /*?><?php if ($pgdata['binderdiscount']=='Yes') echo 'checked'; ?><?php */?> />I do not need a book;</td>

<input type="hidden" name="binder_discount" value="0">
...
if(getControlValue(form.binderdiscount)==="Yes") {form.binder_discount.value=25;}

...
function getControlValue(control) {
var i, output;
output = null;
if (control.type==="checkbox") {
if (control.checked) {
output = control.value;
}
...
return output;
}

Looks perfectly okay to me.

Maybe you just need to do some JavaScript debugging?

Use a good JS debugger, set a breakpoint on that line (the first "if" in above code) and see what the values are and what is happening.

Mishu
03-29-2012, 12:28 AM
I need the initial checkbox to put a yes or no in a field in the mysql table for a field called binderdiscount. yes=check box is checked

then I need the hidden field to input either a 25 or a zero in a table field called binder_discount based on the yes/no check.

The first is working (Yes/No), but the second is not .

If the above is not working then you need to post the php code that loads the form data into the database. Have a look at the url in the action attribute of your form and post the code in that url.

Old Pedant
03-29-2012, 12:36 AM
Actually, an easy way to figure out if the JS is working would be to TEMPORARILY change the hidden form field to <input type="text">.

If this is all happening when the form is submitted, then you might also need to change the <form> tag to suppress the submittal, so you can actually see the (now non-hidden) field's value.

Old Pedant
03-29-2012, 12:39 AM
Yeah, change the <input type="hidden"> to <input type="text"> and then in the JS code change this:


form.onsubmit = function() {
if (formValid()) {
this.action = 'process.php';
return true;
} else {
return false;
}
};

change the return true to return false temporarily. Now the form won't submit, but the call to formValid() will still be made and, presumably, you will be able to see the now non-hidden field be changed, if appropriate.

Mishu
03-29-2012, 12:59 AM
I need the initial checkbox to put a yes or no in a field in the mysql table for a field called binderdiscount. yes=check box is checked

then I need the hidden field to input either a 25 or a zero in a table field called binder_discount based on the yes/no check.

The first is working (Yes/No), but the second is not .

If the above is not working then you need to post the php code that loads the form data into the database. Have a look at the url in the action attribute of your form and post the code in that url.

But before you do that, you can check if the php script has received the form data correctly which is what you should normally do when writing any form processing code.

Let's assume your form is using POST to send the data. Then at the top of your php form processing script add



echo $_POST['binder_discount'];
die();You should also echo or print out in one go any other sent form data to make sure it has been received correctly before continuing to code the rest of thescript. If any of the sent form data is missing or not correct, then go back to your html form and javascript to debug the problem.

Old Pedant
03-29-2012, 01:13 AM
So does PHP have a convenient way to dump out all posted values???

In ASP I can do this:


<%
CONST DBG = True
If DBG Then
For Each name In Request.Form
Response.Write name & "::" & Request.Form(name) & "<br/>" & vbNewLine
Next
Response.Write "<hr/>"
End If
%>

And I often put just that at the top of my pages. And then I can turn the "dump" on and off by just chnaging the DBG to True or False.

Mishu
03-29-2012, 01:50 AM
So does PHP have a convenient way to dump out all posted values???



Yes, it does. There are a few 1 line of code options or at most 3 lines if you want to customise the output.

I don't see how your asp code is relevent to anything because the op is using php.

Old Pedant
03-29-2012, 02:19 AM
> I don't see how your asp code is relevent to anything because the op is using php

It isn't. I just was asking if there is a PHP equivalent. Since there is, I'd recommend he use that. Might as well see all the form fields that are being posted, instead of looking at one at a time.

Mishu
03-29-2012, 02:23 AM
Might as well see all the form fields that are being posted, instead of looking at one at a time.

That's what I normally do (display all the form data) but I'm sure the op is capable of deciding for themselves how they want inspect the sent form data.

If you're at the stage of having just 1 problem input then it might not be necessary to display all 5, 10, 20, 50 or however many you might have in a particular situation.

learning curve
03-29-2012, 04:19 PM
I have looked and there are apparently three pages that process the form. One mainly deals with accessing the database and the other one deals with some other items that I don't think are related. The person who wrote this tried to build universal pages of scripts that could be used by different forms. Here is the code that belongs to the main form processing. I gotta say though that this has become a nightmare with all this script in all these pages. I think I am going to go back to trying to do it in php. I feel much more comfortable there. I do appreciate all the help and I have learned some new things. Hopefully I can become proficient enough in javascript to eventually return the favor.


<?php
require '/www/universal.php';
require '/www/submission_functions.php';
require '/www/conference/main_include.php';

function journalData($which,$newID) {
$nextYear = date('Y')+1;
$subscription = array(
'contact_name'=>$_POST['lastname'].', '.$_POST['firstname'],
'department'=>$_POST['department'],
'institution'=>$_POST['institution'],
'address'=>$_POST['address'],
'city'=>$_POST['city'],
'state'=>$_POST['state'],
'zip'=>$_POST['zip'],
'country'=>$_POST['country'],
'phone'=>$_POST['office_phone'],
'email'=>$_POST['email'],
'order'=>$_POST[$which.'_subscription']." x 1 year ($nextYear) @ Lilly Conference rate = ".$_POST[$which.'_cost'],
'subscription_length'=>'1',
'total'=>$_POST[$which.'_cost'],
'payment_method'=>'Lilly registration',
'lilly_purchase'=>$newID // Referenced for CashNet later
);
switch ($_POST[$which.'_subscription']) {
case 'Individual - Print':
$subscription['type'] = 'individual_print';
break;
case 'Individual - Electronic':
$subscription['type'] = 'individual_electronic';
break;
case'Individual - Combo':
$subscription['type'] = 'institution_combo';
break;
case'Institutional - Electronic':
$subscription['type'] = 'institution_electronic';
break;
case'Institutional - Combo':
$subscription['type'] = 'institution_combo';
break;
}
return $subscription;
}

if ($_POST['sincere']=='Yes') $_POST['institution'] = 'Sincere University';
if ($_POST['chooseCountry']!='Other:') $_POST['country'] = $_POST['chooseCountry'];
unset($_POST['chooseCountry']);

$_POST['user_agent'] = $_SERVER['HTTP_USER_AGENT'];

// Save row ID for later updates
$newID = submitForm($_POST,'lillycon',"registration$thisyear");
$_POST['id'] = $newID;
setcookie('last_id',$newID,0, '/lillycon/registration/','celt.sincere.edu');

$allJournals = array('ject'=>'5612CW', 'lcj'=>'9151CW', 'jctl'=>'9113CW');
foreach ($allJournals as $journal=>$itemCode) {
if ($_POST[$journal.'_subscription']!='No' && $_POST[$journal.'_subscription']!='') {
submitForm(journalData($journal,$newID), $journal, 'orders', 'NOW()', 'received', 'ip', FALSE);
}
}

if (!isset($_POST['payment_online']) || $_POST['payment_online']!='Yes') {
header('Location: http://celt.sincere.edu/conference/registration/thankyou.php');
} else {
$lilly = $_POST['total'];
$journalCosts = array();
foreach ($allJournals as $journal=>$itemCode) {
$journalCosts[$journal] = $_POST[$journal.'_cost'];
$lilly -= $_POST[$journal.'_cost'];
}
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

echo <<<EndOfText
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Lilly Conference Registration</title>
<style type="text/css">
@import url(http://www.sincere.edu/conference/style.css);
</style>
</head>

<body>
EndOfText;

$includeFile = file_get_contents("http://www.sincere.edu/conference/lillyheader.php");
echo $includeFile;
echo <<<EndOfText
<h1>Lilly Conference Registration</h1>
<h2>Step 2: Make online payment</h2>
<p>You will now be taken to a secure payment site to enter your credit card information and complete your online payment. If you have any problems or questions, please contact us at 513-529-1234 or <a href="mailto:conference@sincere.edu">conference@sincere.edu</a></p>
<form method="post" action="https://commerce.cashnet.com/sincereCONFP">
<p><input type='hidden' name='itemcode1' value='9042CW'><input type='hidden' name='amount1' value='$lilly'>
EndOfText;

$newItemCode = 2;
foreach ($allJournals as $journal=>$itemCode) {
if ($journalCosts[$journal] != 0) {
echo "<input type='hidden' name='itemcode$newItemCode' value='$itemCode'><input type='hidden' name='amount$newItemCode' value='{$journalCosts[$journal]}'>";
$newItemCode++;
}
}

echo <<<EndOfText
<input type="hidden" name="ref1type1" value="ID">
<input type="hidden" name="ref1val1" value="$newID">
<input type="hidden" name="ref2type1" value="STORE">
<input type="hidden" name="ref2val1" value="lillycon">
<input type="hidden" name="ref3type1" value="NAMEFIRST">
<input type="hidden" name="ref3val1" value="$firstname">
<input type="hidden" name="ref4type1" value="NAMELAST">
<input type="hidden" name="ref4val1" value="$lastname">

<input type="submit" value="Make online payment">
</p>
</form>
EndOfText;

$ncludeFile = file_get_contents("http://www.sincere.edu/conference/lillyfooter.php");
echo $includeFile;

echo '</body></html>';
}
?>

learning curve
03-29-2012, 04:32 PM
Old Pedant,
I just tried your suggestion of changing the hidden field to text and the output to false and that showed the correct answer of 25 when the checkbox was checked and 0 when it wasn't. So clearly the script is working and the problem is in the output. I want to thank you for this brilliant idea - I can't believe i didn't think of trying this earlier - duh. So, I have a clue now.

learning curve
03-29-2012, 06:50 PM
and now it is working... ?!? I am not sure why it just starting working. I want to thank all of you for the help and suggestions. I can now concentrate more on learning how to build fire rather than put out someone else's fire.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum