-3

I have a very simple code which checks whether email address exists in the table or not. If it exists then alert Already Exists and redirect to another page and if it doesn't exist then insert into table. Inserting when the email address is not present in the table is happening properly and is getting redirected to the respected page but when the email is existing in the table then although it alerts that username exists, it shows headers already sent at error

My code is

<?php   
    require_once('connection.php'); 
    $tbl_name="cust_contacts";      
        $ccode=$_POST['ccode'];
        $name=$_POST['name'];
        $des=$_POST['desig'];
        $dep=$_POST['dep'];
        $phone=$_POST['phone'];
        $mobile=$_POST['mob'];
        $email=$_POST['email']; 
        $check="select * from $tbl_name where email='$email'";
        $checkqry=mysql_query($check);
        if($checkqry['error'])
            die(mysql_error()); 
        $countcheck=mysql_num_rows($checkqry);
        if($countcheck==0)
        {
            $qry="insert into $tbl_name(cust_code,name,designation,department,phone,mobile,email) values('$ccode','$name','$des','$dep','$phone','$mobile','$email')";
            if (!mysql_query($qry))
            {
                die('Error: ' . mysql_error());
            }       
            header("location:index.php?customername=$ccode#pop4");
        }
        else
        {
            echo '<script type="text/javascript">alert("User already exists. Try editing the user");</script>';
            header("location:index.php?customername=$ccode");
        }                   
    //$count=mysql_num_row($result);
    //if($count==1) 
?>

2 Answers2

0

If you want to show an alert message and redirect to another page, you can use JavaScript like this:

Befor (Your Original Code):

    echo '<script type="text/javascript">alert("User already exists. Try editing the user");</script>';
    header("location:index.php?customername=$ccode");

After:

 echo '<script type="text/javascript">';
 echo  'alert("User already exists. Try editing the user");';
 echo  'location.href = "index.php?customername=' . $ccode. '";';
 echo '</script>';

Hope this helps.

naota
  • 4,695
  • 1
  • 18
  • 21
  • how do I access $ccode?? Its javascript and $ccode is a php variable – user3472285 Apr 04 '14 at 10:01
  • @user3472285, Sorry, I eddited code above. I expected a valiable in double quotasion could be trancelated into value, but it was a single quote not a double quote. – naota Apr 04 '14 at 10:11
0

I had the same problem, and as far as i can see if the header() is use after some code then it throws an error, so maybe try removing the echo statement.

If that doesn't work, i used a function call. this the function:

function redirect($url) {
  if (!headers_sent()) {
      header('Location: '.$url);
      exit;
} else {
    echo '<script type="text/javascript">';
    echo 'window.location.href="'.$url.'";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
    echo '</noscript>'; exit;
}

}

And this is an example of the call:

if($mail->send()) {
        // redirect to thank you message.
        redirect( BASE_URL . "thanks/");
        exit;
    } else {
        $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
    }

Here i used it to redirect if an email was successful sent using PDO class, but it can used anywhere.

Frank
  • 11
  • 3