Hi everyone, I wanted to display different data from the same table in 2 different tabs in php. When i select the Responsible person and click Notify in tabs-1, it will be saved and displayed in the improvement_plan table. But when i select Responsible person and click Notify in tabs-2, Responsible person is not save and not display in the improvement_plan table. Please advise how to solve this matter. Thanks a lot.

   <form action="progress.php" method="post" form name="progress" id="progress"> 
    <div id="tabs-1">    
        <input type="hidden" name="Progressid" id="Progressid"  value="<?php echo $row['Progressid']; ?>"> 
        <p><b>3.Improvement Plan</b></p>
        <p>Team Leader:<input type="text" readonly="readonly" name="Position" id="Position" value="<?php echo $_SESSION["Position"];?>" ></p> 
    <?php
    Session_start();
    $cn=mysql_connect("localhost","user","") or die(mysql_error());
    mysql_select_db("pq",$cn) or die(mysql_error());

    $_SESSION['Progressid']=$row['Progressid'];

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

    $responsible = filter_input(INPUT_POST, 'responsible2', FILTER_SANITIZE_STRING);

    $q = "SELECT Email FROM user WHERE Position = '".$responsible."'";
    $r = mysql_query($q) or die(mysql_error());
    while($row = mysql_fetch_array($r))
     {
    "Email:".$row['Email']."<br/>";
    $cemail2=$row['Email'];
     } 
    echo $cemail2;
    echo $_SESSION['Progressid'];
    $responsible2=$_POST['responsible2'];
    echo $responsible2;
    echo $_SESSION['Email'];
    echo $_SESSION['Username']; 

    mysql_query("INSERT INTO improvement_plan (Responsible2,Progressid) VALUES ('" . $responsible2 . "', '" . $_SESSION['Progressid'] . "')");
            $Ipid = mysql_insert_id();
            # send the email
            if( ! empty($Ipid))
            {
              $message = "New improvement plan added successfully";

    require 'PHPMailer_5.2.4/PHPMailer_5.2.4/class.phpmailer.php';
    $mail = new PHPMailer;
    $mail->IsSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.office365.com';   // Specify main and backup server
    $mail->Mailer   = "smtp";
    $mail->SMTPAuth = false; 
    $mail->IsHTML(true);                                  // Set email format to HTML
    $mail->ContentType = "text/html";
    $mail->From =($_SESSION['Email']);                         
    $mail->FromName =($_SESSION['Username']);   
    $mail->AddAddress("$cemail2");                       // Name is optional
    $mail->Subject = 'system Notification';
    $mail->Body    = 'IP system ';

    if(!$mail->Send()) {
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
       exit;
    }else{
        echo 'Message has been sent'; 
    }  
    }
    }
    $responsibles = mysql_query("SELECT * FROM user"); 
    ?>
        <input type="hidden" name="Progressid" value="<?php echo $row['Progressid']; ?>" />
        <table width="850" border="1">
          <tr align="center">
           <th width="70" scope="col">Ipid</th>
          <th width="70" scope="col">Item</th>
          <th width="80" scope="col">Responsible</th>
            <select name="responsible2" id="responsible2">
            <?php
                while($row = mysql_fetch_array($responsibles))
                    echo "<option value=\"{$row['Position']}\">{$row['Position']}</option>";
            ?>
            </select>
            <input type="submit" name="Notify2" value="Notify"/> 
         </tr>   
    <?php 

        $disable = '';
        $i = 0;
        $d = 0;

    $con = mysql_connect("localhost","user","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("pq", $con);    
    $Progressid= $_SESSION['Progressid'];
    $sql = "SELECT * FROM improvement_plan where  Progressid ='" . $Progressid . "'";
    $res_list = mysql_query($sql);

    while($row_list = mysql_fetch_array($res_list)){
        $i++;       
    ?>  
     <td align="center"><div name="Ipid" id="Ipid<?php echo $i; ?>"><?php if($row_list['Ipid'] != '0000-00-00'){ echo ($row_list['Ipid']); }else{ echo '-'; } ?></div></td>
     <td align="center"><div name="Item2" id="Item2<?php echo $i; ?>"><?php if($row_list['Item2'] != '0000-00-00'){ echo ($row_list['Item2']); }else{ echo "-"; } ?></div></td>
     <td align="center"><div name="Responsible2" id="Responsible2<?php echo $i; ?>"><?php if($row_list['Responsible2']!= '0000-00-00'){ echo ($row_list['Responsible2']); }else{ echo '-'; } ?></div></td>
     <td align="center"><div name="Progressid" id="Progressid<?php echo $i; ?>"><?php if($row_list['Progressid'] != '0000-00-00'){ echo ($row_list['Progressid']); }else{ echo '-'; } ?></div></td>
     </tr> 
    <?php
    } 
    if($i == 0){
    ?>
    <tr><td colspan="7">No data.</td></tr> 
    <?php
        $disable = 'disabled="disabled"';
    }
    ?>
    <br>
    </table>
    <br>
    <?php
    mysql_close($con);  
    ?>  
    </div>

    <div id="tabs-2">
        <input type="hidden" name="Progressid" id="Progressid" value="<?php echo $row['Progressid']; ?>"> 
        <p><b>3.Improvement Plan</b></p>
        <p>Team Leader:<input type="text" readonly="readonly" name="Position" id="Position" value="<?php echo $_SESSION["Position"];?>" ></p>
    <?php
    Session_start();
    $cn=mysql_connect("localhost","user","") or die(mysql_error());
    mysql_select_db("pq",$cn) or die(mysql_error());

    echo $_SESSION['Progressid'];

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

    $responsible1 = filter_input(INPUT_POST, 'responsible4', FILTER_SANITIZE_STRING);

    $q = "SELECT Email FROM user WHERE Position = '".$responsible1."'";
    $r = mysql_query($q) or die(mysql_error());
    while($row = mysql_fetch_array($r))
     {
    "Email:".$row['Email']."<br/>";
    $cemail4=$row['Email'];
     } 
    echo $cemail4;
    echo $_SESSION['Progressid'];
    $responsible4=$_POST['responsible4'];
    echo $responsible4;
    echo $_SESSION['Email'];
    echo $_SESSION['Username']; 

    $sql=("Update improvement_plan set Responsible4='" . $responsible4 . "' WHERE Progressid='" . $_SESSION['Progressid'] . "'");
    {
    $result = mysql_query("SELECT * FROM improvement_plan WHERE Progressid='" . $_SESSION['Progressid'] . "'"); 
    $row= mysql_fetch_array($result); 

    require 'PHPMailer_5.2.4/PHPMailer_5.2.4/class.phpmailer.php';
    $mail = new PHPMailer;
    $mail->IsSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.office365.com';   // Specify main and backup server
    $mail->Mailer   = "smtp";
    $mail->SMTPAuth = false; 
    $mail->IsHTML(true);                                  // Set email format to HTML
    $mail->ContentType = "text/html";
    $mail->From =($_SESSION['Email']);                         
    $mail->FromName =($_SESSION['Username']);   
    $mail->AddAddress("$cemail4");                       // Name is optional
    $mail->Subject = 'system Notification';
    $mail->Body    = 'IP system';

    if(!$mail->Send()) {
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
       exit;
    }else{
        echo 'Message has been sent'; 
    }  
    }
    }
    $responsibless = mysql_query("SELECT * FROM user"); 
    ?>
        <input type="hidden" name="Progressid"  value="<?php echo $row['Progressid']; ?>" />
        <table width="850" border="1">
          <tr align="center">
           <th width="70" scope="col">Ipid</th>
           <th width="70" scope="col">Item</th>
           <th width="80" scope="col">Responsible</th>
            <select name="responsible4" id="responsible4">
            <?php
                while($row = mysql_fetch_array($responsibless))
                    echo "<option value=\"{$row['Position']}\">{$row['Position']}</option>";
            ?>
            </select>
            <input type="submit" name="Notify4" value="Notify"/> 
         </tr>   
    <?php 

        $disable = '';
        $i = 0;
        $d = 0;

    $con = mysql_connect("localhost","user","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("pq", $con);    

    $Progressid= $_SESSION['Progressid'];
    $sql = "SELECT * FROM improvement_plan where  Progressid='" . $Progressid . "'";
    $res_list = mysql_query($sql);

    while($row_list = mysql_fetch_array($res_list)){
        $i++;       
    ?>  
             <td align="center"><div name="Ipid" id="Ipid<?php echo $i; ?>"><?php if($row_list['Ipid'] != '0000-00-00'){ echo ($row_list['Ipid']); }else{ echo '-'; } ?></div></td>
             <td align="center"><div name="Item4" id="Item4<?php echo $i; ?>"><?php if($row_list['Item4'] != '0000-00-00'){ echo ($row_list['Item4']); }else{ echo "-"; } ?></div></td>
             <td align="center"><div name="Responsible4" id="Responsible4<?php echo $i; ?>"><?php if($row_list['Responsible4']!= '0000-00-00'){ echo ($row_list['Responsible4']); }else{ echo '-'; } ?></div></td>
             <td align="center"><div name="Progressid" id="Progressid<?php echo $i; ?>"><?php if($row_list['Progressid'] != '0000-00-00'){ echo ($row_list['Progressid']); }else{ echo '-'; } ?></div></td>
        </tr> 
    <?php
    } 
    if($i == 0){
    ?>
        <tr><td colspan="7">No data.</td></tr> 
    <?php
        $disable = 'disabled="disabled"';
    }
    ?>
        <br>
        </table>
        <br>
        <?php
        mysql_close($con);
    ?>
        </div>
        <td colspan="2"><input type="submit" name="submit" id="submit" value="Save" class="btnSubmit"></td>    
        </form>  

Hi,

I suggest you to print the contents of the POST request to see what is actually sent to the script, so on top of the progress.php file simply put:

<?php

    if($_POST)
        die("<pre>". print_r($_POST, true) ."</pre>");

At the moment I see you have duplicated input fields like Progressid, by doing this you get unwanted behaviour as one of the two fields will be overwritten, the same happens with the input field named Position. It is not only related to the name attribute (which, is some cases is allowed: arrays name="Progressid[]" or in radio buttons) but it is also related to the id attribute, that must be unique over the page. In your case Progressid is set two times in the form and the format ProgressidN (where N is $i++) is set in both tabs.

In addition, but not related to the form submission, you're applying the name attribute to div tags, this is not allowed.

Also, you're initializing the session (session_start()) multiple times: set it only once on top of the page, otherwise you won't get the values of $_SESSION, unless the session is already initialized on the portion of code you did not posted.

And finally on lines 3, 119 and 177 you wrote:

<input type="hidden" name="Progressid" id="Progressid" value="<?php echo $row['Progressid']; ?>">

But I don't see when you set $row for the first time. I think the problem is here: after line 3, on line 19 you have:

while($row = mysql_fetch_array($r))

The function mysql_fetch_array() will overwrite the original $row setting it to boolean FALSE when there are no more results to loop. In other cases $row will assume the last value of the loop, for example:

$i = 2;

while($i <= 5)
{
    var_dump($i);
    $i++;
}

var_dump($i);

Outputs:

# in loop
int(2)
int(3)
int(4)
int(5)

# out of the loop
int(6)

So, if you relate to the same variable name, there are chances that it will be overwritten by something else, some loops in this case.

I really suggest you to read this:

Hi cereal, thanks a lot for your reply. When i remove the $_SESSION['Progressid'] and just maintain the hidden field Progressid, now there is no data updated in the table. Please advise. Thanks.

At the moment, fix the variable, id & name attributes issues (i.e. avoid repetition), then according to the changes fix your update queries with the new names and then try my first advice:

print the contents of the POST request to see what is actually sent to the script, so on top of the progress.php file simply put:...

run it on both tabs and return the results here.

Hi cereal, have tried to change the variable. Now the Responsible2 is displayed in the table in tabs-2. However, the Responsible4 still not save and display as supposed to be when i click Notify4 button. Below is the print content of the post request. Please advise. Thanks.

Array
(

[Progressid] => 408   
[Position] => Planning Manager
[responsible2] => Data Officer
[Notify2] => Notify
[responsible4] => Planning Manager

)
Array
(

[Progressid] => 408
[Position] => Planning Manager
[responsible2] => Data Officer
[responsible4] => Data Officer
[Notify4] => Notify

Ok, on line 147:

$sql=("Update improvement_plan set Responsible4='" . $responsible4 . "' WHERE Progressid='" . $_SESSION['Progressid'] . "'");
{

the query function is missing, so there is no update. Also the following curly bracket should not be there as this is used to delimit functions, classes or variables in strings: $a = "hello {$var}";

Thanks a lot. The query works now. Thanks!

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.