0

I got one input field named code, which i like to use in 2 php files. First php file checks the code on database. If successfull second php will generate a pdf file (and prints). Now the ajax request for the first php file works, but for the second php file the variable code isn't set (i checked form-data in chrome. In 2nd php file $_POST['code'] seems to be empty.

    $(document).ready(function ()
            {
                $(document).on('submit', '#reg-form', function ()
                {
                    $.post('lookup.php', $(this).serialize())
                            .done(function (data)
                            {
                                $("#reg-form").fadeOut('slow', function ()
                                {
                                    $(".result").fadeIn('slow', function ()
                                    {
// Just a hello ....
                                        $(".result").html(data);
                                        // Generate + Print PDF badge
                                        $.post('generate_print.php',$(this).serialize())//<-- how do i get the code from form
                                    });
                                });
                            })
                            .fail(function ()
                            {
                                alert('fail to submit the data');
                            });
                    return false;
                });

            });

In generate_print.php i have

$code = isset($_POST['code']) ? $_POST['code'] : 'this-should-not-be-shown';

For testing and this-should-not-be-shown generated :(

[EDIT] I change the js to see if I can get the var code

 $(document).ready(function ()
        {
            $(document).on('submit', '#reg-form', function ()
            {
                var tmpCode = $("#code").val();//<-- here edit

                $.post('lookup.php', $(this).serialize())
                        .done(function (data)
                        {
                            console.log("inner test "+tmpTest);//<-- here check
                            $("#reg-form").fadeOut('slow', function ()
                            {
                                $(".result").fadeIn('slow', function ()
                                {
                                    $(".result").html(data);
                                    // Generate + Print PDF badge
                                    $.post('generate_print.php',tmpCode) //<-- here edit , not working 
                                });
                            });
                        })
                        .fail(function ()
                        {
                            alert('fail to submit the data');
                        });
                return false;
            });

        });
alex
  • 4,804
  • 14
  • 51
  • 86
  • what is the tag which you referred `$(".result").html(data);` – AnkiiG Jan 13 '16 at 13:14
  • @AnkiiG that is just a welcome message coming from lookup.php. I realised I should not use data, so I editeb my question now i am trying to pass var tmpCode – alex Jan 13 '16 at 13:23
  • 1
    Try as `$.post('generate_print.php',{ tmpCode:tmpCode});` – AnkiiG Jan 13 '16 at 13:25
  • @AnkiiG, that is it! If i have more than just one variable how should i do this? And can you post your comment as an answer so i can check it! – alex Jan 13 '16 at 13:28
  • 1
    To send multiple variables try as `$.post( 'generate_print.php', { tmpCode:tmpCode, name: "Alex"} );` – AnkiiG Jan 13 '16 at 13:30

0 Answers0