I have here an integrated version of my code into a modal...
This code gives undefined index id

 <!-- Edit-Page Modal -->
              <div class="modal fade" id="edit-pages" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                      <h4 class="modal-title" id="myModalLabel">Edit Page</h4>
                    </div>
                    <div class="modal-body">


                        <?php 
                        //check for any errors
                        if(isset($error)){
                            foreach($error as $error){
                                    echo $error.'<br />';
                                }
                            }

                           try {




                                $qp = $db->prepare('SELECT PageID, PageName, Content FROM pages WHERE pageID = :pageID') ;

                                $qp->execute(array(':pageID' => $_GET['id']));
                                $row = $qp->fetch(); 

                             } catch(PDOException $e) {
                                echo $e->getMessage();
                                }   




                        ?>
                        <form action='' method='POST'>

                        <input type='hidden' name='pageID' value='<?php echo $row['PageID'];?>'>

                        <p><label>Content</label><br />
                        <textarea name='Content' cols='60' rows='10'><?php echo $row['Content'];?></textarea></p>



                                <script>

                                    CKEDITOR.replace( 'Content',{customConfig:'/config.js'});

                                </script>
                                <p><input class="btn btn-primary btn" type='submit' name='submitpage' value='Update'></p>
                                 <?php include("../includes/submitpage.php");?>

                        </form>


                    </div>

                    <div class="modal-footer">
                      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

                    </div>
                  </div>
                </div>
              </div>

              <!-- END Page MODAL -->

Here is submitpage.php that doesnt insert or update the content on the database

<?php

    //if form has been submitted process it

    if(isset($_POST['submitpage'])){

        //collect form data
        extract($_POST);

        //very basic validation
        if($pageID ==''){
            $error[] = 'This page is missing a valid id!.';
        }


        if($Content ==''){
            $error[] = 'Please enter the content of the page.';
        }


        if(!isset($error)){

            try {

                 $query = 'SELECT fullname from members where idNUMBER="'.$_SESSION['idnumber'].'"';
                try {
                        $pdoStatement = $db->query($query);
                }
                    catch (PDOException $exception) {
                        // the query failed and debugging is enabled
                        echo "<p>There was an error in query: $query</p>";
                        echo $exception->getMessage();
                        $pdoStatement = false;
                    }
                    if ($pdoStatement) {
                        // the query was successful
                        // get the result (if any)
                        // fetchObject returns FALSE if there is no record
                    if ($recordObj = $pdoStatement->fetchObject()) {
                        $user=$recordObj->fullname;       


                $act= "Edited page ".$pageName;
                $addcat=$db->prepare('INSERT INTO userlog (Name, Datelog, Activity ) VALUES (:Name, :Datelog, :Activity)') ;
                $addcat->execute(array(
                    ':Name' => $user,

                    ':Datelog' => date('Y-m-d H:i:s'),

                    ':Activity'=> $act  

                ));

}       
}


                //insert into database
                $insp = $db->prepare('INSERT into pages (Content) VALUES (:Content)');
                $insp->execute(array(

                    ':Content' => $Content
                ));



                //redirect to Main Panel page
                header('Location: MainPanel.php');
                exit;

            } catch(PDOException $e) {
                echo $e->getMessage();
            }

        }

    }

?>

Lastly, is this code

 <!-- Tab panes -->
    <div class="tab-content">
      <!-- Pages -->
      <div class="tab-pane active" id="pages">
        <table class="table table-hover">
          <thead>
            <tr>
             <th>#</th>
              <th>Page</th>
              <th>Options</th>
            </tr>
          </thead>
            <?php

            try {
                //instantiate the class
                $pages = new Paginator('3','p');

                //collect all records for the next function
                $stmt = $db->query('SELECT pageID FROM pages');

                //determine the total number of records
                $pages->set_total($stmt->rowCount());
                $stmt = $db->query('SELECT pageID, pageName FROM pages ORDER BY pageID ASC '.$pages->get_limit());

                while($row = $stmt->fetch()){
                        echo ' <tbody>
                                <tr>
                                <td>'.$row['pageID'].'</td>
                                <td>'.$row['pageName'].'</td>'
            ?>
                               <td><a href="#edit-pages?id=<?php echo $row['pageID'];?>" data-toggle="modal" data-target="#edit-pages">Edit</a> </td>
                                </tr>
                <?php
                         }
                             echo '<div>'.$pages->page_links().'</div>';
                        }catch(PDOException $e) {
                            echo $e->getMessage();
                    }

                ?>                        


        </table>
      </div>
      <!-- Pages end -->

Recommended Answers

All 15 Replies

Can you post the exact error? Undefined index regarding which array: $_GET, $_POST, $row?

Member Avatar for diafol

Are you referring to Line 27:

$qp->execute(array(':pageID' => $_GET['id']));

You could have saved us a lot of work by highlighting the actual line being reported. Is this the line that's causing problems?

Is there a ?id=... in the url when you get this error? If not, that's your problem. Perhpas you need to check for its existence before running a block of code. If it's missing, decide what to do - throw a message or set a default value to allow the code to continue flowing.

e.g.

if(isset($_GET['id']))
{
//run the main code
}else{
//provide an error message
}

or

$id = (isset($_GET['id'])) ? $_GET['id'] : 1;

The default value of 1 will be given if the 'id' is not a parameter in the url querystring.

Im not familiar with using modals but all i can say is that $_GET['id'] should contain the id found in this line of code

<td><a href="#edit-pages?id=<?php echo $row['pageID'];?>" data-toggle="modal" data-target="#edit-pages">Edit</a> </td>

so that my query would select the record where the pageID matched the id passed in the $_GET['id'] request...

Member Avatar for diafol

So does this ?id=... show in the browser address bar?

in my browser?..well when i click edit the modal window appears but I dont see id on my browsers address bar

Member Avatar for diafol

OK, this is stretching my knowledge a bit. Is the error shown before the modal link is clicked?

the undefined index appears inside the modal window

Member Avatar for diafol

I'm assuming that the modal window isn't ajaxed, meaning that it is loaded on main page load - so it's all there hidden away, waiting to be displayed by the link. The querystring (?id=...) has absolutely no effect as the modal content has already been loaded. The #edit-pages is just a hook to show the modal window. Well, that's my take anyway.

Member Avatar for diafol

Just to clarify. AFAIK, the modal is just a fancy javascript construct to show existing html. Any php within the "modal" has already been run when the page with the links to show the modal was loaded.

If you need to change the data (from server) shown in the modal, you'll probably need to run ajax on link click.

i dont understand what all that meant but ill try and study ajax now..thanks

Any link that shows how to pass the id to the modal window?

Ron, if you are performing an ajax request within the modal window then show us the javascript code you're using, that way we can suggest an appropriate solution.

But diafol suggested that the error is already there, it does not depend on the execution of the modal window, because the undefined index error happens in the server side.

This error should say something else, not only undefined index but also the index name, the script name and line error, for example:

<?php

    $a = array();
    echo $a['id'];

Will generate a notice:

PHP Notice:  Undefined index: id in /tmp/test.php on line 4

By checking the html source received by the browser you should be able to see the entire error and understand if this is generated by the absence of $_GET['id'] or of a column requested from the query result set.

Member Avatar for diafol

As cereal says (he knows more about this stuff than me).

Another thing you can test quickly:

If you look at the page source in the browser BEFORE you click on the link and search for "undefined index". If it is present, it means that all the php code has been run on page load - including the code required to create the content of the modal. So in other words, it's too late to pass an id to it as the php has already been run and has found no $_GET['id'] because it simply didn't exist when the page was loaded.

Another thing to try - type ?id=1 at the end of the url in your browser address bar and reload. You may see that it now works. Again in this case it shows that all the php was run on page load.

If this is the case, a fix would be to hijack the link (a) click and run an ajax routine to update some div or other in the modal html with new data from the server.

Member Avatar for diafol

Hi ROn, I noticed this was fixed - any chance of letting us know what the issue was and how you managed to solve it? It may help others.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.