1

I have passed some values from a page to another using ajax with request method post. But there is one condition that f some one is directly accessing the url, it should be redirected to some other page. Problem is that its not getting redirected (In else condition in img.php) . Can any one tell me what mistake I am committing?

Thanks in advance.

Code:-

imageupload.php:

document.getElementById("submit").addEventListener("click", function(event){
    event.preventDefault();
    saveImgfunc();
});

function saveImgfunc(){
    var form = new FormData(document.getElementById('saveImg'));
    var file = document.getElementById('imgVid').files[0];
    if (file) {   
        form.append('imgVid', file);
    }
    $.ajax({
        type : 'POST',
        url : 'core/img.php',
        data : form,
        cache : false,
        contentType : false,
        processData : false
    }).success(function(data){
        document.getElementById('msg').innerHTML = data;
    });
}

img.php:

<?php
require '../core.php';
$qry = new ProcessQuery('localhost', 'root', '', 'mkart');

$uid = 6;

if($_SERVER["REQUEST_METHOD"] == "POST"){
//Some code here
}
else{
    header("Location : ../core.php");
}
Loïc
  • 11,804
  • 1
  • 31
  • 49
Vineet Mishra
  • 100
  • 1
  • 11

4 Answers4

3

See this post https://stackoverflow.com/a/21229246/682754

There's a good chance that you may have some whitespace before you use the header function? Perhaps in the form of a hidden error/warning.

Try the following at the top of your PHP code in img.php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);

Would advise removing that once you've found your issue

Community
  • 1
  • 1
Carlton
  • 5,533
  • 4
  • 54
  • 73
  • @VineetMishra this post is about headers already sent. Follow the link, and do the @ob_start() before saying this won't work./ – Loïc May 12 '15 at 11:07
1

It works for me removing the whitespace between Location and :

header("Location: ../core.php");
moonknight
  • 334
  • 1
  • 7
0

Error found. In the core.php thre is one code which stopping the further execution of code. Its specially coded for uid = 6. Thanks for your time.

Vineet Mishra
  • 100
  • 1
  • 11
  • Glad you found the issue, I would definitely look into a tool called xdebug if you don't know about it already. It will help you identify the flow through your code and you can be 100% sure of the flow without printing echos/var_dump/exits etc. We've all been there but xdebug is an amazing thing to have, trust me – Carlton May 12 '15 at 11:26
0

Dont use header("Location : ../core.php"); some time it gives error or not working properly so use javascript redirection

like

?>
 <script>window.location='../core.php';</script>
<?php
Rahul Saxena
  • 465
  • 4
  • 15