Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    ajax - Get value in field before submit

    I want to got to page with AJAX and get the value that is in the field because i want to get the value and add my value also.

    this is what i have so far:

    $.ajax({
    type: "POST",
    url: "file.php",
    async: false,
    beforeSend: function(msg){
    var i = $(msg).find('[name*=user]').attr('value');
    console.log(i);

    },
    success: function(msg){
    alert("sucess");
    }
    });

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    What exactly do you want? You already got the value there in your beforeSend. You want to add your own value? Just do it then.

    If you want me to help, you have to be a bit more specific about what exactly is causing you trouble.

    On a side note: The parameter msg has to hold the selector string for your container DOM element. Why on earth would you call that "msg"?

    Also, do you have any good reason to use synchronous AJAX?

    Oh, interesting. Just when I wrote "synchronous AJAX" I threw up a little.

    Again.

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well there is a page which is file.php.

    There is a field named user. The field has a value which is the username.
    I want to get that value and add my value to the end, but if there is no value I just want to post my own.
    Last edited by 510carlos; 07-26-2009 at 06:04 AM.

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Ok, that's not all that hard.

    Within the beforeSend, you can try something like this:
    Code:
    var field = $(msg).find('[name*=user]').get(0);
    field.value += field.value ? "your value if username exists" : "your value if username doesn't exist";
    Also, please answer my questions.

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by venegal View Post
    What exactly do you want? You already got the value there in your beforeSend. You want to add your own value? Just do it then.

    If you want me to help, you have to be a bit more specific about what exactly is causing you trouble.

    On a side note: The parameter msg has to hold the selector string for your container DOM element. Why on earth would you call that "msg"?

    Also, do you have any good reason to use synchronous AJAX?

    Oh, interesting. Just when I wrote "synchronous AJAX" I threw up a little.

    Again.
    I think I answered that question.
    I really don't know because i am new to the jquery scene, I was just trying to get with trial an error.
    I also don't know why i used synchronous, I guess i'll switch it.

    It gave me an error in firebug. "Field is underdefiened"

    Code:
    	$.ajax({
      type: "POST",
       url: "file.php",
       dataType: "html",
       beforeSend: function(msg){ 
    		var field = $(msg).find('[name*=user]').get(0);
    		field.value += field.value ? "your value if username exists" : "your value if username doesn't exist";
    		alert(field);
    	},
     });
    Last edited by 510carlos; 07-26-2009 at 05:33 PM.

  • #6
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Can you post the whole code for this?

    What is triggering your ajax call? A form submit event? Why are you using $.ajax rather than $.get or $.post?

    I think you're making this harder than it is. Get the value of your input field first (and give your field an ID attribute, save the browser some work), use that to create your parameters to send to the processing page, and fire a nice, normal $.get request without all this mucking about trying to modify values in an XMLHTTP request on the fly.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •