tags:

views:

193

answers:

2

Hi,

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...

A: 

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.

deizel
A: 

You can try this.

'options' => array( 'Male'=>'Male', 'Female'=>'Female' )
Bindas