2011-03-05 24 views
7

Bir sayfa yenilemesi olmadan jQuery'den PHP'ye bir değişkeni nasıl geçirirsiniz? Bir onay kutusunu tıkladığımda jQuery'den PHP'ye bir değişken aktarmak istiyorum. Ben de formdialog kullanıyorum.jQuery değişkenleri PHP değişkenine nasıl aktarılır?

My PHP kodu

<?php 
echo "<input name='opendialog' type='checkbox' class='opendialog' onclick='countChecked()' value=".$taskid." ?>" /> </td>" 
?> 

benim javascript kodu

function countChecked() { 
    var n = $("input:checked").length; 

    var allVals = []; 
    $('input:checkbox:checked').each(function() { 
    allVals.push($(this).val()); 

    }); 
    $('.sel').text(allVals+' '); 
    $('.select1').val(allVals); 
    alert(allVals); 

    <?php $taskidj=$rowtask['taskID']; 
    // echo "aaa...".$rowtask['taskID']; ?>  

} 

$(":checkbox").click(countChecked); 


// my jquery code 


     $('.mydialog').dialog({ 

      bgiframe: true, 
      autoOpen: false, 
      modal: true, 
      width: 700, 
      height:500, 
      resizable: false, 
      open: function(){closedialog = 1;$(document).bind('click', overlayclickclose);}, 
      focus: function(){closedialog = 0;}, 
      close: function(){$(document).unbind('click');}, 
      buttons: { 
       Submit: function(){ 
       var bValid = true; 
      // allFields.removeClass("ui-state-error"); 

      // bValid = bValid && checkLength(name, "username", 3, 16); 



      // bValid = bValid && checkRegexp(name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter."); 





       if (bValid) { 

         processDetails(); 

         return false; 

       } 


       }, 
       Cancel: function() { 
        $(this).dialog("close"); 
        $('input[name=opendialog]').attr('checked', false); 
       } 
      } 
     }); 

    $('.opendialog').click(function() { 
      $('.mydialog').dialog('open'); 
      closedialog = 0; 
     }); 
+1

olası yinelenen (http://stackoverflow.com/questions/2376913/how-to-pass-variable-from-javascript- [jQuery POST kullanarak PHP JavaScript gelen değişken geçirmek nasıl] to-php-kullanarak-jquery-post) – deceze

cevap

22

Ajax yapabilirsiniz. Google'a gidin ve api.jquery.com'a göz atın ve ajax işlevlerine bakın, .ajax(), .post(), .get(), .load(), vb.

Sorunuz için burada yapacağını:

//Javascript file 
$("input[type=checkbox]").click(function() { 
    $.post('my_ajax_receiver.php', 'val=' + $(this).val(), function (response) { 
     alert(response); 
    }); 
}); 

//PHP file my_ajax_receiver.php 
<?php 
    $value = $_POST['val']; 
    echo "I got your value! $value"; 
?> 
+0

merhaba tandu, Tanımlanmamış dizin: val. Ben isset() işlevini kullanıyorum. ama değeri alamıyorum. – geetha

+0

$ (this) .val() öğesinin ne olduğunu kontrol edin. Onay kutunuz için bir değer tanımlıyor musunuz? –

İlgili konular