Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

jquery - Fix the options in Cakephp Form FIeld of type radio button

i am having Cakephp code like

   <?php foreach ($viewfields as $r): 

                          if($r['Attribute']['type']=='radio')
            {

?>

   <script type="text/javascript">
    jQuery.noConflict();
   jQuery(document).ready(function($){


    $("#"+<?=$r['Attribute']['id'];?>).each(function() { 

    type= "<?=$r['Attribute']['type'];?>";
    attribute_id="<?=$r['Attribute']['id'];?>";
if(type=="radio")
{
   var ht = $.ajax({
   type: "GET",
   url: "http://localhost/FormBuilder/index.php/forms/viewChoices/"+attribute_id,
   async: false
   }).responseText;



       var myObject = eval('(' + ht + ')');

 var data = myObject;var j=0;
 $.map(data.choices, function(i){ j++; 
 alert(i.choice);
     return i.choice;});  
     }//type==radio
        });//each
 });//jquery

 </script>


<?php


         echo $form->input('field', array(
        'type' => 'radio','legend'=>$r['Attribute']['label'],
       'separator' => '--separator--',
        'options' => array() 
   ));



 }//if php type == radio

 endforeach; ?>

alert(i.choice);/alerts me the choices for the label gender as male and female and for the label "experience as yes and no..

How to place male and female in the 'options' => array() ..please suggest me...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The choices were fetched by the client (ie. JavaScript running on the browser), so you cannot use them in your server-side PHP code (ie. CakePHP FormHelper).

The PHP code above, once run, generates a page with a JavaScript block. This is sent to the a user's browser. Once the page loads up, it requests a list of choices from the server.

It is too late at this point to edit the page that was already sent to the client, unless you use even more JavaScript.

You might want to consider fetching these choices in your CakePHP controller action and then passing them in the view. This will allow you to also use them in the view, in FormHelper, before the page is sent to the client.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...