window.open() rather than ajax url

window.open() rather than ajax url

dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0

I am trying to get the result of submit() from an editor call to cause a window.open() to get the result of the GET call to an URL.

Here is what I have that does the GET request, but of course datatables is waiting for the json response which won't happen.

In fact what will happen is that the URL I want to GET towards will create a PDF and send that to the new window.

    var labeleditor = new $.fn.dataTable.Editor( {
        "ajax": {
            "url": "pdf/label.php",
            "type": "GET",
        },
        "formOptions": {
            main: {
                blurOnBackground: false
            }
        },
        "fields": [
            {
                "name": "printrun",
                "label": "Enter the Number of Labels to Print"
            }
        ],
    i18n: {
       create: { title: "Print Blank Valve Labels" }
            }
     });

So my PHP script at pdf/label.php can receive the $_GET string no problem and decodes the value supplied for "printrun", and even generates the PDF file as required.

But I want a new browser window - I assume via window.open(), to capture the generated PDF result, not ajax like a normal DT operation.

thanks.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin
    edited July 2015 Answer ✓

    Hi,

    Interesting one. I think what you'll probably need to do is have pdf/label.php return some information to the Editor request that will cause the page to be forwarded on. So rather than immediately generating and returning the PDF, it would give, for example an id, or a url then you could just do:

    labeleditor.on( 'postSubmit', function ( e, json ) {
      window.open( urlBaseOnDataInJson, ... );
    } );
    

    Allan

  • dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0
    edited July 2015

    Thanks for the huge clue on how to do it. It works great.

    Here are some code snippets for those who come later looking for crumbs.

    labeleditor.on( 'postSubmit', function( e, json ) {
            if ( json.printrun != "" ) {
                var url = 'pdf/valvelabel.php?printrun=' + json.printrun ;
                window.open( url, '_blank' );
                this.close();
            }
        } );
    

    and the PHP to do the linkage (latest DT format)

    if ( isset($_GET['data']) && isset($_GET['data'][0]) && isset($_GET['data'][0]['printrun']) ) {
        $printrun = $_GET['data'][0]['printrun'];   
    }
    $link = array();
    
    $link['printrun'] = $printrun;
    // need an empty data element so that DT will find an empty update and not put up an error.
    $link['data'] = "";
    
    echo json_encode($link);
    

    Lots of other ways to do it, I chose GET as that was easier to debug.

This discussion has been closed.