Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass jQuery variables to PHP variable?

How do you pass a variable from jQuery to PHP without a page refresh? When I click on a checkbox I would like to pass a variable from jQuery to PHP. I am also using formdialog.

My PHP code

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

my javascript code

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;
        });
like image 719
geetha Avatar asked Mar 05 '11 06:03

geetha


People also ask

Can I use jquery variable in PHP?

Create an hidden input tag in HTML , then set it's value as a JSON string with jquery. Next whenever you need that data , simply call the PHP function which returns value of input tag . Now you can convert the JSON data and store it in PHP variable.

How can store variable variable value in jquery in PHP?

If at all you want to send php value to php page using jquery and ajax, then first of all, create input text with type hidden. put your php value in it. and when you click then get that value from that input type hidden in jquery and then pass it to whichever page you want.

Is it possible to pass data from JavaScript to PHP?

JavaScript is the client side and PHP is the server side script language. The way to pass a JavaScript variable to PHP is through a request.


1 Answers

Ajax can do this. Google it, and check out api.jquery.com and look at the ajax functions, .ajax(), .post(), .get(), .load(), etc.

As for your specific question, here is what you would do:

//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";
?>
like image 57
Explosion Pills Avatar answered Oct 15 '22 00:10

Explosion Pills