On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi everyone - Evo 1.0.12

    I have been getting the familiar old "Cannot modify header information - headers already sent" error message and it is driving me crazy.
    No white space before or after, Utf-8 with NO BOM - tried pretty much everything I could find online - PLEASE HELP.
    *******
    The full error message is - Warning: Cannot modify header information - headers already sent by (output started at /home/rsacours/public_html/assets/modules/coursemanager/process.cm.php:11) in /home/rsacours/public_html/manager/includes/document.parser.class.inc.php on line 3595
    Error
    (Note: The line number referenced here - process.cm.php:11 is the one following this line
    <div id="padbox">
    below)
    *******
    I have a (Course Manager) module that manages some custom tables. One of the functions is to update/edit course numbers which works fine for the first row (red box in attached image) but for all subsequent rows the above error is generated.


    Two files are involved here - 1.process.cm.php which is called to process the POSTED data and 2.index.cm.php which is the GUI for the Course Manager Module.
    If anyone can spot the issue in the offending file below I would be eternally grateful.
    1. process.cm.php
    <html>
    <head>
    </head>
    <body>
    <div id="padbox">
    <?php
    // include the modx config and parser -- Modified by WSG - 10/01/2014 added places
    require_once('../../../manager/includes/config.inc.php');
    require_once('../../../manager/includes/document.parser.class.inc.php');
    $modx = new DocumentParser;
    
    // ******** NEW SESSION **********
    if($_POST['add_new']==1){
    $c=$_POST['course'];
    $d=$_POST['date'];
    $t=$_POST['time'];
    $v=$_POST['venue'];
    $p=$_POST['places'];
    
    // ****** ADD NEW SESSION ********** WSG
    $sql = "INSERT INTO xtra_session VALUES(NULL,$c,$v,'$d','$t',$p)";
    $add = $modx->db->query($sql);
    $msg = ($add? "new session has been added" : "error: ".mysql_error());	
    }
    
    // ******** REMOVE SESSION **********
    if($_POST['remove']==1){
    $id=$_POST['sid'];
    $sql = "DELETE FROM xtra_session WHERE xtra_session.id=$id";
    $add = $modx->db->query($sql);
    $msg = ($add? "session has been deleted" : "error: ".mysql_error());	
    }
    
    // ******** EDIT SESSION PLACES ********** WSG
    if($_POST['edit']==1){
    $id=$_POST['sid'];
    $z=$_POST['edit_places'];
    $sql = "UPDATE xtra_session SET places=$z WHERE xtra_session.id=$id";
    $add = $modx->db->query($sql);
    $msg = ($add? "The available places have been updated" : "error: ".mysql_error());
    }
    else {
    echo "Please enter a number of places to change";
    }
    }
    echo $msg;
    ?>
    <form><input type="button" value="Back to admin page" onclick="history.go(-1); return true;" /></form>
    <noscript style="color:#c00">oh dear... looks like you have no javascript and the button is broken, click Course Manager from top menu to continue</noscript>
    </div>
    </body>
    </html>


    and 2. index.cm.php
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="media/style/<?php echo $modx->config['manager_theme']; ?>/style.css" />
    <link rel="stylesheet" type="text/css" href="../assets/modules/coursemanager/cm_style.css" />
    <script type="text/javascript" src="../assets/modules/coursemanager/calendarDateInput.js"></script>
    <script type="text/javascript" src="../assets/modules/coursemanager/input.js"></script>
    </head>
    <body>
    <noscript style="color:#c00">oh dear... it looks like you have no javascript, continue at your own risk! This module recommends you turn your javascript on.</noscript>
    <div id="padbox">
    
    <h1>Course Manager</h1>
    
    <!-- add session -->
    <!-- this block of code has been removed for brevity -->
    <!-- ******************************************************************************* -->
    
    <!-- list all sessions with edit places and removal option WSG 11/01/2014-->
    <h2 class="section">Currently available sessions</h2>
    <table cellspacing="0" cellpadding="5">
    	<tr><th>ID</th><th>Course</th><th>yyyy-mm-dd</th><th>Venue</th><th>Time</th><th>Places</th><th>New #</th><th> </th></tr>
    <?php
    $q2="SELECT xtra_session.id AS id,xtra_courses.code AS code,xtra_session.date AS date,xtra_session.time AS time, xtra_session.places AS places, xtra_venues.name AS venue FROM xtra_session,xtra_courses,xtra_venues WHERE xtra_session.date >= CURDATE() AND xtra_session.course_id=xtra_courses.id AND xtra_session.venue_id=xtra_venues.id ORDER BY xtra_session.course_id, xtra_session.date";
    $r2= mysql_query($q2);
    
    while($details = mysql_fetch_array($r2)){
    $sid = $details['id'];
    $code = $details['code'];
    $date = $details['date'];
    $venue = $details['venue'];
    $time = $details['time'];
    $places = $details['places'];
    
    echo '<tr'.(($c = !$c)?' class="o"':' class="e"').">";
    echo"<td>".$sid."</td>";
    echo"<td>".$code."</td>";
    echo"<td>".$date."</td>";
    echo"<td>".$venue."</td>";
    echo"<td>".$time."</td>";
    echo"<td>".$places."</td>";
    
    <!-- The block below is the code that works for the first row only then produces the ERROR -->
    
    echo"<td><form name=\"edit_session\" id=\"edit_session\" method=\"post\" action=\"../assets/modules/coursemanager/process.cm.php\"><input type=\"hidden\" name=\"edit\" value=\"1\" /><input type=\"text\" size=\"3\" name=\"edit_places\" value=\" \" /><input type=\"hidden\" name=\"sid\" value=\"".$sid."\" /><input type=\"button\" name=\"Submit\" value=\"edit places\" onclick=\"return resetForm();\"/></form></td>";
    
    echo"<td><form name=\"remove_session".$sid."\" method=\"post\" action=\"../assets/modules/coursemanager/process.cm.php\" onsubmit=\"return check_del();\"><input type=\"hidden\" name=\"remove\" value=\"1\" /><input type=\"hidden\" name=\"sid\" value=\"".$sid."\" /><input type=\"submit\" name=\"Submit\" value=\"delete session\" /></form></td>\n\n";
    echo"</tr>";
    }
    ?>
    </table>
    <p> </p>
    <!-- list all sessions with removal option -->
    <!-- this block of code has been removed for brevity -->
    <!-- ******************************************************************************* -->
    
    <!-- list all courses -->
    <!-- this block of code has been removed for brevity -->
    <!-- ******************************************************************************* -->
    
    <!-- list all venues -->
    <!-- this block of code has been removed for brevity -->
    <!-- ******************************************************************************* -->
    
    </div>
    <script language="javascript">
    function resetForm()
    {
        document.forms["edit_session"].submit(); //first submit the form values
        document.forms["edit_session"].reset(); //then reset the form values
      }
    </script>
    </body>
    </html>
    [ed. note: Twobears last edited this post 7 years, 8 months ago.]
    • That error message almost always means that there is a PHP error. Both MODX and PHP are trying to display the error, both are setting headers for their error page, and they are tripping over each other. Check your server's PHP log, it will probably have the actual error that they are trying to report.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
      • Shalom Susan
        Nice to have your support again.
        I checked the error logs and it would appear that the error is the variable &z is not passing any value to the UPDATE statement:

        MODX encountered the following error while attempting to parse the requested resource:
        « Execution of a query to the database failed - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `xtra_session`.`id`=907' at line 1 »
        SQL > UPDATE `rsacours_modx`.`xtra_session` SET `places`= WHERE `xtra_session`.`id`=907


        The odd thing is that the first row of the table always gets modified successfully even when the value is in another row.
        There would appear to be an error in my code, I think around line 44 of the index.cm.php file that eludes me.

        Kind regards Stephen
        • Hm. Is this in an iFrame or something?
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
          • Hi Susan
            Each row is in a form generated by an echo statement as per the example below:

            echo"<td>
            <form name=\"edit_session".$sid."\" id=\"edit_session\" method=\"post\" action=\"../assets/modules/coursemanager/process.cm.php\">
            <input type=\"hidden\" name=\"edit\" value=\"1\" />
            <input type=\"text\" size=\"3\" name=\"new_places\" id=\"new_places\" value=\"\" />
            <input type=\"hidden\" name=\"sid\" value=\"".$sid."\" />
            <input type=\"button\" name=\"Submit\" value=\"edit places\" onclick=\"return resetForm();\"/>
            </form>
            <script language=\"javascript\">
            function resetForm()
            {
                document.forms[\"edit_session".$sid."\"].submit(); //first submit the form values
                document.forms[\"edit_session".$sid."\"].reset(); //then reset the form values
              }
            </script>
            </td>";


            I have moved the submit script to inside the <td><form> block so it is present in, and specific to, each form.
            Which produces a result as per below:

            <td>
            <form name="edit_session959" id="edit_session" method="post" action="../assets/modules/coursemanager/process.cm.php">
            <input type="hidden" name="edit" value="1">
            <input type="text" size="3" name="new_places" id="new_places" value="">
            <input type="hidden" name="sid" value="959">
            <input type="button" name="Submit" value="edit places" onclick="return resetForm();">
            </form>
            <script language="javascript">
            function resetForm()
            {
                document.forms["edit_session959"].submit(); //first submit the form values
                document.forms["edit_session959"].reset(); //then reset the form values
              }
            </script>
            </td>


            Now each button processes as far as the not empty condition (see function below)

            // ******** EDIT SESSION PLACES ********** WSG
            if($_POST['edit']==1 && $_POST['new_places']!=""){
            $id=$_POST['sid'];
            $z=$_POST['new_places'];
            $sql = "UPDATE `rsacours_modx`.`xtra_session` SET `places`=$z WHERE `xtra_session`.`id`=$id";
            $add = $modx->db->query($sql);
            $msg = ($add? "The available places have been updated" : "error: ".mysql_error());
            }
            else {
            echo "Please enter a number of places to change";
            }


            but does not receive any input value when one is entered in the new_places form input.
            EXCEPT - very oddly - the last row does now does get updated, the rest of the input boxes produce the "Please enter a number of places to change" error message.
            (see code excerpt below where the problem seems to be coming from??)

            <input type=\"text\" size=\"3\" name=\"new_places\" id=\"new_places\" value=\"\" />


            Why, I don't know - it seems correct coding to me? [ed. note: Twobears last edited this post 7 years, 8 months ago.]
            • How is this involved with MODX? MODX does not use inline PHP.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
              • Quote from: sottwell at Jan 19, 2014, 03:29 AM
                How is this involved with MODX? MODX does not use inline PHP.
                True, it is part of an installed Module that came with a MODX website I am working on.
                • Hi, in case this is useful to anyone
                  I have resolved this little problem by simplifying the whole form for updating the database and removing the need for the inline php as follows.
                  Form code:
                  <form name="change_places" method="post" action="../assets/modules/coursemanager/process.cm.php">
                  <input type="hidden" name="change" value="1" />
                  <h2 class="section">Change Available Places</h2>
                  <table border="0">
                  <th>Session ID</th><th>New Place #</th><th></th>
                  <tr>
                  <td><input type="text" size="5" name="update_id" id="update_id" value="" /></td>
                  <td><input type="text" size="3" name="update_places" id="update_places" value="" /></td>
                  <td><input type="button" name="Submit" value="update places" onclick="return changePlaces();"/></td>
                  </tr>
                  </table>
                  <script language="javascript">
                  function changePlaces()
                  {
                      document.forms["change_places"].submit(); //first submit the form values
                  	document.forms["change_places"].reset(); //then reset the form values
                    }
                  </script>
                  </form>


                  and the form processing script:
                  if($_POST['change']==1) {
                  	if($_POST['update_id']!=""){
                  	   if($_POST['update_places']!=""){
                  $id=$_POST['update_id'];
                  $z=$_POST['update_places'];
                  $sql = "UPDATE `xtra_session` SET `places`=$z WHERE `xtra_session`.`id`=$id";
                  $add = $modx->db->query($sql);
                  $msg = ($add? "The available Course Places have been updated" : "error: ".mysql_error());
                  }
                  else {
                  echo "Please enter a number of places to change";
                   }
                  }
                  else {
                  echo "Please enter a Session ID number to change";
                   }
                  }
                  
                  • Ah. Working with legacy stuff is always a pain in the neck. Good work on solving the problem.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                    • Old thread I came across, just want to warn anyone not savvy, $z and $id above should be sanitized before being passed into db->query(). Best to use sql bind placeholders, or call a function to sanitize.