开发者

Setting the value of a checkbox based on what is selected in a list

I am working in the confines of a CMS system, which defines certain fields which can be used to make forms for use within the application in PHP.

The list function has the signature:

function inputBasicList ($id,$value = "",$list = array(), $displayName = NULL, $displayLabel = true)

I use it thusly:

$theinput = new inputBasicList("type",$therecord["paymenttype"],array("Cash"=>"cash","Credit"=>"credit"), "Payment Type");

Likewise, there is a checkbox, which has the signature:

function inputCheckbox($id,$value = false, $displayName = NULL, $disabled = false, $displayLabel = true)

I use it thusly

$theinput = new inputCheckbox("paid", $therecord["paid"], "Paid");

What I would like to do, is if the list is set to credit instead of the default cash, to automatically s开发者_如何学运维et the checkbox to true/checked.

I don´t think the CMS system allows a way to do this using any built in functions, and am wary of adding any javascript.

Is such a thing possible with just PHP?

Otherwise, how complicated would the javascript have to be to do such a thing?

edit:

The generated HTML from the phpBMS forms

<p class="big"><label for="type" class="important">Payment Type</label>
<br />
<select name="type" id="type"  class="important" > 
<option value="cash"  >Cash</option>
<option value="credit"  >Credit</option>
</select>
</p>
<p class="big">
<input type="checkbox" id="paid" name="paid" value="1" class="radiochecks"  /> 
<label id="paidLabel" for="paid" >Paid</label>
</p>


It's not possible to do such thing with PHP only, because PHP is run on your server. You need some code that is run on the client.

I believe that the first paramater $id is used as id attribute for the elements? If I'm wrong correct me. If so you can do the following using the jQuery JavaScript Library:

jQuery(function($){
  $('#type').change(function(){
    if ($(this).val() == "credit") {
      $('#paid').attr('checked','checked');
    } else {
      $('#paid').removeAttr('checked');
    }
  });
});

UDPATE BMS is using Mootools, the JavaScript should like like this to work in mootools:

window.addEvent('domready', function(){
  $('type').addEvent('change',function(){
    if($(this).get('value') == 'credit') {
      $('paid').set('checked','checked');
    } else {
      $('paid').removeProperty('checked');
    }
  });
});

I would recommend using the mootools version of this snippet, but just for your interest, if you want to install jQuery, you can add the jquery.js into phpbms/common/javascript. Then you can edit phpbms/header.php to include this:

after the last $tempjsarray[] add:

$tempjsarray[] = "common/javascript/jquery.js";

then after $phpbms->showJsIncludes(); you need to include this, so jQuery works without problems besides mootools:

echo '<script type="text/javascript">jQuery.noConflict();</script>';

If this doesn't work, you should post what the html output looks like.


It should be fairly easy in the javascript . You can attach an event listener on the onchange function of the list and set the value of checkbox in that function .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜