yaakov
yaakov

Reputation: 4655

JS-generated GET variables not being read PHP

I am using JavaScript to generate a form's action attribute:

$("#select-font").on('change', function(){
    var font = $(this).val();
    $("#font-family").val(font);
    $(this).css({
      'font-family': font
    });
    $("#edit-doc").css({
      'font-family': font
    });
    // BUG IS HERE --->
    $('#save')
        .attr('src', '/img/icons2/save.png')
        .attr('onclick', '$(this).parents("form").submit();');
    $('#save')
        .parents('form')
        .attr('action', 
            String($('#save')
                .parents('form')
                .attr('action')) + '&autosave=true');
    });
    $("#select-font-size")
        .on('change', function(){
            var size = $(this).val();
            $("#edit-doc").css({
                'font-size': size
            });
            $("#font-size").val(size);
            // BUG IS HERE --->
            $('#save')
                .attr('src', '/img/icons2/save.png')
                .attr('onclick', 
                    '$(this).parents("form").submit();');
            $('#save')
                .parents('form')
                .attr('action',   
                    String($('#save')
                        .parents('form')
                        .attr('action')) + '&autosave=true');
        });

The GET variable is passed to the next page; in the URL bar the location shows up as http://localhost/foo/?var1=a&var2=b&autosave=true. However, when I test the GET variable:

if($_GET["autosave"]){
   // some code...
}

The condition is not run. Why can't the PHP see the variable?

Upvotes: 0

Views: 25

Answers (1)

Tim Ogilvy
Tim Ogilvy

Reputation: 1973

Rather than using

if($_GET["autosave"]){
    // some code...
}

try:

if (isset($_GET["autosave"])) {
    // your code...
}

Also when adding handlers try this, rather than using the inline click handler, use jQuery.on()

$('#save')
    .attr('src', '/img/icons2/save.png')
    .on('click', function(){
         $(this).parents("form").submit();
    });

Upvotes: 1

Related Questions