使用header()重定向到另一个php脚本之前无法警告

时间:2014-02-14 15:47:03

标签: header

我正在尝试为练习创建一个虚拟的社交网站。我被困在一个发送好友请求的脚本中..

我创建了一个表单(带有单个文本输入,隐藏输入和提交按钮),登录用户可以在其中输入朋友的电子邮件地址。当他点击搜索按钮时,他获得了他朋友的姓名和电子邮件(使用sql获取)..

我已经在细节附近添加了一个锚标记(命名为发送请求)。当用户点击锚标签时,他被重定向到一个新的脚本,该脚本将朋友请求发送给他的朋友。第二个脚本只处理mysql。

在sql完成时,用户根据sql查询获取“成功发送好友请求”警报框或“已发送好友请求”警告框。现在我想在执行alertbox后将用户重定向到第一个脚本。

我尝试在alertbox之后使用header(“Location:send_request.php”)。但现在它将用户重定向到第一个脚本而不显示警告框。

这是我的第一个脚本(名为find_friends.php)

<?php

    if(isset($_SESSION)) session_start();
    require('header.php');
    echo "<form method='POST' action=''>";
        echo "<input type='text' name='search_user'/>";
        echo "<input type='hidden' name='search_form_status' value='sent'/>";
        echo "<input type='submit' value='Search User'/>";
    echo "</form>";


    if(isset($_POST['search_form_status']) && $_POST['search_form_status']=='sent'){
        $search_user = $_POST['search_user'];
        $sql_search_user = "SELECT id, name, email FROM registration where email=\"$search_user\"";
        $query_search_user = mysql_query($sql_search_user);
        $user_check = mysql_num_rows($query_search_user);
        if($user_check==1){
            $results_search_user = mysql_fetch_assoc($query_search_user);
                echo $_SESSION['searched_id'] = $results_search_user['id'];
                echo $_SESSION['searched_name'] = $results_search_user['name']."<br/>";
                echo $_SESSION['searched_email'] = $results_search_user['email'];
                echo "</br/><a href='send_request.php'>Send Request</a>";
        }   
    }
?>

这是第二个脚本(名为send_request.php)

<?php
    $db1 = mysql_connect("localhost", "root", "");
    mysql_select_db("test", $db1);
    $db2 = mysql_connect("localhost", "root", "", true);
    mysql_select_db("friends", $db2);
    if(!isset($_SESSION)) session_start();

    $searched_email = $_SESSION['searched_email'];
    $searched_id = $_SESSION['searched_id'];
    $user_name = $_SESSION['name'];
    $user_email = $_SESSION['email'];


    $sql_table_check = "SHOW TABLES LIKE 'friends_".$searched_id."'";
    $table_check = mysql_num_rows(mysql_query($sql_table_check, $db2));
    if($table_check==1){

        $sql_insert_values = "INSERT INTO friends_".$searched_id." VALUES(\"\", \"$user_name\", \"$user_email\", 0)";
        if(mysql_query($sql_insert_values)){
            echo "<script>alert('Friends Request Sent Successfully')</script>";
            header("Location: find_friends.php");
        }
        else{
            echo "<script>alert('Friends Request Already Sent')</script>";
            header("Location: find_friends.php");
        }       

    }else{

        $sql_create_table = "CREATE TABLE friends_".$searched_id." (friend_id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, friend_name VARCHAR(30) NOT NULL, friend_email VARCHAR(50) NOT NULL UNIQUE, active TINYINT NOT NULL)";

        $sql_insert_values = "INSERT INTO friends_".$searched_id." VALUES(\"\", \"$user_name\", \"$user_email\", 0)";

        mysql_query($sql_create_table, $db2);
        if(mysql_query($sql_insert_values, $db2)){
            echo "<script>alert('Friends Request Sent Successfully')</script>";
        }
    }
?>

1 个答案:

答案 0 :(得分:5)

试试这个:

 if(mysql_query($sql_insert_values)){
        echo "<script>
                   alert('Friends Request Sent Successfully');
                   window.location.href='find_friends.php';
             </script>";
    }