Posted on Tuesday, 8th July 2008 by Lee
I have recently become quite fond of using jQuery as my javascript library in conjunction with my CodeIgniter PHP projects. In PHP, I often name my form fields in the format of...
model_name[attribute_name] For Example... contact[first_name] contact[last_name] etc.
I do this so that I can easily retrieve all the attributes for each model and then quickly store them to the database using CodeIgniter's database helper functions. For example, taking advantage of the Active Record Insert Syntax, I might write PHP code like this...
-
...
-
$contact = $_POST['contact'];
-
$this->db->insert("contacts", $contact);
-
...
When I started to use jQuery to do some client side javascript validation on my forms it was not immediately obvious to me that I needed to escape the square brackets in the jQuery calls.
-
<script language="javascript">
-
$(document).ready(function() {
-
alert($('contact\\[first_name\\]).val());
-
});
-
</script>
Due to javascript's encoding, you need to use double slashes - not just a single slash - to escape the square brackets.
Posted in CodeIgniter, PHP, Web Development | Comments (2)











July 10th, 2008 at 1:41 am
Wow, it just took an half hour to find that. I’ve just encountered the same problem with CakePHP. Many thanks for your helping post.
P.
August 11th, 2008 at 11:41 am
Thanks for this!