NEW button for Whole Inline Create example stopped working when using PHP var as default field

NEW button for Whole Inline Create example stopped working when using PHP var as default field

ebrufinoebrufino Posts: 6Questions: 1Answers: 0

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
Hi, I modified the fullRowCreate.html example so that it sets a default email value on create. It works flawlessly when the default field value for email, i.e., def: "someone@yahoo.com".

Now I want to replace the hard-coded default w/ a php variable, upon doing so, however, the NEW button stopped working (not displaying inline row to create new record)

I am considerably still at the process trying to learn how Datatables Editor works and how to manipulate/customize it using values from PHP variables.


here's my code for fullRowCreate.html:


// THIS WORKED
fields: [
{
label: "E-mail:",
name: "email",
def: "someone@yahoo.com"

// NOT WORKING
<?php $email="anotherperson@yahoo.com"; ?>
.
.
.

. . . fields: [ { label: "E-mail:", name: "email", def: function () { var p_email = "<?php echo $email ?>"; var def_email = json_encode(p_email); return def_email; } . ..

This question has an accepted answers - jump to answer

Answers

  • ebrufinoebrufino Posts: 6Questions: 1Answers: 0

    the link to test case is:
    https://emamen.com/portal/dte/examples/inline-editing/fullRowCreate.html

    Debugger Code:
    fullRowCreate.html:50 Uncaught ReferenceError: json_encode is not defined
    at def (fullRowCreate.html:50:23)
    at A.def (dataTables.editor.min.js:1:57124)
    at A.<anonymous> (dataTables.editor.min.js:1:23819)
    at Function.each (jquery-3.5.1.js:387:19)
    at R.Pt [as inlineCreate] (dataTables.editor.min.js:1:23756)
    at B.action (dataTables.editor.min.js:1:66979)
    at t (dataTables.buttons.min.js:18:39)
    at HTMLButtonElement.<anonymous> (dataTables.buttons.min.js:18:472)
    at HTMLButtonElement.dispatch (jquery-3.5.1.js:5429:27)
    at elemData.handle (jquery-3.5.1.js:5233:28)

  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin

    Looking at the rendered HTML you have:

                {
                    label: "E-mail:",
                    name: "email",
                    def: function () {
                            var p_email = "<?php echo $email ?>";
                            var def_email = json_encode(p_email);
                            return def_email;
                        }
                }, {
    

    It looks like the server isn't set up to run PHP on files with the .html extension. Perhaps you just need to rename fullRowCreate.html to fullRowCreate.php to allow the PHP to execute.

    I'd use:

    var p_email = "<?php echo json_encode($email); ?>";
    return p_email;
    

    Allan

  • ebrufinoebrufino Posts: 6Questions: 1Answers: 0

    i will try that and get back to you on the result. Thank you Allan for accommodating my query.

  • ebrufinoebrufino Posts: 6Questions: 1Answers: 0

    Hi Allan,

    I tried renaming fullRowCreate.html to fullRowCreate.php - NEW button still doesn't work.

    Furthermore, when I replaced the code inside the function w/ your recommended lines, the entire Datatable did not rendered and has disappeared

  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin
    Answer ✓

    The page currently has:

                    def: function () {
                            var p_email = "someone@yahoo.com";
                            var def_email = json_encode(p_email);
                            return def_email;
                            //var p_email = ""someone@yahoo.com"";
                            //return p_email;
                        }
                }, {
    

    That, correctly, causes an error when the New button is presses because json_encode() is a PHP function - not a Javascript one. I think my code above is still correctly, but perhaps $email isn't being defined?

    You must define the PHP variable $email somewhere? Where are you doing that?

    Allan

  • ebrufinoebrufino Posts: 6Questions: 1Answers: 0

    Hi Allan,

    PHP varialble is defined at the very top of fullRowCreate.php:

    <?php
    $email = "someone@yahoo.com";

    <?php > ?>

    <!DOCTYPE html>
    <html>

  • ebrufinoebrufino Posts: 6Questions: 1Answers: 0

    Hi Allan,

    Thank you for correctly pointing out that json_encode() is a PHP function and not a Javascript function.

    I corrected my code as follows and its now working.

    from:
    var def_email = json_encode(<?php echo $email; ?>);

    to:
    var def_email = <?php echo json_encode($email); ?>;

    I am sure your code works much the same way, and I'll be using it to replace my code to make it shorter and cleaner,

    Thanks again Allan for helping me resolve my issue.

    Best Regards,

    Eric Rufino

Sign In or Register to comment.