Add HTML tags to Add page by Page_Render() server event

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
mehmetbkm
User
Posts: 21

Add HTML tags to Add page by Page_Render() server event

Post by mehmetbkm »

I need to dynamically add my own custom html form tags (input, button and div) while on the add data to database screen (add page).

function Page_Render()
{
    // Directly output HTML and JavaScript
    $html = '<input type="text" id="isbnInput" placeholder="add ISBN" class="form-control" style="width: 200px; display: inline-block; margin-right: 10px;">
             <button type="button" id="searchButton" class="btn btn-primary">Search</button>
             <div id="result"></div>';

    // Output the HTML
    echo $html;

    // JavaScript code as string
    $script = <<<EOD
    <script>
        $(document).ready(function() {
            $('#searchButton').click(function() {
                var isbn = $('#isbnInput').val();

                $.ajax({
                    url: '../istek.php',
                    type: 'GET',
                    data: { isbn: isbn },
                    success: function(response) {
                        $('#result').html(response);
                    },
                    error: function(xhr, status, error) {
                        $('#result').html('<p>Bir hata oluştu: ' + error + '</p>');
                    }
                });
            });
        });
    </script>
    EOD;

    // Output the JavaScript
    echo $script;
}

sangnandar
User
Posts: 1031

Post by sangnandar »

If you open the add-page you'll see this error in browser's console (F12)

Uncaught ReferenceError: $ is not defined

Meaning: jquery.js is not loaded yet.

Change the event listener from

$(document).ready(function() {
  // your-code
}

to

loadjs.ready("load", function () {
  // your-code
}

Or just simply move your-code to add-page startup script


DGarza
User
Posts: 152

Post by DGarza »

I recommend you that all your scripts add them to Client Scripts->Table-Specific->Add/Copy Page (Or whatever page you want)

You don't need to add $('document').ready(funcion(){...}) to load, just add your scripts and phpmaker encapsule everything for you.


Post Reply