-1

I got an image gallery crud script and decided to make the edit image page an edit profile page. My issue is what I added doesn't redirect back to profile page. Here is my code :

<?php
// Start session
session_start();

// Include and initialize DB class
require_once 'DB.class.php';
$db = new DB();

// Fetch the users data
$images = $db->getRows('images');

// Get session data
$sessData = !empty($_SESSION['sessData'])?$_SESSION['sessData']:'';

// Get status message from session
if(!empty($sessData['status']['msg'])){
    $statusMsg = $sessData['status']['msg'];
    $statusMsgType = $sessData['status']['type'];
    unset($_SESSION['sessData']['status']);
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>SWARZIE</title>
<meta charset="utf-8">

<!-- Bootstrap library -->
<link rel="stylesheet" href="bootstrap/bootstrap.min.css">

<!-- Stylesheet file -->
<link rel="stylesheet" type="text/css" href="css/style.css">
<style>
  .form {
    width: 300px;
    margin: 0 auto;
}

  </style>
</head>
<body>
<div class="container">
    <h1>Image Gallery</h1>
    <hr>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
  <a class="navbar-brand" href="#">Swarzie</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarsExampleDefault">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <> <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <"></a>
      </li>
      <li class="nav-item">
        <></a>
      </li>
       <li class="nav-item">
        <></a>
    
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
      <button class="btn btn-secondary my-2 my-sm-0" type="submit">Search</button>
    </form>
        <form class="form-inline my-2 my-lg-0">
      <a href="profile.php" class="profile">Profile</a>
    </form> 
 <form class="form-inline my-2 my-lg-0">
      <a href="useaccount.php?logoutSubmit=1" class="logout">Logout</a>
    </form>
  </div>
</nav>
  <?php //from   ww  w  .j  a va2  s .c om
    if ( isset( $_POST["submitButton"] ) ) { 
      // (deal with the submitted fields here) 
      header( "Location: profile.php" ); 
      exit; 
    } else { 
      displayForm(); 
    } 
    function displayForm() { 
    ?> 
    <!DOCTYPE html5> 
    <html> 
      <body>
        <form action="profile.php">
  <label for="img">Select image:</label>
  <input type="file" id="img" name="img" accept="image/*">
  <input type="submit">
</form>
        <form action="profile.php" method="post"> 
           <label for="firstName">First name</label> 
            <input type="text" name="firstName" id="firstName" value="" /> 
            <label for="lastName">Last name</label> 
            <input type="text" name="lastName" id="lastName" value="" /> 
<textarea rows="2" cols="30" name="comment" form="usrform">
About</textarea>
            <input type="submit" name="submitButton" id="submitButton" value= "Send Details" /> 
        </form> 
      </body> 
    </html> 
    <?php 
    } 
    ?> 

  
  

    <!-- Display status message -->
    <?php if(!empty($statusMsg)){ ?>
    <div class="col-xs-12">
        <div class="alert alert-<?php echo $statusMsgType; ?>"><?php echo $statusMsg; ?></div>
    </div>
    <?php } ?>
    
    <div class="row">
        <div class="col-md-12 head">
            <h5>Images</h5>
            <!-- Add link -->
            <div class="float-right">
                <a href="addEdit.php" class="btn btn-success"><i class="plus"></i> New Image</a>
            </div>
        </div>
        
        <!-- List the images -->
        <table class="table table-striped table-bordered">
            <thead class="thead-dark">
                <tr>
                    <th width="5%"></th>
                    <th width="12%">Image</th>
                    <th width="45%">Title</th>
                    <th width="17%">Created</th>
                    <th width="8%">Status</th>
                    <th width="13%">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if(!empty($images)){
                    foreach($images as $row){
                        $statusLink = ($row['status'] == 1)?'postAction.php?action_type=block&id='.$row['id']:'postAction.php?action_type=unblock&id='.$row['id'];
                        $statusTooltip = ($row['status'] == 1)?'Click to Inactive':'Click to Active';
                ?>
                <tr>
                    <td><?php echo '#'.$row['id']; ?></td>
                    <td><img src="<?php echo 'uploads/images/'.$row['file_name']; ?>" alt="" /></td>
                    <td><?php echo $row['title']; ?></td>
                    <td><?php echo $row['created']; ?></td>
                    <td><a href="<?php echo $statusLink; ?>" title="<?php echo $statusTooltip; ?>"><span class="badge <?php echo ($row['status'] == 1)?'badge-success':'badge-danger'; ?>"><?php echo ($row['status'] == 1)?'Active':'Inactive'; ?></span></a></td>
                    <td>
                        <a href="addEdit.php?id=<?php echo $row['id']; ?>" class="btn btn-warning">edit</a>
                        <a href="postAction.php?action_type=delete&id=<?php echo $row['id']; ?>" class="btn btn-danger" onclick="return confirm('Are you sure to delete data?')?true:false;">delete</a>
                    </td>
                </tr>
                <?php } }else{ ?>
                <tr><td colspan="6">No image(s) found...</td></tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

End result would have the form action(name, text area and profile upload)content to show up above the gallery crud on the profile.php file. How do I do this?

Thank you :)

Meka
  • 51
  • 5

2 Answers2

1

Header function should be called before any output is made.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. see more info in PHP.net

As you are calling header("Location...") after part of your html has been outputed, it will not work.

You should try moving the logic with the header(...) to the top of the file.

francisco neto
  • 797
  • 1
  • 5
  • 13
1

When you redirect to another page you should have to carry the value of $_POST with your page, otherwise you will get null of it. You can carry with session. You can do it just before the statement where you redirect to another page, like

$_SESSION['post_data']=$_POST;
header('Location:profile.php'); exit;

Now you have posted data in the session.

Anisur Rahman
  • 644
  • 1
  • 4
  • 17