views:

387

answers:

2

Hi ,

i am having a ajax post like

  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;

 alert(data.choices);//alerts the choices as object

   $(""+<?php echo $form->input('field', array('type' => 'radio','legend'=>$r['Attribute']['label'],'separator' => '--separator--',
      'options' => array() ));?>+"").appendTo("#"+<?=$r['Attribute']['id'];?>);

In the last line where i am creating a Radio button inside JQUery which shows me the legend correclty but i am trying to use the data.choices which is a JQuery variable inside PHP array i.e. in 'options'=> array() How to do so ???

+4  A: 

You are confusing server side language (PHP) with client side language (JavaScript/jQuery). When your server processes the PHP code it totally ignores JavaScript code, it's just a normal text for it. Then after request was processed and was send to the client, his browser starts to interpret the JavaScript but this time it doesn't have access to PHP variables (because the response was a HTML generated from PHP code).

I don't think I fully understand your question, if you need your code to be processed by PHP you need to use AJAX to call it from JavaScript and then process the results. If you want to generate this radio button while generating site contents you should only rely on PHP variables and use them to create your button.

RaYell
He may not be wrong if his javascript comes from a php file.
yoda
Even if JS is generated by PHP it isn't interpreted until PHP finishes his work and send it back to the browser, so that doesn't change anything.
RaYell
+1  A: 

Just to clarify for you, your lines of code:

$(""+<?php echo $form->input('field', 
                             array('type' => 'radio',
                                   'legend'=>$r['Attribute']['label'],
                                   'separator' => '--separator--',                    
                                   'options' => array() ));?>
  +"").appendTo("#"+<?=$r['Attribute']['id'];?>);

If the PHP echoed out "text1", and "text2" respectively. you would have

$("" + text1 + "").appendTo("#"+text2);

Would fail because, these aren't variable names. Do a view-source on the page to see exactly what is being downloaded to the browser.

Also, consider setting the PHP value in a hidden variable and referencing that from JavaScript. Mixing server and client side code is often very confusing. Something known as tag soup.

James Wiseman