[英]Submit form without reloading or leaving current page (php- mysql)
先感謝您。 每次用戶接到電話時,他們都會以這種形式進行匯總,因為只有少數幾個用戶,所以不需要用戶驗證,但是其想法是,每次提交表單時,用戶名都會返回到輸入字段,因此用戶不必一遍又一遍地輸入他們的名字。 我的問題是,我有將代碼插入Db的php代碼的方式是,如果刪除此行,則重新加載相同的表單URL,但是一旦提交,它將帶我到空白頁。 我有一個PHP代碼,可以在會話中使用用戶名並將其寫在輸入字段中。 但由於我重新加載頁面,該會話被殺死。
問題是提交表單后如何在輸入字段中重新設置用戶名。
這是代碼:
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'xxxxx';
$dbpass = 'xxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "INSERT INTO callWrapper ".
"(data,user,date) ".
"VALUES('$_POST[DataEntered]','$_POST[user_name]',Curdate())";
mysql_select_db('ugsports');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
header('Location: http://careerdev.im.priv/test/thistest2.php'); /* here i'm reloading the page but if a remove this line of code, after a submit it will take to blank page*/
mysql_close($conn);
}
else
{
?>
/* stores the username in the session an return it in the input */
<?PHP
session_start();
$name = $_POST['username'];
$_SESSION['user'] = $name;
echo $_SESSION['user'];
?>
</head>
<body style="background-color:#D8D8D8;">
<form method="post" action="<?php $_PHP_SELF ?>">
<table style="width:320px;" cellpadding="5" cellspacing="5" align="center">
<tr>
<td colspan="4" style="background-color:#FF8000;">
<h1 align="center" style="margin:10px;padding:10px;">Call Wrapper</h1>
</td>
</tr>
<tr>
<td colspan="4" style="background-color:#eeeeee;">
<h5 align="center" style="margin:10px;padding:10px;"><?php echo date("d-m-y"); ?></h5>
<!--here-->
<INPUT TYPE = 'TEXT' Name ='user_name' VALUE="<?php echo $name ?>">
</td>
</tr>
<!-- start Menu sidebar -->
<tr>
<td style="background-color:#6E6E6E;width:150px;vertical-align:top;">
<p align="center"><b >Menu</b></p>
<br><br>
<button onclick="showTag('agent_call');" type="button" style="width: 100px; height: 40px;">Agent Call</button><br><br>
<button onclick="showTag('player_call');" type="button" style="width: 100px; height: 40px;">Player Call</button><br><br>
<button onclick="showTag('Runner_call');" type="button" style="width: 100px; height: 40px;">Runner Call</button><br><br>
</td>
<td align="center" style="background-color:#eeeeee;height:400px;width:300px;vertical-align:top;">
<select size="23" id="agent_call" class="DropDown" style="display: none" Name="DataEntered">
<optgroup label="Player Adjustment">
<option value="Agent called to make a Turn on/off">Turn on/off</option>
<option value="Agent called to make a credit Change">Credit Change</option>
<option value="Agent called to make a Temp Cred Change ">Temp Cred Change</option>
<option value="Agent called to Open New Customer">Open New Customer</option>
<option value="Agent called for other reason">Other</option>
</optgroup>
</select>
<select size="22" id="player_call" class="DropDown" style="display: none" Name="DataEntered">
<optgroup label="Account Adjustment">
<option value="Acc Adj-Change PW">Change PW</option>
<option value="Acc Adj-More Credit">More Credit</option>
<option value="Acc Adj-Turn off account">Turn off account</option>
</optgroup>
</select>
<select size="5" id="Runner_call" class="DropDown" style="display: none" Name="DataEntered">
<option value="Runner-Check item it">Check item it</option>
<option value="Runner-Check item it">Receive Work</option>
<option value="Runner-Check item it">Confirm/Ask for information</option>
<option value="Runner-Check item it">Problem</option>
<option value="Runner-Check item it">Other</option>
</td>
</tr>
<tr>
<td colspan="4" style="background-color:#FF8000;text-align:center;">
<img onclick="openWin();" src="Images/minimize.jpg" width="50" height="30" alt="20">
<input name="add" type="submit" id="add" style="font-size:10pt;color:white;background-color:#FF8000;border:1px solid #fcfff4;padding:10px" value="Submit" >
</td>
</tr>
</table>
</form>
</body>
<?php
}
?>
PHP使用POST / GET提交方法。 這意味着頁面總是會重新加載...但是,您可以使用AJAX提交重新加載。
$('form').on('submit', function (e) {
e.preventDefault(); //prevent to reload the page
$.ajax({
type: 'POST', //hide url
url: 'formvalidation.php', //your form validation url
data: $('form').serialize(),
success: function () {
alert('The form was submitted successfully'); //display an alert whether the form is submitted okay
}
});
});
將session_start放在腳本的頂部
<?PHP
session_start();
$name = $_POST['username'];
$_SESSION['user'] = $name;
echo $_SESSION['user'];
?>
在文本輸入字段中,將值設置為用戶名
<input type="text" value="<?php echo isset($_SESSION['user']) ? $_SESSION['user'] : '' ?>" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.