Welcome Guest, Not a member yet? Register   Sign In
Can someone help about form_dropdown() and onChange() in CodeIgniter
#1

[eluser]Unknown[/eluser]
Hi everybody!! I a newbie for CodeIgniter. I would like to ask all you guys.

I have to table in databases, tb_customer and tb_customer_site. Firstly, I query customer name to show in first drop-down list (as below code). I want to do that once I select customer name, system should query site name for that customer and show in the second drop-down list without refresh page. I understand that I can use onChange() in 4th parameter of form_dropdown(). I already test by alerting some text but It dose not work.

##My controller
Code:
//Query CUS_CODE and CUS_NAME from TB_CUSTOMER
  $query = $this->db->get('tb_customer');
  $result = array();
  
  //Assign query result to dropdown list
   $result['0'] = '- - - Select One - - -';
   foreach($query->result_array() as $row):
    $result[$row['CUS_CODE']] = $row['CUS_NAME'];
   endforeach;
  $data['listCusName'] =  $result ;
  
  //Send all parameters to view section
  $this->load->view('mrtg_frm',$data);

##My view
Code:
<?
  $js = 'onChange="alert("Hello World");"';
  echo form_dropdown('mrtgCusName', $listCusName, '0', $js);  
  ?>

Thank you your kind. ^^
#2

[eluser]boltsabre[/eluser]
Try escaping your second double quotes " like this:

Code:
$js = 'onChange="alert(\\\\"Hello World\");"';
//Okay, im getting 2 backslashes before Hello, due to the escaping mechanism of this chat
// it should only be one backslash

Without escaping it you are basically telling it that it ends here:
Code:
onChange="alert("

That MAY fix your problem, I'm at work and don't have an installation of CI here to test it on.

Also, did you get any JS errors? Or other errors?
I use firefox for production as I like it's range of developer tools (plugins), use this one (see link below) to easily see JS errors. click on the cross to see what the error is.

Once you have it installed the very top right hand corner it's tool bar has 3 ticks or crosses (or a combination, but always 3), the very right one is for JS... if you have a tick you have no obviously JS code errors, but if you get a cross then yep, something's wrong.

Obviously you don't have to use FF or it's extensions, but it's my preference. Another great one if Firebug if you're not already using it (you can google that link yourself if you want it).

https://addons.mozilla.org/en-US/firefox...developer/

Good luck with it.
#3

[eluser]Unknown[/eluser]
Thank for your prompt reply. Yes!! you are correct that there is something wrong on my JS because there is red crosses on the top right. But as I check is said ""

Code:
Error: SyntaxError: illegal character
Source File: http://localhost/CodeIgniter/mrtg/frm
Line: 1, Column: 6
Source Code:
alert(\

I already change as your comment

Code:
$js = 'id="mrtgCusName" onChange="alert(\\"Hello World\");"';
//There is only one blackslash before double quote. :)

Thank!




Theme © iAndrew 2016 - Forum software by © MyBB