views:

48

answers:

2

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 set 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>
A: 

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 .

jmhowwala
It is not a standard HTML list, but a custom list defined by the CMS. I don´t think I can attach a listener.
Jacob
A: 

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.

jigfox
Hi, yes $id refers to the field name.I can´t simply import jquery into an existing CMS system though....
Jacob
why can't you do that? I've worked with plenty of CMS and they all allow to use any javascript you want to! What CMS are you using?
jigfox
its a BMS more than a CMS, phpBMS. here is the template for a form: http://www.phpbms.org/browser/trunk/phpbms/modules/sample/sampletable_addedit.php I don´t think I could just include jquery easily
Jacob
Hi, thanks for your answer, i really appreciate it. This may seem like a stupid question, but where exactly should I place the moo tools example you provided? There does not really seem to be anywhere to add JS code template for a form, and I am weary of adding in a <script> section....
Jacob
you can create your own `some_name.js` and add as I described above for the jQuery thing. or you write the code between `<script>` tags, there is nothing to be weary about.
jigfox
I maid my own checkpaid.js, and placed it in modules/mymodule/javascript/checkpaid.js - then, as per the template example, I added it with $phpbms->jsIncludes[] = "modules/mymodule/javascript/checkpaid.js"; - however it does not seem to do anything. No errors at all, but no functionality either. - It seem to definitely be including it, as I see it is requessted with GET when the page loads
Jacob
then i need to see your generated inputs. I mean the html code generated by `inputBasicList()` and `inputCheckbox()`
jigfox
Hi Jens, I have edited my answer to include the generated html
Jacob
than something is not loading correctly, are you sure your javascript is loaded? try to put `alert("loaded");` at the end of the javascript file you created. I've setup a small working demo: http://jsfiddle.net/JwCRm/
jigfox
Hmm, the alert does not come up, although in firebug I see it GET the javascript file, and it appears in the list of scripts I can debug with firefox. It never actually seems to be executed however.
Jacob
here is an example of a working js include file that ships with the core module: http://phpbms.org/browser/trunk/modules/bms/javascript/client.js?rev=51 - maybe I have to define a function first?
Jacob
that happens if there is a syntax error in the script. what exactly does you script look like?
jigfox
its your example, verbatim paced in a file called checkpaid.js, and then included in my addedit form
Jacob
I also tried putting the checkpaid.js file in common/javascript and adding it to tempjsarray, and it makes no difference. No errors are reported, but nothing happens, not even the alert()
Jacob
you must be doing something wrong, because the linked example works just fine. are you sure the file is loaded correctly? try to access this file by entering the url to the file manually.
jigfox
I can access the file through the address bar fine, and it shows up in firebug as being requested and as a script in the script panel. Is it perhaps the way I am including it...maybe it has to be as a function or something?
Jacob
making it into a function made no difference, nor did calling it with $phpbms->onload[] = "checkPaid()";
Jacob
I´ve placed my form code and the js file on pastebin: http://pastebin.com/as5JXZAw
Jacob
can you provide a link where I can take a look at this?
jigfox
you have spaces in your folder names, you should avoid them.
jigfox
you don't need to wrap it in a function or put it to the onload!
jigfox
surely that is not the cause as it has been working fine so far...i.e. other files I include work fine, and it is a bit late to change it now...plus, the checkpaid.js does show up as being included in firebug
Jacob
sorry, can't help you without a link to your page!
jigfox
I only put it in a function and tried onload after it was not working the other way. I don´t think I can give a link to my page as I am behind a router using NAT without accesses to change things.
Jacob
If you look at the way other javascript files are included, they are all functions....is that not relevant?
Jacob
then i don't how to help you any further, sorry. My Code is working.
jigfox
It works with mootools by itself, but not within phpBMS. I guess I have to try and find out how to do this admidst the poor documentation and compelte lack of support. sigh. Thanks for your assistance so far though.
Jacob
If you had time, I wonder if you could take a look at this problem as well: http://stackoverflow.com/questions/3160433/tokenizing-a-metafield - its quite stumping me and apparently everyone i ask.
Jacob
OK, it doesn´t work because: window.addEvent is not a functionhttp://localhost/1/phpbms/modules/micro%20hospitality/javascript/checkpaid.jsLine 1
Jacob