3

so I am new to using jquery .post() however I am not using methods i haven't already used before.

I am trying to post two hidden input values when a button is clicked:

$('#button').live('click', function() {
    $.post('export_file.php', { group: form.group.value , test: form.test.value },
    function(output)    {
        $('#return').html(output).show();
    });
});

i have tested the button event is firing successfully and currently all I trying to do in export_file.php is echo something.

here is my form:

<form name="form">
<input type="hidden" name="group" value="<?echo $group;?>">
<input type="hidden" name="test" value="<?echo $test_id;?>">
<input type="button" class="Mybutton" id="button" name="btnSubmit" value="Export Results">
</form>

i have got my div on the original page:

<div id='return'></div>

export_file.php:

<?php

echo "whatever, something!";

?>

Could anyone point out where I am going wrong. Many thanks,

dapperwaterbuffalo
  • 2,672
  • 5
  • 35
  • 48

4 Answers4

4

Try:

$('#button').live('click', function() {
    $.post('export_file.php', { group: $("input[name='group']").val() , test: $("input[name='test']").val() },
    function(output)    {
        $('#return').html(output).show();
    });
});
Dormouse
  • 5,130
  • 1
  • 26
  • 42
  • that would make it apply to **any** input with the `name=test` and any one with the `name=group` – Naftali May 17 '11 at 16:03
  • @Neal: And his current form only has one. C'mon, why does this have to be so overly complex? Don't assume requirements that aren't there. – mellamokb May 17 '11 at 16:04
  • @mellamokb i code to what i believe the OP will do in the future. – Naftali May 17 '11 at 16:05
  • 1
    @Neal, in that case why haven't you validated the input for him as well? Why haven't you added in an autocomplete function as well? – Dormouse May 17 '11 at 16:11
  • 1
    In fact, why haven't you also coded a small plugin for him that will ajaxsubmit any form he likes using serialized form data? I mean you surely "believe" that he'll want to submit a form via AJAX in the future, no? – Dormouse May 17 '11 at 16:17
3

Fix this line:

$.post('export_file.php', { group: form.group.value , test: form.test.value },

Change it to something like this:

var group_val = $('input[name="group"]', 'form[name="form"]').get(0).value;
var test_val = $('input[name="test"]', 'form[name="form"]').get(0).value;
$.post('export_file.php', { group: group_val , test: test_val },

Fiddle: http://jsfiddle.net/maniator/cQ2vZ/

Naftali
  • 144,921
  • 39
  • 244
  • 303
1

I've added ids to your form elements in your HTML:

<form name="form">
    <input type="hidden" name="group" id="group" value="<?echo $group;?>">
    <input type="hidden" name="test" id="test" value="<?echo $test_id;?>">
    <input type="button" class="Mybutton" id="button" name="btnSubmit" value="Export Results">
</form>

Then amended the jQuery to get the values from these fields by ID, and use these in the parameters of your AJAX call:

$('#button').live('click', function() {
    var groupValue = $("#group").val();
    var testValue = $("#test").val();

    $.post('export_file.php', { group: groupValue , test: testValue },
    function(output)    {
        $('#return').html(output).show();
    });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

try this one

$('#button').live('click', function() {
    var group_val = $("input[name='group']").val(); // gets the value of hidden field with the name group
    var test_val = $("input[name='test']").val(); // gets the value of hidden field with the name test and store it in test_val variable
    $.post('export_file.php', { group: group_val  , test: test_val  },
    function(output)    {
        $('#return').html(output).show();
    });
});
Sharp Coders
  • 458
  • 5
  • 7