-3
<?php
    $con = mysqli_connect("localhost", "root", "", "" ) or die("Neuspjelo spajanje");
        function InsertUser(){  global $con;
            if(isset($_POST['sign_up'])){   
        $name = mysqli_real_escape_string($con, $_POST['u_name']);
        $pass = mysqli_real_escape_string($con,$_POST['u_pass']);
        $email = mysqli_real_escape_string($con,$_POST['u_email']);
        $country = mysqli_real_escape_string($con,$_POST['u_country']);
        $gender = mysqli_real_escape_string($con,$_POST['u_gender']);
        $b_day = mysqli_real_escape_string($con,$_POST['u_birthday']);      
        $date = date("m-d-Y");
        $status = "unverified";
        $posts = "No";

        $get_email = "select * from users where user_email='$email'";
        $run_email = mysqli_query($con, $get_email);
        $check = mysqli_num_rows($run_email);


$insert = "insert into users (user_name, user_pass, user_email, user_country, user_gender, user_b_day, 
            user_image, register_date, last_login, status, posts) values 
            ('$name','$pass', '$email', '$country', '$gender', '$b_day', 'default.jpg',
             '$date', '$date', '$status', '$posts')";
                $run_insert = mysqli_query($con, $insert);
        $result = mysql_query($insert);

        if($result){

            echo "<script>alert ('You're successfully registered!')</script>";
            echo "<script>window.open('home.php', '_self')</script>";
        }






            }

}
?>
Rocket Hazmat
  • 204,503
  • 39
  • 283
  • 323
Fmaster
  • 1
  • 1
  • Please describe your problem, what you're trying to achieve, what you've tried so far and so on. With just a line of code, nobody will understand your problem. – tillz Sep 14 '15 at 15:54

3 Answers3

1

You can't echo javascript and run it in a page that's already loaded. This would need to be the result of an ajax call on the client side with your redirects occuring from your ajax callbacks. If you're ok with ditching the alert, you can just issue a redirect from php:

header('Location: home.php');

To do it ajaxy:

$.ajax({
   type: "GET",
   url: "your_insert_user.php"
}).success(function(xhr) {
   alert ("You're successfully registered!");
   window.open('home.php', '_self');
}).fail(function (jqXHR, status, errorThrown) {
   //something else here
});

But, why would you want to issue an ajax call just to redirect? Additionally, you need to issue the appropriate responses from your insert script:

if ($result) { echo ""; } //issues a "200 OK"
else { header("HTTP/1.1 422 Unprocessable Entity"); } //fires the failure callback in ajax

I would pass a conditional GET or POST paramater to home.php with some value flag and display your message there.

eggmatters
  • 1,127
  • 10
  • 25
0

Based on what you post above, you are dealing with two separate issues here.

You say "it inserts" so I'm assuming that means that the mysql query to insert the new row into your database completes successfully. Then you send some HTML code, containing a (somewhat mangled) Javascript snippet, to the browser, which is supposed to issue a redirect request to the client's web browser, which doesn't have the desired result, seeing as you write that it "won't redirect".

Keep in mind that redirection is performed by the browser, is dependent on the browser's capabilities and/or settings, and requires proper javascript in the first place.

How do properly request a redirect from the browser has been discussed before on SO.

Community
  • 1
  • 1
0

First of all,remove this line $result = mysql_query($insert); then modify your code and add this, hope it will work:

$run_insert = mysqli_query($con, $insert);
if($run_insert){
  echo "<script>alert ('You\'re successfully registered!')</script>";
  echo "<script>window.open('home.php', '_self')</script>";
}
Payer Ahammed
  • 814
  • 5
  • 15