Reputation: 309
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.
Here is the code:
<?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
}
?>
Upvotes: 0
Views: 7010
Reputation:
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
}
});
});
Upvotes: 1
Reputation: 2812
Place session_start at the top of your script
<?PHP
session_start();
$name = $_POST['username'];
$_SESSION['user'] = $name;
echo $_SESSION['user'];
?>
in your text input field, set the value to the username
<input type="text" value="<?php echo isset($_SESSION['user']) ? $_SESSION['user'] : '' ?>" />
Upvotes: 0