سؤال

لذلك أنا جديد في استخدام jquery .post() ولكني لا أستخدم طرقًا لم أستخدمها من قبل.

أحاول نشر قيمتي إدخال مخفيتين عند النقر فوق الزر:

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

لقد اختبرت حدث الزر الذي يتم إطلاقه بنجاح وكل ما أحاول القيام به حاليًا export_file.php هو صدى شيء.

هنا هو النموذج الخاص بي:

<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>

لقد حصلت على div الخاص بي على الصفحة الأصلية:

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

ملف_التصدير.php:

<?php

echo "whatever, something!";

?>

يمكن لأي شخص أن يشير إلى أين أخطئ.تشكرات،

هل كانت مفيدة؟

المحلول

إصلاح هذا الخط:

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

تغييره إلى شيء مثل هذا:

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 },

كمان: http://jsfiddle.net/maniator/cQ2vZ/

نصائح أخرى

يحاول:

$('#button').live('click', function() {
    $.post('export_file.php', { group: $("input[name='group']").val() , test: $("input[name='test']").val() },
    function(output)    {
        $('#return').html(output).show();
    });
});

لقد أضفت معرفات إلى عناصر النموذج الخاصة بك في 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>

ثم قم بتعديل jQuery للحصول على القيم من هذه الحقول حسب المعرف، واستخدمها في معلمات استدعاء 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();
    });
});

جرب هذه

$('#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();
    });
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top