提交表单而不重新加载或离开当前页面(php- mysql)

时间:2014-05-29 16:52:41

标签: php html mysql

提前谢谢你。每次用户接到电话时,他们都会以这种形式对其进行汇总,因为只有少数人可以进行用户验证,但想法是每次提交表单时用户名都会返回到输入字段,所以用户不必一遍又一遍地输入他们的名字。我的问题是,我有PHP代码将值插入Db的方式是RELOADING相同的form-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
}
?>

2 个答案:

答案 0 :(得分:1)

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
            }
          });

        });

答案 1 :(得分:0)

将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'] : ''  ?>" />