0

I am trying to retrieve data from my form when I post(submit) the data. But I have some ignorance about ajax with jquery.

Today, my code looks like this:

$(function() {
    $(".apply_up").submit(function(e){    
        $.post("controller/ajax.php", function(data) {
            alert("Data Loaded: " + data);
        }); 
    });
});

And here's my HTML:

<form name="Apply" class="apply_up" method="Post">
    <input type="image" src="img/t_up.png" id="ajax" name="t_up" value="1 ">
</form>

I want to pick up value = "1"

5
  • Just use the normal jquery selectors like $("#ajax").attr("value") Commented Mar 25, 2013 at 10:54
  • For value it's better to use val(). Also, for code longevity, it's better to use prop(). Commented Mar 25, 2013 at 10:55
  • answered with code plz :) Commented Mar 25, 2013 at 10:57
  • @MaxTorstensson Did you miss it? I already did... Commented Mar 25, 2013 at 10:58
  • can anyone advise on how I should send value to a php function then? Commented Mar 25, 2013 at 11:15

4 Answers 4

1

You need to access the value and then pass it through:

$(".apply_up").submit(function(e){    
    var the_val = $('#ajax').val();
    $.post("controller/ajax.php", { 'value': the_val }, function(data) {
        alert("Data Loaded: " + data);
    }); 
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try like this one:

 $(".apply_up").submit(function(e){  
    e.preventDefault();
    var imgVal = $('#ajax').val();  
    $.post("controller/ajax.php", {value : imgVal}, function(data) {
        alert("Data Loaded: " + data.value);
    }, "json"); 
});

5 Comments

How does that differ from my answer?
The OP didn't include it, so you don't know that it's required ;)
@MaxTorstensson You should try with getjson instead. or add the "json" in the post.
i change "data" to "imgval". can anyone advise on how I should send value to a php function then?
@MaxTorstensson just updated the answwer you can try that one.
0

and use serialize().. if you have lots of fields in your form

html

<form name="Apply" id="apply" class="apply_up" method="Post">
...

jquery

$(".apply_up").submit(function(e){    
   $.post("controller/ajax.php",$(this).serialize(), function(data) {
   ...

  //OR
    $.post("controller/ajax.php",$("#apply").serialize(), function(data) {
        alert("Data Loaded: " + data);
    }); 
});

1 Comment

I'd probably be tempted to recommend $(this).serialize() for the OP's situation.
0

Proper solution for most forms:

HTML

<form action="controller/ajax.php" name="Apply" class="apply_up" method="Post">
    <input type="image" src="img/t_up.png" id="ajax" name="t_up" value="1 ">
</form>

JS

$(".apply_up").on('submit', function(e){
    e.preventDefault();

    var formAction = this.action,
        formData = $(this).serialize();

    $.post(formAction, formData, function(data) {
       alert("Data Loaded: " + data);
    }); 
});

4 Comments

Not necessarily. POST requests have a max size, and sending superfluous data could be expensive in the long run.
Is that a question to my answer or to the comment the OP already deleted?
It was the OP's comment that he's deleted. Sorry for the confusion.
To your first comment: sure the requests have a max size, which is per default 8MB. You can imagine how many inputs your form would have to have to exceed this limit in a json string...

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.