提交表单而无需重新加载或离开当前页面(php- mysql) [英] Submit form without reloading or leaving current page (php- mysql)

查看:87
本文介绍了提交表单而无需重新加载或离开当前页面(php- mysql)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先谢谢您.每次用户接到电话时,他们都会以这种形式进行汇总,因为其中只有少数几个用户,所以不必进行用户验证,但是其想法是,每次提交表单时,用户名都会返回到输入字段,因此用户不必一遍又一遍地输入他们的名字.我的问题是,我有将代码插入Db的php代码的方式是,如果删除此行,则重新加载相同的表单URL,但是一旦提交,它将带我到空白页. 我有一个PHP代码,可以在会话中使用用户名并将其写在输入字段中.但由于我重新加载页面,该会话被杀死.

thank you in advance. Every time an user get a call they will summarize it in this form, user validation is not necessary since there are just a few of them but the idea is that every time that the form is submitted the username goes back to the input field, so the user doesn't have to type their names over and over. My problem is that the way i have the php code to insert the values into Db is RELOADING the same form-url BUT if a remove this line, once i submit it takes me to a blank page. I have A PHP code that takes the username in the session and write it in the input field just fine. but since i reloading the page the session gets killed.

问题是提交表单后如何在输入字段中重新设置用户名.

the question is how to set username back in the input field after a submit the form.

这是代码:

<?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 来提交重新加载.

PHP works with POST / GET submit methods. This means always the page will reload... But, you can use AJAX to submit withoud reloading.

$('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
            }
          });

        });

这篇关于提交表单而无需重新加载或离开当前页面(php- mysql)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆