php问题表单在页面重新加载时提交自动

时间:2016-05-31 06:47:01

标签: php html

我有一个表格,我想发布问题是这个,当页面刷新表单提交自动我只想表单只有表单提交,当我点击按钮。其他表格不发布提前感谢

    <?php
session_start();

//mail("ifaisal24@gmail.com","hhhhhh",print_r($_REQUEST,TRUE));
define('HOST','xxxxx');
define('USER','xxxxx');
define('PASS','Ixxxxx_');
define('DB','xxxx');

$con = mysqli_connect(HOST,USER,PASS,DB);
require_once 'include/DB_Functions.php';
 if (!isset($_SESSION['email'])){
     ?>
     <script>
     window.location = 'login2.php';
     </script>
 <?php
 }


$db = new DB_Functions();

$response = array("error" => FALSE);
if (isset($_POST['sender_email'])&& isset($_POST['reciver_email'])&& isset($_POST['data'])) {

    // receiving the post params

     $sender_email = $_SESSION['email'];
     $_POST['sender_email']=$_SESSION['email'];
     $reciver_email = $_POST['reciver_email'];
     $data =$sender_email.' : '. $_POST['data'];

        ///echo $sender_email;
        //die();

        // create a new chat
        $user_data = $db->storeData($data,$sender_email,$reciver_email);
        //die;
        if ($user_data) {
            // chat stored successfully
            $response["error"] = FALSE;
           // $response["uid"] = $user_data["unique_id"];
            $response["user_data"]["sender_email"] = $user_data["sender_email"];
            $response["user_data"]["reciver_email"] = $user_data["reciver_email"];
            $response["user_data"]["data"] = $user_data["data"];
            $response["user_data"]["created_at"] = $user_data["created_at"];

            echo json_encode($response);
        }
                        }


    //    if (isset($_REQUEST['sender_email'])&& isset($_REQUEST['reciver_email'])&& isset($_REQUEST['fatch_server_data'])) {

        //                 $sender_email = $_REQUEST['sender_email'];
          //                 $reciver_email = $_REQUEST['reciver_email'];

         $sql = "select * from user_data WHERE (sender_email='$sender_email' AND reciver_email='$reciver_email') OR 
         (reciver_email ='$sender_email' AND sender_email='$reciver_email')" ;

        //$sql = "select * from user_data";
                          $res = mysqli_query($con,$sql)or mysql_error();
        $resultt = array(); 
            while($row = mysqli_fetch_array($res)){
                array_push($resultt,
                array(

                'data'=>$row[3],
                'created_at'=>$row[4]
                //   'email'=>$row[3]
                ));
            }  
            //            }         
 json_encode(array("resultt"=>$resultt));

mysqli_close($con);

?>

<?php
define('HOST','mxxxxx');
define('USER','axxxxxxx');
define('PASS','Ixxxxxxx_');
define('DB','axxxxxxxx');

$con = mysqli_connect(HOST,USER,PASS,DB);

$sql = "select * from users";

$res = mysqli_query($con,$sql);
$result = array();

    while($row = mysqli_fetch_array($res)){
    array_push($result,
    array(
     //  'id'=>$row[0],
      'email'=>$row[3], 
));
} 
 json_encode(array("result"=>$result));
mysqli_close($con);

?>
<html>
<head>
<style>
#userschat { 
    font-family:tahoma;
    font-size:12px;
    color:black;
    border: 5px teal solid;
    height: 325px;
    width: 300px;

 overflow-y:scroll;
}
#usersOnLine { 

    font-family:tahoma;
    font-size:12px;
    color:black;
    border: 10px teal solid;
    height: 625px;
    width: 300px;
 overflow-y:scroll;
}
#allcon{
    margin-bottom: 15px;
    font-family:tahoma;
    font-size:12px;
    color:black;
    border: 10px teal solid;
    height: 40px;
    width: 300px;
}
#contact{
    float:right;
}
#all{
    float:left;
}
#data {
    width: 250px;
    height: 30px;
    border: 5px teal solid;
    float:left;
    margin-top:1px;
}

#send {
    border: 5px teal solid;
    width: 57px;
    height: 30px;
    float:right;
    margin:1px;
}
.container{
    width:970px;
    height:auto;
    margin:0 auto;
}
#messageForm{

}
.clickAble{
                    display: inline-block;;
                    cursor:pointer; 
                }

</style>
<meta http-equiv="refresh" content="600">
</head>
<body bgcolor="beige">>
<div class="container">

<form method="post" action="login2.php">
<input type="submit" id="l_btn" name="l_btn" value="Logout" />

</form>

<?php
if ($_POST) {
   // here is where your code would be included
    session_destroy();
   header("Location:chat.php" . $_SERVER['REQUEST_URI']);
   exit();
}


?>


<div id = "welcome"><h2>Welcome to Chat</h2>
<?php echo $_SESSION['email']; ?>
</div>

<div id = "all"> 
<h1> Chat Box</h1>
<div id="receiver">
                <h3>receiver</h3>
                <!-- NOTICE THAT THIS IS NEW NOW... -->
                <!-- IT'S IMPORTANT THAT YOU HAVE THIS PARAGRAPH EXACTLY AS IT IS HERE -->
                <p class='bubbled-data'></p>
            </div>
<div id="sender"><h3>sender</h3>
    <?php echo $_SESSION['email']; ?>
    </div>
<div id="userschat">
<h4 style = " margin-top: 4px; margin-left: 7px">
<?php
foreach($resultt as $key => $val)
{
    echo $val['data'];
    echo "<br>";
    echo $val['created_at'];   
    echo "<br>";
    echo "<br>";
}
 ?>
 </h4>
</div>
<form id="messageForm" action = "<?php $_PHP_SELF ?>" method = "POST">

    <input type="hidden" name="sender_email" id="sender_email" value="">
    <input type="hidden" name="reciver_email" id="reciver_email" value="">
<input type="text" name="data" id="data"   placeholder="data" onFocus="fun1(this)" onBlur="fun2(this)" required=""   />

    <input id="send" type="submit" value="Send">
</form>
</div> 

<div id = "contact">
<div id = "allcon"> 
  <h1 style = "  text-align: center; margin-top: 4px;">   All Contacts   </h1>
</div>
<div id="usersOnLine" >
<h2 style = " margin-top: 4px; margin-left: 7px">
<?php
          foreach($result as $key => $val){
          echo "<span class='clickAble'>" . $val['email'] . "</span>";
          echo "<br>";
          echo "<br>";
         }
 ?>
 </h2>
</div>
</div>

</div>





        <!-- JAVASCRIPT - JQUERY -->
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
       <script type="text/javascript">

       var objDiv = document.getElementById("userschat");
        objDiv.scrollTop = objDiv.scrollHeight;

    (function ($) {
        $(document).ready(function (e) {
            var receiver = $("#receiver");
            var clickAbles = $(".clickAble");

            receiver.find(".bubbled-data").text(localStorage.getItem("nameValue"));
           $("#reciver_email").val(localStorage.getItem("nameValue"));
            // BIND THE clickAble SPAN TO AN ON-CLICK EVENT...
            // SO THAT WITH EACH CLICK, YOU CAN SIMPLE APPEND THE CONTENT OF THAT SPAN TO THE RECEIVER DIV
            clickAbles.on("click", function (evt) {
                var nameValue = $(this).text();
                receiver.find(".bubbled-data").text(nameValue);

                $("#reciver_email").val(nameValue);
                localStorage.setItem("nameValue", nameValue);


            });

        });
    })(jQuery);
        </script>

</body>
</html>

这里是完整的代码。我编辑它

1 个答案:

答案 0 :(得分:0)

Post/Redirect/Get设计将是解决您问题的可行方案。完成此任务的代码示例如下:

if ($_POST) {
   // here is where your code would be included

   header("Location: " . $_SERVER['REQUEST_URI']);
   exit();
}

然后header函数将用户重定向到同一页面,这意味着$_POST信息丢失,因此刷新页面不会重新发送表单。