Javascript form data not updating Mysql database - javascript

EDIT
Ive now got the following - but still it does not work
<script>
$(document).ready(function() {
$("button").click(function(){
var jsPostcode = document.login.getElementsByName("postcode").value;
var jsEmail = document.login.getElementsByName("email").value;
var formdata = {postcode:jsPostcode,email:jsEmail};
$.ajax(
{
type: "POST",
url: "database.php", //Should probably echo true or false depending if it could do it
data : formdata,
success: function(feed) {
if (feed!="true") {
// DO STUFF
} else {
console.log(feed);
// WARNING THAT IT WASN'T DONE
}
}}}};
</script>
=================================================================
ORIGINAL QUESTION FOLLOWS
I'm trying to take in data using a form and send some of this to a Mysql database. I cant use the action keyword in the form as that is being used to complete authentication therefore I am trying to use ajax and jquery. I have got to a point where I know I am close to cracking it but I'm not sure what is wrong. Please see my files below. First off the main file login.php is below:
Form Follows (login.php)
<?php
$mac=$_POST['mac'];
$ip=$_POST['ip'];
$username=$_POST['username'];
$linklogin=$_POST['link-login'];
$linkorig=$_POST['link-orig'];
$error=$_POST['error'];
$chapid=$_POST['chap-id'];
$chapchallenge=$_POST['chap-challenge'];
$linkloginonly=$_POST['link-login-only'];
$linkorigesc=$_POST['link-orig-esc'];
$macesc=$_POST['mac-esc'];
if (isset($_POST['postcode'])) {
$postcode = $_POST['postcode'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
?>
**SOME HTML HERE**
<script src="jquery-3.2.1.min.js"></script>
<script>
var js-postcode = document.login.getElementsByName("postcode").value;
var js-email = document.login.getElementsByName("email").value;
var formdata = {postcode:js-postcode,email:js-email};
$("button").click(function(){
$.ajax(
{
type: "POST",
url: "database.php", //Should probably echo true or false depending if it could do it
data : formdata,
success: function(feed) {
if (feed!="true") {
// DO STUFF
} else {
console.log(feed);
// WARNING THAT IT WASN'T DONE
}
}}}
</script>
</head>
<body>
<table width="100%" style="margin-top: 10%;">
<tr>
<td align="center" valign="middle">
<table width="240" height="240" style="border: 1px solid #cccccc; padding: 0px;" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="bottom" height="175" colspan="2">
<!-- removed $(if chap-id) $(endif) around OnSubmit -->
<form name="login" action="<?php echo $linkloginonly; ?>" method="post" onSubmit="return doLogin()" >
<input type="hidden" name="dst" value="<?php echo $linkorig; ?>" />
<input type="hidden" name="popup" value="true" />
<table width="100" style="background-color: #ffffff">
<tr><td align="right">login</td>
<td><input style="width: 80px" name="username" type="text" value="<?php echo $username; ?>"/></td>
</tr>
<tr><td align="right">password</td>
<td><input style="width: 80px" name="password" type="password"/></td>
</tr>
<tr><td align="right">Postcode</td>
<td><input style="width: 80px" name="postcode" type="text" /></td>
</tr>
<tr><td align="right">Email</td>
<td><input style="width: 80px" name="email" type="text" /></td>
</tr>
<td><button><input type="submit" value="OK" /></button></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
<script type="text/javascript">
<!--
document.login.username.focus();
//-->
</script>
</body>
</html>
and called file database.php is as follows:
<?php
if ((isset($_POST['postcode'])) && (isset($_POST['email']))) {
$postcode = $_POST['postcode'];
$email = $_POST['email'];
$connect= new mysqli_connect('xx','xx','xx','xx');
if ($conn->connect_errno) {
echo "There was a problem connecting to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}
if (!($sql = $conn->prepare("INSERT INTO visitors(postcode,email) VALUES(postcode,email)"))) {
echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
}
//NOTE: the "ss" part means that $postcode and $email are strings (mysql is expecting datatypes of strings). For example, if $postcode is an integer, you would do "is" instead.
if (!$sql->bind_param("ss", $postcode, $email)) {
echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
}
if (!$sql->execute()) {
echo "Execute failed: (" . $sql->errno . ") " . $sql->error;
}
} else {
echo 'Variables did not send through ajax.'; // any echoed values would be sent back to javascript and stored in the 'response' variable of your success or fail functions for testing.
}
?>
Please help - all assistance greatly appreciated

Related

sum of rows is not updated when running AJAX code until F5 refresh

Following this link here about sending data into MySQL using AJAX, I have this output:
What I want, is to see the row with the curent rows shown in the div and not at the bottom. And how to refresh the sum, and not wait to refresh the page ?
Here is the final AJAX code:
function addFunction()
{
var selectW = $('#insert_new').val();
var selectW = $('#selectW').val();
var select_at = $('#select_at').val();
var pay = $('#pay').val();
var facture = $('#facture').val();
var select_opt = $('#select_opt').val();
if(pay!="")
{
$.ajax({
data: {'selectW': selectW, 'select_at': select_at, 'pay': pay, 'facture': facture, 'select_opt': select_opt},
type: "post",
url: "insert_buy.php",
success: function(response){
if(response=="success")
{
$('#incident_table').append('<tr><td height="30" align="center">' + selectW + '</td><td align="center">' + select_at + '</td> <td align="center" dir="ltr">' + pay + '</td> <td align="center">' + facture + '</td> <td align="center"><form action="delete.php" method="post"><input type="hidden" name="rowid" value="" /><input class="imgClass_dell" type="submit" onclick="return confirm(\'هل أنت متأكد؟\')" name="delete_sales" value="" /></form></td></tr>');
alert(data);
$('#selectW').val('');
$('#select_at').val('');
$('#pay').val('');
$('#facture').val('');
$('#select_opt').val('');
}
else
{
alert("No Data added");
}
},
error: function(){
//alert('error; ' + eval(error));
}
});
}
else
{
alert("All Fields Are Required!!");
}
}
And here is where PHP calculate the sum:
</tr>
</form>
<?php
$sum = 0;
$selectAll = "SELECT * FROM sales WHERE date_now = :date ORDER BY date_now DESC, time_now DESC";
$stmtAll=$conn->prepare($selectAll);
$stmtAll->bindValue(':date', date("y-m-d"));
$execAll=$stmtAll->execute();
$result=$stmtAll->fetchAll();
?>
<?php foreach($result as $rows){
$sum = $sum + $rows['pay'];
//var_Dump($rows) ?>
<tr>
<td height="30" align="center"><?php echo $rows['type'] ?></td>
<td align="center"><?php echo $rows['provider'] ?></td>
<td align="center" dir="ltr"><?php echo (number_format($rows['pay'], 0, ',', ' ')). ' L.L'?></td>
<td align="center"><?php echo $rows['facture'] ?></td>
<td align="center"><form action='delete.php' method="post">
<input type="hidden" name="rowid" value="<?php echo $rows['id'] ?>" />
<input class="imgClass_dell" type="submit" onclick="return confirm('هل أنت متأكد؟')" name="delete_sales" value="" />
</form></td>
</tr>
<?php } ?>
<tr>
<th colspan="4" align="center" bgcolor="#666666">المجموع</th>
<td dir="ltr" bgcolor="#666666" align="center"><?php
echo ($sum = number_format($sum, 0, ',', ' ')). ' L.L';
?></td>
</tr>
</table>
</div>
</div>
I hope I can get some help.
you can try with this
You'll need a getTable.php page that displays your table, and nothing else: no headers, footers, etc.
PHP (getTable.php) - this can be any server side code (asp, html, etc..)
<?php
echo '<table><tr><td>TEST</td></tr></table>';
?>
Then, in your JS, you can easily refresh the table by using the load() method:
HTML
<div id="tableHolder"></div>
js
<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#tableHolder').load('getTable.php', function(){
setTimeout(refreshTable, 5000);
});
}
</script>
try and good luck i prefer work with jqgrid

php works from action but not from onSubmit in html form

I have an HTML form which gets some text data from the user. I want to insert that into my database and stay on the same form so that another row can be created in the database. I have the php working fine if I call it from the form using action='includes/addUser.php' but that then leaves the browser on the addUser.php page which doesnt display anything. I had thought I could use ajax to not change the display but the php does not seem to get called. If I change the name of the php file in the ajax call I do see an error.
<div id="addUserFormDivID">
<!-- <form id='addUserFormID' method='post' action='includes/addUser.php'> -->
<form id='addUserFormID' method='post' onSubmit='javascript:addUser()'>
Add User<br /><br />
<table>
<tr>
<td align="right">Full name:</td>
<td align="left"><input type="text" name="fullName" /></td>
</tr>
<tr>
<td align="right">Nickname:</td>
<td align="left"><input type="text" name="nickName" /></td>
</tr>
<tr>
<td align="right">Email:</td>
<td align="left"><input type="text" name="email" /></td>
</tr>
<tr>
<td align="right">Phone:</td>
<td align="left"><input type="text" name="phone" /></td>
</tr>
<tr>
<td align="right">Postal Address:</td>
<td align="left"><input type="text" name="address" /></td>
</tr>
<tr>
<td align="right">Postcode:</td>
<td align="left"><input type="text" name="postcode" /></td>
</tr>
</table>
<input type="submit" name="submitAddUser" value="Add User" style="float:right">
</form>
</div>
<script type="text/javascript">
function addUser()
{
console.log("javascript addUser()");
$.ajax
({
type: "POST",
url: "includes/addUser.php",
//data: '1',
success: function()
{
alert("User added");
}
}); // Ajax Call alert("hello");
}
addUser.php:
<?php
include_once 'db_connect.php';
include_once 'functions.php';
//sec_session_start();
debug_to_console("addUser.php");
$fullName = $_POST[fullName];
$nickName = $_POST[nickName];
$email = $_POST[email];
$phone = $_POST[phone];
$address = $_POST[address];
$postcode = $_POST[postcode];
$stmt = mysqli_prepare($mysqli, "INSERT INTO Person (FullName, NickName, Email, Phone, PostalAddress, Postcode) VALUES (?, ?, ?, ?, ?, ?)");
if ($stmt === false)
{
trigger_error('Statement failed! ' . htmlspecialchars(mysqli_error($mysqli)), E_USER_ERROR);
}
$bind = mysqli_stmt_bind_param($stmt, "ssssss", $fullName, $nickName, $email, $phone, $address, $postcode );
if ($bind === false)
{
trigger_error('Bind param failed!', E_USER_ERROR);
}
$exec = mysqli_stmt_execute($stmt);
if ($exec === false)
{
trigger_error('Statement execute failed! ' . htmlspecialchars(mysqli_stmt_error($stmt)), E_USER_ERROR);
}
//printf ("New Record has id %d.\n", mysqli_insert_id($mysqli));
mysqli_stmt_close($stmt);
echo "done addUser";
?>
with the action='includes/addUser.php' line not commented out (and the onSubmit commented out) then it works as I want up to displaying a blank page
With the onSubmit line active instead, the javascript addUser function is called ("javascript addUser()" is output to the console and the alert is shown with "User added") but nothing in the addUser.php file seems to be run. If I include_once a nonexistent file at the start of addUser.php, I dont see an error.
You need to include the data of your form in the request. Also note that it's generally considered best practice to hook your events up in JavaScript. With that in mind try this:
<form id="addUserFormID" method="post" action="include/addUser.php">
<!-- rest of your inputs -->
</form>
$(function() {
$('#addUserFormID').submit(function(e) {
e.preventDefault(); // stop the normal form submission
$.ajax({
type: this.method,
url: this.action,
data: $(this).serialize(),
success: function() {
alert("User added");
}
});
});
});
After url use:
url: "includes/addUser.php",
data : $("#addUserFormID").serialize(),
type: "POST",
.......

Ajax output html comment

I have a simple ajax call but doesen't work perfectly!
index.php (situated in / )
<?php
require 'files/config.php';
require 'files/functions.php';
include 'files/variabili.php';
?>
<script>
$(document).ready(function() {
$('#cerca-amico').on('keyup', function(e) {
if (e.which === 13) {
e.preventDefault();
$('#cerca_amico').trigger('click');
}
var dati = $("#form-cerca-amico").serialize();
$.ajax({
type: "POST",
url: "inc/friends.php",
data: dati,
dataType: "html",
success: function(msg){ $("#ShowFriends").html(msg); },
error: function(){ alert("Ricerca fallita, riprovare..."); }
});
});
});
</script>
<?php
include "inc/testpage.php";
?>
testpage.php (situated in /inc/)
<table width="100%">
<tr>
<td rowspan="2" width="150" align="center" valign="top">
<?
$q="SELECT * FROM amico WHERE (user1='$_SESSION[valid_user]' OR user2='$_SESSION[valid_user]') AND accetta='s'";
$q_r=mysql_query($q);
$num=mysql_num_rows($q_r);
if($num<1)
echo "Non hai amici.";
else{
?>
<form id="form-cerca-amico" onSubmit="return false;">
<input name="cerca-amico" type="text" placeholder="Cerca amici..." id="cerca-amico" autocomplete="off">
<br><br>
<input type="hidden" id="cerca_amico" value="Cerca">
</form>
<div id="ShowFriends"></div>
<?
}
?>
</td>
<td height="400" align="center" valign="top">
chat
</td>
</tr>
<tr>
<td align="center">
<form method="POST">
<textarea id="ChatField"></textarea><input type="submit" value="Invia" id="ChatInput">
</form>
</td>
</tr>
</table>
and friends.php (situated in /inc/)
<?
require 'files/config.php';
require 'files/functions.php';
include 'files/variabili.php';
$nome=urldecode($_POST['cerca-amico']);
echo $nome;
?>
When I write " hello " on the input " look - friend " I should receive as output in the div # ShowFriends . But I never get results , I noticed that viewing the source code I get all the content of the page friends.php between comments .
Specifically :
<div id="ShowFriends">
<!--?
require 'files/config.php';
require 'files/functions.php';
include 'files/variabili.php';
$nome=urldecode($_POST['cerca-amico']);
echo $nome;
?-->
</div>
Any solutions? I have no idea...
<div id="ShowFriends">
<?php
require 'files/config.php';
require 'files/functions.php';
include 'files/variabili.php';
$nome=urldecode($_POST['cerca-amico']);
echo $nome;
?>
</div>

PHP - Submit form using Get method.

I'm using Get method to add data that has been entered by user to the event table in database
as you can see I'm using the get method to see if the user has posted and then run the code
but the if statement does not run and I can't find out where is the problem.
Any help would be useful
Thank you very much
if(isset($_GET['add']))
{
$title=$_POST['txttitle'];
$detail=$_POST['txtdetail'];
$eventdate=$month."/".$day."/".$year;
$sqlinsert="INSERT INTO book (title, detail, eventdat, dateadded) VALUES ('{$title}', '{$detail}', '{$eventdate}',now())";
$result = mysql_query($sqlinsert, $connect);
if($result)
{
echo"date has been added";
}
else
{
echo "ops there was problem ";
}
}
this is the full Code
event.php
<?php
include_once('db_connection.php');
$sqlinsert="";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function goLastMonth(month, year)
{
if(month==1)
{
--year
month= 13;
}
--month
var monthstring=""+month+"";
var monthlength= monthstring.length;
if(monthlength<= 1)
{
monthstring="0"+monthstring;
}
document.location.href="<?php $_SERVER['PHP_SELF'];?> ?month="+monthstring+"&year="+year;
}
function goNextMonth(month , year)
{
if(month==12)
{
++year
month= 0;
}
++month
var monthstring=""+month+"";
var monthlength= monthstring.length;
if(monthlength<= 1)
{
monthstring="0"+monthstring;
}
document.location.href="<?php $_SERVER['PHP_SELF'];?> ?month="+monthstring+"&year="+year;
}
</script>
</head>
<body>
<?php
if(isset($_GET['day']))
{
$day=$_GET['day'];
}
else
{
$day=date('j');
}
if(isset($_GET['month']))
{
$month=$_GET['month'];
}
else
{
$month=date("n");
}
if(isset($_GET['year']))
{
$year=$_GET['year'];
}
else
{
$year=date("Y");
}
$t=date('H:i');
//$day=date('j');
//$month=date("n");
//$year=date("Y");
$time=$day.",".$month.",".$year;
$currenttimestamp= strtotime("$year-$month-$day");
$monthname=date("F",$currenttimestamp );
$numdays= date("t",$currenttimestamp);
if(isset($_GET['add']))
{
$title=$_POST['txttitle'];
$detail=$_POST['txtdetail'];
$eventdate=$month."/".$day."/".$year;
$sqlinsert="INSERT INTO book (title, detail, eventdat, dateadded) VALUES ('{$title}', '{$detail}', '{$eventdate}',now())";
$result = mysql_query($sqlinsert, $connect);
if($result)
{
echo"date has been added";
}
else
{
echo "ops there was problem ";
}
}
?>
<?php echo $sqlinsert;?>
<table border="2">
<tr>
<td colspan="5">Month And Year <?php echo $monthname;?></td>
<td ><input style="width:55px" type="button" value="<<" name="previousbutton" onclick="goLastMonth(<?php echo $month. ",".$year;?>)"> </td>
<td> <input style="width:55px" type="button" value=">>" name="nextbutton" onclick="goNextMonth(<?php echo $month. ",".$year;?>)"> </td>
</tr>
<tr>
<td width="50">Sun</td>
<td width="50">Mon</td>
<td width="50">Tue</td>
<td width="50">Wed</td>
<td width="50">Thu</td>
<td width="50">Fri</td>
<td width="50">Sat</td>
</tr>
<?php
$counter="";
echo "<tr >";
for ($i=1;$i< $numdays+1; $i++, $counter++)
{
$timeStamp=strtotime("$year-$month-$i");
if($i==1)
{
$firstDay=date("w", $timeStamp);
for($j=0;$j<$firstDay; $j++, $counter++)
//blank space
{
echo "<td> </td>";
}
}
if($counter %7==0)
{
echo "</tr><tr>";
}
$monthstring=$month;
$monthlength= strlen ($monthstring);
//
$daystring=$i;
$daylength= strlen($daystring);
if($monthlength<=1)
{
$monthstring="0".$monthstring;
}
if($daylength<=1)
{
$daystring="0".$daystring;
}
echo "<td align='center'><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a></td>";
}
echo"</tr>";
?>
</table>
<?php
if(isset($_GET['v']))
{
echo"<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true&f=true'>ADD EVENT</a>";
if(isset($_GET['f']))
{
include("eventform.php");
}
}
?>
</body>
</html>
and this is Event Form
<form name="eventform" name="POST" action="<?php $_SERVER['PHP_SELF'];?>?month=<?php echo $month;?>&day=<?php echo $day;?>&year=<?php echo $year;?>&v=true&&add=true">
<table width="400px">
<tr>
<td width="150">Title</td>
<td width="250"><input type="text" name="txttitle"></td>
</tr>
<tr>
<td width="150">Detail</td>
<td width="250"><textarea name="txtdetail"></textarea></td>
</tr>
<tr>
<td colspan='2' align="center"><input type="submit" name"btnadd" value="Add Event"></td>
</tr>
</table>
</form>
PHP:
if(isset($_GET['btnadd']))
{
$title=$_GET['txttitle'];
$detail=$_GET['txtdetail'];
$eventdate=$month."/".$day."/".$year;
$sqlinsert="INSERT INTO book (title, detail, eventdat, dateadded) VALUES ('{$title}', '{$detail}', '{$eventdate}',now())";
$result = mysql_query($sqlinsert, $connect);
if($result)
{
echo"date has been added";
}
else
{
echo "ops there was problem ";
}
}
And HTML:
<form name="eventform" method="GET" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="400px">
<tr>
<td width="150">Title</td>
<td width="250"><input type="text" name="txttitle"></td>
</tr>
<tr>
<td width="150">Detail</td>
<td width="250"><textarea name="txtdetail"></textarea></td>
</tr>
<tr>
<td colspan='2' align="center"><input type="submit" name"btnadd" value="Add Event"></td>
</tr>
</table>
</form>
method="GET" automatically adds the vars to the URL, you do not need to add them in the action.
There are various errors in your code, but the main is that you are setting <form name="POST">. I believe that you want to set <form method="POST">.
first of all, if you go for method GET.. any user by just refreshing the page after sending, it would bucle your action, cause PHP will check for the url form if it has a GET method form.
also, your PHP code seems to be a bit wrong..
if(isset($_GET['add']))
{
$title=$_POST['txttitle'];
$detail=$_POST['txtdetail'];
$eventdate=$month."/".$day."/".$year;
$sqlinsert="INSERT INTO book (title, detail, eventdat, dateadded) VALUES ('{$title}', '{$detail}', '{$eventdate}',now())";
$result = mysql_query($sqlinsert, $connect);
if($result)
{
echo"date has been added";
}
else
{
echo "ops there was problem ";
}
}
your PHP is checking if theres a var, (array="add"), so its checking if there's an input with the name "add" in your form, i think you are trying check for "btnadd" because your submit button has that name.
and you have an error in your input:
<input type="submit" name"btnadd" value="Add Event">
you didnt enter "=", so should be:
<input type="submit" name="btnadd" value="Add Event">
where "btnadd" is has to be in your PHP like this
if(isset($_GET['btnadd']));//Exact var name

disabled textbox causes errors in PHP page when trying to update table in mysql

I'm caught in a set of twisting codes. I've a table in a form which consists of all the information from one table say emp_info. I am not including all the mysql query and db connection but just suppose this table has fetched information from emp_info table from my database. Example:
<table align="center">
<form name="f1" action="update.php" method="post">
<tr>
<td>Enter name : </td><td><input type="text" name="ename" id="field1"
disabled="disabled" /></td><td><input id="myCheckBox1" type="checkbox"
onclick="enableText1(this.checked, 'field1');" />
</td>
</tr>
<tr>
<td>Enter Address : </td><td><input type="text" name="address" id="field2"
disabled="disabled" /></td><td><input id="myCheckBox2" type="checkbox"
onclick="enableText2(this.checked, 'field2');" /></td>
</tr>
<tr>
<td><input type=""submit value="submit"/></td>
</tr>
</form>
</table>
and following is the respective JAVASCRIPT written in the HEAD section:
<script type="text/javascript">
function enableText1(checkBool, textID)
{
text1 = document.getElementById(textID);
//Disable the text field
text1.disabled = !checkBool;
//Clear value in the text field
if (!checkBool) { text1.value = ''; }
}
function enableText2(checkBool, textID)
{
text2 = document.getElementById(textID);
//Disable the text field
text2.disabled = !checkBool;
//Clear value in the text field
if (!checkBool) { text2.value = ''; }
}
</script>
I want user to check the checkbox to enable the textbox to edit it. What can a user do with it? He can check the checkbox to edit it and when he unchecks it once again textbox will be disabled. That means, what he edited in the checkbox is going to be locked. To prevent him from doing it; I'm setting the textbox to empty; once he unchecks the checkbox. It's working up to it. My real problem starts from here. when the textbox is disabled and he submits the records to update, it throws an error while accepting the value using POST method ($name=$_POST['name']) in update.php:
Notice: Undefined variable: address in C:\xampp\htdocs\xampp\Test\HRMS\try\chcking with checkboxes\update.php on line 4
My main intention is to update the record into the temporary table. When a record is submitted using disabled textbox or empty textbox; in that case I would like to fetch its value from the old table and store it into the temporary table. I do not know how clear I am but accepting some support from enthusiastic master's in coding here. Thanks in advance.
please check my actual code. I am including both the pages below :
<?php
session_start();
if(!$_SESSION['logged'])
{
header("Location: login.php");
exit;
}
echo 'Welcome, '.$_SESSION['user'];
//echo 'Welcome, '.$_SESSION['email'];
//echo 'Welcome, '.$_SESSION['eid'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function enableText1(checkBool, textID)
{
text1 = document.getElementById(textID);
//Disable the text field
text1.disabled = !checkBool;
//Clear value in the text field
if (!checkBool) { text1.value = ''; }
}
</script>
</head>
<body>
</body>
</html>
<?php
$user=$_SESSION['user'];
$email=$_SESSION['email'];
$eid=$_SESSION['eid'];
echo "<br>";
//echo $email;
//echo $eid;
//echo $user;
$con = mysql_connect("localhost","mars","mars");
if (! $con)
die(mysql_error());
mysql_select_db("marsweb",$con);
mysql_query(" UPDATE login_info SET user_staus=1 WHERE username='$email'");
$query="SELECT * FROM emp_info WHERE eid='$eid'";
//echo "$query";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {
$userid=mysql_result($result,$i,"eid");
$name=mysql_result($result,$i,"ename");
$password=mysql_result($result,$i,"password");
$address=mysql_result($result,$i,"address");
$source=mysql_result($result,$i,"source");
$salary=mysql_result($result,$i,"salary");
$zip=mysql_result($result,$i,"zip");
$mobile=mysql_result($result,$i,"mobile");
$email=mysql_result($result,$i,"email");
?>
<div class="go_right">
<table width="100" cellpadding="10" cellspacing="0" border="2" align="center">
<form action="change_record1.php" method="post">
<tr>
<td>Employee ID</td><td><input type="text" name="userid" value="<?php echo "$userid" ?>" readonly></td>
</tr>
<tr>
<td>Name:</td><td><input type="text" name="name" value="<?php echo "$name"?>" readonly></td>
</tr>
<!--<tr>
<td>Password: </td><td><input type="text" name="password" value="<?php echo "$password"?>" ></td>
</tr>-->
<tr>
<td>Address:</td><td> <input type="text" name="address" value="<?php echo "$address"?>" id="field1" dissabled="disabled" /></td>
<td><input id="myCheckBox1" type="checkbox" onClick="enableText1(this.checked, 'field1');" /></td>></td>
</tr>
<tr>
<td>Source: </td><td><input type="text" name="source" value="<?php echo "$source"?>"></td>
</tr>
<tr>
<td>Salary: </td><td><input type="text" name="salary" value="<?php echo "$salary"?>" readonly></td>
</tr>
<tr>
<td>Mobile:</td><td> <input type="text" name="mobile" value="<?php echo "$mobile"?>"></td>
</tr>
<tr>
<td>Zip: </td><td><input type="text" name="zip" value="<?php echo "$zip"?>"></td>
</tr>
<tr>
<td>Email: </td><td><input type="text" name="email" value="<?php echo "$email"?>" readonly></td>
</tr>
<tr style="border:#FFF";>
<td><input type="Submit" value="Update"></td>
</tr>
</form>
</table>
<?php
++$i;
}
?>
and the php page which will update the temporary table is given below...
<?php
session_start();
if(!$_SESSION['logged'])
{
header("Location: login.php");
exit;
}
echo 'Welcome, '.$_SESSION['user'];
echo "<br>";
echo "<br>";
?>
<?php
$userid=$_POST['userid'];
//$name=$_POST['name'];
//$password=$_POST['password'];
if(isset($_POST['address']))
$address=$_POST['address'];
$source=$_POST['source'];
$salary=$_POST['salary'];
$mobile=$_POST['mobile'];
$zip=$_POST['zip'];
$email=$_POST['email'];
$con = mysql_connect("localhost","mars","mars");
if (! $con)
die(mysql_error());
mysql_select_db("marsweb",$con);
//echo $userid; echo "<br>";
echo $address; echo "<br>";
if($address=="")
{
$query1="select address from `emp_info` where email='$email'";
$fetched1=mysql_query($query1);
while($record1=mysql_fetch_assoc($fetched1))
{
while(each($fetched1))
{
$address=$record1["address"];
}
}
}
echo $address;
mysql_query("UPDATE temp_emp_info SET eid='$userid' , address='$address' , source='$source' , mobile='$mobile' , zip='$zip' WHERE eid='$userid'");
echo "<br>";echo "<br>";echo "<br>";
echo "<center><h2>Record updated</h2></center><br><br>";
?>
When a field is marked "disabled" it doesn't get submitted to the server, so the value is lost. You either need to adapt your server side code to be aware of the possibility that the textbox is unset, or use readonly="readonly" instead of disabled="disabled" to make the text field uneditable. Read only controls are still submitted to the server, they just can't be edited by the user.

Categories