javascript problem

hi, friends

i have _form.php view which contain javascript function. in that function, i embeded php code into it,then run and the result work as well as i want. but when i separated that function to anther file, the php script is not run, but display only Remove link.

below is my javascript fucntion:




  function addProduct() {

 var ni = document.getElementById('divProduct');

 var numi = document.getElementById('countLastInput');


 var num = (document.getElementById('countLastInput').value -1)+ 2;


 numi.value = num;


 var newdiv = document.createElement('div');


 var divIdName = num;


 newdiv.setAttribute('id',divIdName);


 newdiv.innerHTML = <?php echo $form->dropDownList($orderdetail,'product_id',CHtml::listData(Products::model()->findAll(),'product_id','product_name'),

array('empty' => '--- Choose---','name'=>'productorder[]')); ?>+<?php echo $form->textField($orderdetail,'qty', array('name'=>'qtyorder[]')); ?>+'<a href="#" onclick="removePhoto('+divIdName+')">Remove</a>';


 ni.appendChild(newdiv);


}



Plz help me!!! :(




function addProduct() {

   var ni = document.getElementById('divProduct');

   var numi = document.getElementById('countLastInput');


   var num = (document.getElementById('countLastInput').value -1)+ 2;


   numi.value = num;


   var newdiv = document.createElement('div');


   var divIdName = num;


   newdiv.setAttribute('id',divIdName);


   var html = '<?php echo $form->dropDownList($orderdetail,'product_id',CHtml::listData(Products::model()->findAll(),'product_id','product_name'),

array('empty' => '--- Choose---','name'=>'productorder[]')); ?>';


   html+='<?php echo $form->textField($orderdetail,'qty', array('name'=>'qtyorder[]')); ?>';


   html+='<a href="#" onclick="removePhoto(\''+divIdName+'\')">Remove</a>';


   newdiv.innerHTML=html;

   ni.appendChild(newdiv);


}



@phosItc

Hi

if you have a change functions location and you have a put on .js file than it’s not work (php code) or if it is .php file than it’s works.

Thanks

Nobody will understand a word from what you’ve just said.

He’s just saying that you can’t have php code in a .js file.

So don’t move your javascript function that contains php code into a .js file.

Or if you do, split it into two parts, one that contains the php code and one that contains the pure javascript.

@zilles - Thanks :)

thanks @twisted1919,for all your reply. I have followed your solution but it didn’t work and got error message like this “missing ; before statement”. I thought the problem is where Textfield. I can’t fix it now. Do you have any idea?

Thanks, otherwise if i don’t move that javascript function to another file(myjs.js), my dialog open, yet doesn’t display nothing. i can see only a title of dialog, and i got error message that “NetworkError: 500 PHP Error - http://localhost:180/mobile_delivery_admin_2012_02_11/index.php/order/create”. I have used CJuiDialog. Below is dialog function:




 function addProduct()

 {

    

 	$('#dialogProduct').dialog('open');

 	<?php echo CHtml::ajax(array(

             'url'=>array('/order/create'),

             'data'=> "js:$(this).serialize()",

             'type'=>'post',

             'dataType'=>'json',

             'success'=>"function(data)

             {

                 if (data.status == 'failure')

                 {

                     $('#dialogProduct div.divForForm').html(data.div);

                           // Here is the trick: on submit-> once again this function!

                     $('#dialogProduct div.divForForm form').submit(addProduct);

                 }

                 else

                 {

                     $('#dialogProduct div.divForForm').html(data.div);

                     setTimeout(\"$('#dialogProduct').dialog('close') \",3000);

                 }

  

             } ",

             ));

        ?> 

     return false; 

  

 }



where is my wrong? please help!

On what line do you get the error, and what’s on the line with the error ?

For your dialog issue, prefix your success function with js: too.

also, i see you have




$('#dialogProduct div.divForForm form').submit(addProduct);



What do you try to achieve using that snippet of code ? When form submits you try to call addProduct() again ?

if so, you need to bind the submit event, like:




$('#dialogProduct div.divForForm form').submit(function(){

   addProduct();

   return false;

});



Another thing, in your ajax call, i see you have:




'data'=> "js:$(this).serialize()",



Do you have any idea who is $(this) in an ajax call ?

I assume you try to serialize the form from the dialog ?

If so, you need to :




'data'=> "js:$('#dialogProduct div.divForForm form').serialize()",



Now, a word of advice, the jquery ui dialog allows you to bind actions when the buttons are pressed or when the dialog opens/closes/etc.

If i where you, i would define a dialog with two buttons, one for save one for cancel and in the dialog i would have the form . On the save button i would bind the ajax call to serialize the form from within the dialog and send it to server. Based on the response from the server, i would close the dialog or show errors or whatever.You don’t need all the spaghetti code you’ve created, it’s useless and full of errors anyway.

The code would look would be like:




$(".dialog-selector").dialog({ 

   buttons: { 

      "Save": function() { 

         $.post('/order/create',$('#dialogProduct div.divForForm form').serialize(),function(data){

            if (data.status == 'failure'){

               alert('you have an error');

            }else{

               alert('the product has been saved...');

               $(this).dialog("close");

            }

         },"json");

      },

      "Close": function(){

        $(this).dialog("close");

      } 

   } 

});

$('some-add-product-selector').click(function(){

   $(".dialog-selector").dialog("open");

});



Of course you’ll have to make use of the JUI Dialog to achieve this, but the documentation is good enough.

Have fun learning jQuery :)

Thanks for your advice,twisted1919. Now i can fix it.

please can anyone give me the detail description on how to multiply the value of two fields and get its output automatically in the third field.

For example:

I have Quantity, Cost and Amount fields:

if i enter 2 -> Quantity

       15000 -&gt; Cost

then i should get the output 30,000 in amount.

please do reply me.