problema com jquery .post() não funcionando
Pergunta
então sou novo no uso de jquery .post(), mas não estou usando métodos que ainda não usei.
Estou tentando postar dois valores de entrada ocultos quando um botão é clicado:
$('#button').live('click', function() {
$.post('export_file.php', { group: form.group.value , test: form.test.value },
function(output) {
$('#return').html(output).show();
});
});
eu testei que o evento do botão está disparando com sucesso e atualmente tudo que estou tentando fazer export_file.php
é ecoar alguma coisa.
aqui está meu formulário:
<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>
eu tenho meu div na página original:
<div id='return'></div>
arquivo_exportação.php:
<?php
echo "whatever, something!";
?>
Alguém poderia apontar onde estou errando.Muito obrigado,
Solução
Corrija esta linha:
$.post('export_file.php', { group: form.group.value , test: form.test.value },
Mude para algo assim:
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 },
Violino: http://jsfiddle.net/maniator/cQ2vZ/
Outras dicas
Tentar:
$('#button').live('click', function() {
$.post('export_file.php', { group: $("input[name='group']").val() , test: $("input[name='test']").val() },
function(output) {
$('#return').html(output).show();
});
});
Adicionei IDs aos elementos do seu formulário no 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>
Em seguida, alterei o jQuery para obter os valores desses campos por ID e usá-los nos parâmetros da sua chamada AJAX:
$('#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();
});
});
tente este
$('#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();
});
});