-1

I have a form which i want to post problem is this when ever page refresh form submit auto I just want form only form submit when I click on the button.otherwise form not post thanks in advance

    <?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>

here is the full code.i edit it

Malik
  • 21
  • 8

1 Answers1

0

A Post/Redirect/Get design would be a viable solution to your problem. An example of code that would accomplish this is:

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

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

The header function then redirects the user to the same page, which means that the $_POST information is lost, hence refreshing the page would not re-send the form.

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • will you please edit my full code how to use it i am new in php – Malik May 31 '16 at 07:16
  • I'm guiding you on how you can fix the problem. If you give me 2 pages of code to fix, I would then not be doing this site justice since it is a Q and A forum. But to help guide you again in the right direction, place something like `echo "test
    ";` under the `// here is where your code would be included` comment in my above code. You'll see that if you load the page the first time (when submitting the form), you'll see "test" on the page, however when refreshing, it will no longer be there.
    – Webeng May 31 '16 at 07:20
  • @Malik oh and if this answer is the one that worked for you, please accept it (green checkmark) so that future readers/users know it worked for you. – Webeng May 31 '16 at 07:21
  • when i refresh page without click on button form auto submit – Malik May 31 '16 at 07:24
  • @Malik edit your question and include the php that manages the form so that I can see exactly what is happening. – Webeng May 31 '16 at 07:25
  • @Webeng, This is not the solution what Malik is asking – Deepak Dholiyan May 31 '16 at 07:31
  • @Malik , I have checked your code and this is working fine – Deepak Dholiyan May 31 '16 at 07:31
  • i have two problem one is form auto submit when page reload and other button not destroy session here – Malik May 31 '16 at 07:33
  • @Malik/Webeng please upvote my answer for balance. You please check that any header() or js is not doing so. – Deepak Dholiyan May 31 '16 at 07:33
  • when i click on logout button its not destry the seesion – Malik May 31 '16 at 07:37
  • @webeng can we apply this to any one form only? – Malik Jun 01 '16 at 05:51