2

I am trying to send values to an mysql database using ajax from a form. I am selecting the value by taking the parent article of the form and then taking the child element with id="email" as you can see here...

var email = $(this).parent("article").children("#email").val() //gets the user's email

However when I send the data onto the php file for uploading to the mysql database it seems to be doing something wrong, and instead of the value typed in being stored a function (shown below) is being stored... What is going on here!?

function (a) {var c,d,e,g=this[0];{if(!!arguments....
5
  • So have you checked the email variable value? Have you tried to see what's actually sent with firebug? Commented Jul 11, 2012 at 20:34
  • is the value proper before you send it off? Commented Jul 11, 2012 at 20:35
  • Your email field has an id, so just use $('#email').val(), or, because you really don't need to use jQuery for every single thing, document.getElementById('email').value. Commented Jul 11, 2012 at 20:36
  • Technically, you cannot supose id is unique. Of course, you should make ids unique, but maybe he has an awful case where it is necessary. Just saying... jsfiddle.net/NmMGW Commented Jul 11, 2012 at 20:45
  • 1
    Sorry to confuse you all... email is only an id at the moment, however it will be a class and there will be multiple forms on the page which explains the need for the .parent() .child() setup! Commented Jul 11, 2012 at 20:51

2 Answers 2

1

you can try find() method:

The .children() method differs from .find() in that .children() only travels a single level down the DOM tree while .find() can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well.

var email = $(this).parent("article").find("#email").val() 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for that! I will mark you as correct as soon as I'm allowed :)
Actually all that selector line can be replaced with $('#email')
Just out of interest is there any way of traversing up like this to the article? Or will parent do the job?
@simonthumper as you are selecting an element by id name. $('#mail') is fine, but as an alternative to parent(), you can use closest() method.
@zerkms actually no it can't because at some point it will become find(".email") As i want to have this form on each article on my site...
1

Seems a bit confusing. You're using #email, which implies an id, of which you can only have one on the page. jQuery will recognize the hashtag and default to using the native "getElementById" browser method.

So this:

var email = $(this).parent("article").children("#email").val()

Can be converted to this:

var email = $("#email").val()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.