简体   繁体   中英

Error with javascript function not being defined in php file

I have seen many similar questions here , that have solved this problem , but I cant figure out why my code produces this error : editRow() not defined , since I have followed everything the other questions said. I want to open a pop up window , to edit a row in a crud table in php. I have tried to place the functions in the head of the file , but nothing changed.

<!DOCTYPE HTML PUCLIC "-//W3C//DTDHTML 4.0 Transitional//EN">
<HTML>
<HEAD>
    <TITLE>LIBRARY DATABASE</TITLE>
    <link rel="stylesheet" type="text/css" href="tablestyle.css">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
            var newObj = { 
                        width: 900, 
                        height: 460, 
                        editable: false,
                        pageModel: { type: "local", rPP: 15, rPPOptions: [10, 15, 20, 50, 100] },
                        flexHeight: true,
                        title: "Companies listed on the <b>NASDAQ</b>",
                        freezeCols: 1,
                        resizable: true,            
                        editModel: { clicksToEdit: 2 },
                        selectionModel: { mode: 'single', type: 'row' }
                     };

                  var $grid = $("#grid_crud").pqGrid(newObj);
                    //create popup dialog.
                    $("#popup-dialog-crud").dialog({ width: 400, modal: true,
                     open: function () { $(".ui-dialog").position({ of: "#grid_crud" }); },
                     autoOpen: false
                    });
                 function getRowIndx() {
                        var arr = $grid.pqGrid("selection", { type: 'row', method: 'getSelection' });
                        if (arr && arr.length > 0) {
                            return arr[0].rowIndx;                                
                        }
                         else {
                            alert("Select a row.");
                            return null;
                        }
                 }
                function editRow() {
                    var rowIndx = getRowIndx();
                    if (rowIndx != null) {

                        var row = $grid.pqGrid('getRowData', {rowIndx: rowIndx});

                        var $frm = $("form#crud-form");
                        $frm.find("input[name='memberID']").val(row[0]);
                        $frm.find("input[name='MFirst']").val(row[1]);
                        $frm.find("input[name='MLast']").val(row[3]);
                        $frm.find("input[name='Street']").val(row[4]);
                        $frm.find("input[name='number']").val(row[5]);
                        $frm.find("input[name='postalCode']").val(row[6]);
                        $frm.find("input[name='Mbirthdate']").val(row[7]);

                        $("#popup-dialog-crud").dialog({ title: "Edit Record (" + (rowIndx + 1) + ")", buttons: {
                            Update: function () {
                                //update row.
                                var row = [];
                                row[0] = $frm.find("input[name='memberID']").val();
                                row[1] = $frm.find("input[name='MFirst']").val();
                                row[3] = $frm.find("input[name='MLast']").val();
                                row[4] = $frm.find("input[name='Street']").val();
                                row[5] = $frm.find("input[name='number']").val();
                                row[6] = $frm.find("input[name='postalCode']").val();
                                row[7] = $frm.find("input[name='Mbirthdate']").val();

                                $grid.pqGrid('updateRow', { rowIndx: rowIndx, row: row, checkEditable: false });

                                $(this).dialog("close");
                            },
                            Cancel: function () {
                                $(this).dialog("close");
                            }
                        }
                        }).dialog("open");
                    }
                }
        </script>
</HEAD>
<div class="navbar">
                <a href="mydatabase.php">Home</a>
                <a href="member_table.php">Member</a>
                <a href="book_table.php">Book</a>
                <a href="borrows_table.php">Borrows</a>
              </div>
              <br><br>
              <div class="navbar">
                    <a href="insert.php">Insert</a>
                    <a href="update.php">Update</a>
                    <a href="delete.php">Delete</a>
                  </div>

    <TABLE class="minimalistBlack">
        <thead>
        <tr>
        <th> memberID </th>
        <th> First Name </th>
        <th> Last Name </th>
        <th> Street </th>
        <th> Number </th>
        <th> Postal Code </th>
        <th> Birthdate </th>
        <th> Update </th>
        </tr>
        </thead>
        <?php 
            $conn= mysqli_connect("localhost","root","","library");
            if ($conn -> connect_error){
                die("Conenction failed:". $conn->connect_error);
            }
            $sql="SELECT memberID,MFirst,MLast,Street,number,postalCode,Mbirthdate FROM member";
            $result = $conn->query($sql);
            if ($result->num_rows>0){

                while($row= $result->fetch_assoc()){
                    echo "<tr>";
                    echo "<td>".$row['memberID']."</td>";
                    echo "<td>".$row['MFirst']."</td>";
                    echo "<td>".$row['MLast']."</td>";    
                    echo "<td>".$row['Street']."</td>"; 
                    echo "<td>".$row['number']."</td>"; 
                    echo "<td>".$row['postalCode']."</td>"; 
                    echo "<td>".$row['Mbirthdate']."</td>"; 
                    echo "<td><a href='#' onclick='editRow()'>Edit </a>
                     | <a href=\"deletefmtable.php?memberID=$row[memberID]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";  
                }
                echo "</TABLE>";
            }
            else { echo "0 result"; }
            $conn->close();
         ?>
    </TABLE>
</HTML>

Not sure but your document has no <body> tag so that will likely be a problem.

You also need to separate the loading of jquery script and your custom js. so:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>//Your custom scripts</script>

You can't put script code inside a <script> tag that has a src attribute. You need two separate tags, one to load jQuery and another to load your script.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
... your code here
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM