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

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
  $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'];
  $data['listCusName'] =  $result ;
  //Send all parameters to view section

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

Thank you your kind. ^^

Try escaping your second double quotes " like this:

$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:

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


Good luck with it.

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

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

I already change as your comment

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


Theme © iAndrew 2016 - Forum software by © MyBB