重新加载页面时php问题表单自动提交
我有一个表格,我想发布的问题是,每当页面刷新时,表格自动提交。我只想在我点击按钮时,表格只提交。否则,表格不发布,谢谢重新加载页面时php问题表单自动提交,php,html,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');
<?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>
window.location='login2.php';
设计将是解决您的问题的可行方案。实现这一点的代码示例如下:
if ($_POST) {
// here is where your code would be included
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
然后,标题
功能将用户重定向到同一页面,这意味着$\u POST
信息丢失,因此刷新页面不会重新发送表单。可能重复的只需检查echo'';打印(邮政美元);模具();值是多少?我将检查解决方案成功后,您只需重定向页面标题('Location:index.php');退出();请编辑我的完整代码如何使用我是phpI新手,我将指导你如何解决这个问题。如果你给我2页的代码来修复,我将不会做这个网站公正,因为它是一个Q和一个论坛。但是为了帮助您再次朝着正确的方向前进,可以放置类似于echo的“test
”在//下面的code>中,您的代码将包含在我上面的代码中的注释中。您将看到,如果您第一次加载页面(提交表单时),您将在页面上看到“test”,但在刷新时,它将不再存在。@Malik oh如果这个答案对您有效,请接受它(绿色复选标记)这样将来的读者/用户就会知道它对你有用。当我刷新页面而不点击按钮时,表单自动submit@Malik编辑您的问题,并包括管理表单的php,以便我可以准确地看到正在发生的事情。