I've got an error says failed, whenever I check this code I feel there is nothing wrong with my code. Please help me, as I'm only a beginner. There are forms, javascript, and php.
<?php
$species=""; //declare variable to assign table record data
$age= "";
$sex="";
$location="";
$comment="";
require('connectdb.php');
$record=$_REQUEST['id' ];
$query="SELECT * FROM usersform WHERE id = $record "; // listing reservation record
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$species= $row['species']; //retrieve table record and assign to variable
$age= $row['age'];
$location= $row['location'];
$comment= $row['comment'];
if (isset($_POST['submit']))
{
//get data from reservation form
$spec=$_POST['species'];
$eage=$_POST['age'];
$gender=$_POST['gender'];
$elocation=$_POST['location'];
$ecomment=$_POST['comment'];
//update data to table
$query=" UPDATE `usersform` SET
`species` ='$spec',
`age`='$eage',
`location`='$elocation',
`comment`='$ecomment',
WHERE usersform= $record
";
$qresult = mysql_query($query);
if ($qresult){
/*echo '<script type="text/javascript">
window.location = "http://www.example.com/";
</script>';*/
echo '<script type="text/javascript"> window.location="gallery.php";</script>';
#header("location:gallery.php"); // redirect to list
}
else
{
echo "<script type='text/javascript'>alert('failed!')</script>";
}
}
?>
Here is my form code:
<h2>Edit/Update Reservation Record </h2>
<form name="Form" method="Post" onsubmit="return(validateForm());" onreset="cancel();">
<table cellspacing="2" cellpadding="2">
<tr>
<td>species: </td>
<td><input type="text" name="species" size="35" value="<?php echo $species; ?>"/></td>
</tr>
<tr>
<td>age:</td>
<td><input type="text" name="age" maxlength="15" size="15" value="<?php echo $age; ?>"/></td>
</tr>
<tr>
<td>sex:</td>
<td><input type="radio" name="gender" value="male" value="<?php echo $sex; ?>"/>Male <input type="radio" name="gender" value="female" value="<?php echo $sex; ?>"/>Female</td>
</tr>
<tr>
<td>location:</td>
<td><input type="text" name="location" size="35" value="<?php echo $location; ?>"/></td>
</tr>
<tr>
<td>comment:</td>
<td><input type="text" name="comment" size="50" value="<?php echo $comment; ?>"/></td>
</tr>
<tr>
<td><input type="submit" value="Update" name="submit" /></td>
<td><input type="reset" value="Cancel" name="reset" /></td>
</tr>
</table>
</form>
Here is my javascript:
function validateForm() {
if( document.Form.species.value == "" )
{
alert( "Please insert species." );
document.Form.Name.focus() ;
return false;
}
if( document.Form.age.value == "" ||isNaN( document.Form.age.value ) )
{
alert( "Please insert age." );
document.Form.age.focus() ;
return false;
}
if( document.Form.location.value == "" )
{
alert( "Please insert location." );
document.Form.Name.focus() ;
return false;
}
if( document.Form.comment.value == "" )
{
alert( "Please insert comment." );
document.Form.Name.focus() ;
return false;
}
}
function cancel()
{
window.location.href="gallery.php";
}
the update syntax is incorrect.. this is the correct sql syntax
$query=" UPDATE `usersform` SET
`species` ='$spec',
`age`='$eage',
`location`='$elocation',
`comment`='$ecomment'
WHERE usersform= $record
";
change your update query to this
$query=" UPDATE `usersform` SET
`species` ='$spec',
`age`='$eage',
`location`='$elocation',
`comment`='$ecomment'
WHERE id= $record
";
$qresult = mysql_query($query) or die(mysql_error());
Note: Learn mysqli_ or P.D.O as mysql is depriciated
Related
We have a .aspx page with two HTML SELECT element and we want to load countries in one dropdownlist and load cities of a country in the other.
we are using entity framework to access data.
I tried PageMethods and &Ajax but I could not access my city dropdown from within a static web method. this is what we have so far:
<!-- Signup.aspx-->
<select id="cmb_Region" runat="server" onchange="GetCitiesOfRegion();" datatextfield="region_title" datavaluefield="region_id"></select>
<select runat="server" id="cmb_City" datatextfield="city_title" datavaluefield="city_id" class="ui dropdown"></select>
<script type="text/javascript">
function GetCitiesOfRegion(regionId)
{
$.ajax({
url:"Signup.aspx/GetCities",
type:"POST",
data:'{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert("success");
}
});
}
</script>
and this is code-behind code:
//Signup.aspx.cs
[WebMethod]
public static List<City> GetCities(int RegionId)
{
behbimeh_informationEntities db = new behbimeh_informationEntities();
return db.City.Where(i => i.region_id == RegionId).ToList();
}
Actually the code is not correct and I dont know how to pass the cmb_region selected value to the webmethod? and I dont know how to bind the cmb_city to the returned List of cities.
please share your knowledge. thanks...
To get cmb_Region value
var regionId = $('#<%= cmb_Region.ClientID %>').val();
To get cities by region and bind at client side
[WebMethod]
public static string GetCities(int RegionId)
{
behbimeh_informationEntities db = new behbimeh_informationEntities();
var cities= db.City.Where(i => i.region_id == RegionId).Select(item=>new { item.ValueField, item.TextField }).ToList();
return Newtonsoft.Json.JsonConvert.SerializeObject(cities);
}
<script type="text/javascript">
function GetCitiesOfRegion(regionId)
{
$.ajax({
url:"Signup.aspx/GetCities",
type:"POST",
data:'{"RegionId":'+regionId+'}',
success: function (r) {
var cities=JSON.parse(r.d);
var citiesOptions="";
for(var i=0;i<cities.length;i++){
citiesOptions+='<option value="'+cities[i].Value+'">'+cities[i].Text+'</option>';
}
$('#<%= cmb_City.ClientID %>').html(citiesOptions);
}
});
}
first you will need to clear the existing data, like so:
$('#cmb_city').empty();
next, you need to load the returned list to your ddl.
$.ajax({
url:"Signup.aspx/GetCities",
type:"POST",
data:'{"RegionId":'+$('#cmb_Region').val()+'}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
$.each(r, function() {
$('#cmb_city').append($('<option>').text(this.Name).attr('value', this.Id));
});
}
});
where Name and Id are your text and value fields respectively.
you will also probably need to send the selected region.
Here is your answer like how to bind data from database using Ajax in php
<!-- edit.php -->
<?php
include("config.php");
$id=$_REQUEST['id'];
echo $id;
$query=mysqli_query($con,"SELECT * FROM register r
INNER JOIN country c ON r.cuid = c.cuid
INNER JOIN state s ON r.sid = s.sid
INNER JOIN city ct ON r.cid = ct.cid where id='$id'");
while($r=mysqli_fetch_array($query))
{
$fn=$r['firstname'];
$add=$r['address'];
$gn=$r['gender'];
$hobby=$r['hobby'];
$h=explode(',',$hobby);
$q=array('reading','traveling','cricket','drawing');
$country=$r['cuid'];
$state=$r['sid'];
$city=$r['cid'];
echo $gn;
$edu= $r['education'];
$email=$r['email'];
$pass=$r['password'];
$conpass=$r['conpassword'];
$phno=$r['phoneno'];
}
?>
<html>
<head>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#country').on('change',function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'cuid='+countryID,
success:function(html){
$('#state').html(html);
$('#city').html(html);
}
});
}else{
$('#state').html(html);
$('#city').html(html);
}
});
$('#state').on('change',function(){
var stateID = $(this).val();
if(stateID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'sid='+stateID,
success:function(html){
$('#city').html(html);
}
});
}else{
$('#city').html(html);
}
});
});
</script>
</head>
<body>
<form method="post" action="update.php">
<table border="1" align="center">
<caption>Edit user data</caption>
<tr>
<td>First name</td>
<td><input type="text" name="fn" value="<?php echo $fn;?>"></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="add" value="<?php echo $add;?>"></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="gn" value="male" <?php echo ($gn=='male')?'checked':'' ; ?> size="17">Male
<input type="radio" name="gn" value="female" <?php echo ($gn=='female')?'checked':'' ; ?> size="17">Female
</td>
</tr>
<tr>
<td>Hobby</td>
<td><input type="checkbox" name="hobby[]" value="reading" <?php if(in_array('reading',$h)){echo ("checked:'checked'");}?> >reading
<input type="checkbox" name="hobby[]" value="traveling" <?php if(in_array('traveling',$h)){echo ("checked:'checked'");}?> >traveling
<input type="checkbox" name="hobby[]" value="cricket" <?php if(in_array('cricket',$h)){echo ("checked:'checked'");}?> >cricket
<input type="checkbox" name="hobby[]" value="drawing" <?php if(in_array('drawing',$h)){echo ("checked:'checked'");}?> >drawing</td>
</tr>
<?php
$query = mysqli_query($con,"SELECT * FROM country");
//Count total number of rows
$rowCount = mysqli_num_rows($query);
?>
<td>Country</td>
<td><select name="country" id="country">
<option value="<?php echo $country;?>"><?php echo $country;?></option>
<?php
if($rowCount > 0)
{
while($row = mysqli_fetch_array($query))
{
echo '<option value="'.$row['cuid'].'">'.$row['country'].'</option>';
}
}
else
{
echo '<option value="">Country not available</option>';
}
?>
</select>
</td></tr>
<tr>
<td>State</td>
<td>
<select name="state" id="state">
<option value="<?php echo $state;?>"><?php echo $state;?></option>
</select>
</td></tr>
<tr>
<td>City</td>
<td>
<select name="city" id="city">
<option value="<?php echo $city;?>"><?php echo $city;?></option>
</select>
</td>
</tr>
<tr>
<td>Education</td>
<td><input type="text" name="edu" value="<?php echo $edu;?>"></td>
</tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $email;?>"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="pass" value="<?php echo $pass;?>"></td>
</tr>
<tr>
<td>Confirm password</td>
<td><input type="text" name="conpass" value="<?php echo $conpass;?>"></td>
</tr>
<tr>
<td>Phone no</td>
<td><input type="text" name="phno" value="<?php echo $phno;?>"></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="<?php echo $id;?>">
<input type="submit" name="update" value="update"></td>
</tr>
</table>
</form>
</body>
</html>
--------------------
<!--ajaxData.php-->
<?php
//Include database configuration file
include("config.php");
if(isset($_POST["cuid"]) && !empty($_POST["cuid"]))
{
//Get all state data
$query = mysqli_query($con,"SELECT * FROM state WHERE cuid = ".$_POST['cuid']."");
//Count total number of rows
$rowCount = mysqli_num_rows($query);
//Display states list
if($rowCount > 0)
{
echo '<option value="">Select state</option>';
while($row = mysqli_fetch_array($query))
{
echo '<option value="'.$row['sid'].'">'.$row['state'].'</option>';
}
}
else
{
echo '<option value="">State not available</option>';
}
}
if(isset($_POST["sid"]) && !empty($_POST["sid"]))
{
//Get all city data
$query = mysqli_query($con,"SELECT * FROM city WHERE sid =".$_POST['sid']."");
//Count total number of rows
$rowCount = mysqli_num_rows($query);
//Display cities list
if($rowCount > 0)
{
echo '<option value="">Select city</option>';
while($row = mysqli_fetch_array($query))
{
echo '<option value="'.$row['cid'].'">'.$row['city'].'</option>';
}
}
else
{
echo '<option value="">City not available</option>';
}
}
?>
I want to send a mail after registration but it is not working, I don't know what's wrong could plze help me out
javascript
// WRITE THE VALIDATION SCRIPT IN THE HEAD TAG.
function isNumber(evt) {
var iKeyCode = (evt.which) ? evt.which : evt.keyCode
if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57))
return false;
return true;
}
html + php
<html>
<body>
<form name="reg" action="code_exec.php" method="post">
<table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td colspan="2">
<div align="center">
<?php
$remarks=$_GET['remarks'];
if ($remarks==null and $remarks=="") {
echo 'Register Here';
}
if ($remarks=='success') {
echo 'Registration Success';
}
?>
</div>
</td>
</tr>
<tr>
<td width="95"><div align="right">Mobile_no:</div></td>
<td width="171"><input type="text" ID="tbNumbers" name="mobile_no" required maxlength="10" onkeypress="javascript:return isNumber(event)" /></td>
</tr>
<tr>
<td><div align="right">Name:</div></td>
<td><input type="text" name="name" required/></td>
</tr>
<tr>
<td><div align="right">E_mail:</div></td>
<td><input type="text" name="e_mail" E_MAIL required pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" /></td>
</tr>
<tr>
<td><div align="right">Password:</div></td>
<td><input type="password" name="password" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters"/></td>
</tr>
<tr>
<td><div align="right"></div></td>
<td><input name="submit" type="submit" value="Submit" /></td>
</tr>
</table>
</form>
<!-- Javascript -->
<script src="assets/js/jquery-1.8.2.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/js/jquery.backstretch.min.js"></script>
<script src="assets/js/scripts.js"></script>
</body>
</html>
I Want to send a mail after registration the customer id which auto generated
<?php
session_start();
$con = mysqli_connect("retail", "RS", "orbisindia", "rs");
$mobile_no = $_POST['mobile_no'];
$name = $_POST['name'];
$e_mail = $_POST['e_mail'];
$password = $_POST['password'];
$Cust_Id = time();
$message = "";
try {
// execute the stored procedure
$sql = mysqli_query($con,"CALL sp_retail_reg('$Cust_Id','$mobile_no','$name','$e_mail','$password')");
if(!empty($Cust_Id)) {
$toEmail = $_POST['e_mail'];
$subject = "Thank you for registration";
$content = "your Custotmer id is" . $Cust_Id ;
$content1 = "your Password is" . $password ;
$mailHeaders = "From: rohit.jha#99rstreet.com";
if(mail($toEmail, $subject, $content,$content1, $mailHeaders)) {
$message = "You have registered and your cutomer id is sent to your email. plze check spam Also.";
}
unset($_POST);
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
$message.="Your Cust_Id=$Cust_Id";
echo "alert('$message'); window.location.href='home.php';";
remove the window.location.href from code_exec.php where customerid is alerted
echo "<script>alert('$message'); window.location.href='home.php';</script>"
which is redirecting to the home.php before calling the student_confirmation function.
You can write it after calling the student_confirmation function.
I have the following javascript being included in some PHP which I am writing. The problem is that it is not working and developer tools keeps saying "'validate_entry' is undefined" but I don't know what I'm doing
My code is:
<script type="text/javascript">
function validate_entry()
{
var name=document.getElementById("submitted").value;
var system=document.getElementById("system").value;
var details=document.getElementById("description").value;
if(name.trim() == "")
{
alert("The name you entered is too short");
return false;
}
if(name.length > 64)
{
alert("The name you entered is too long");
return false;
}
if(system.trim() == ""
{
alert("System is not valid");
return false;
}
if(details == "")
{
alert("Description cannot be empty");
return false;
}
}
</script>
and I have the following form:
<form action="index.php" method="POST" onsubmit="return validate_entry();">
<table>
<tr>
<td><label for="submitted">Issue Submitted By</label></td>
<td><input type="text" name="submitted" id="submitted" tabindex="<?php echo $header[0]++; ?>"></td>
</tr>
<tr>
<?php $systems = get_system_names($connection);
if($systems==FALSE)
{
echo "<td></td>\n";
echo "<td></td>\n";
}
else
{
echo "<td><label for=\"system\">System Name</label></td>\n";
echo "<td><select name=\"system\" id=\"system\" tabinex=\"". $header[0]++. "\">\n";
echo "<option>Please Select</option>\n";
foreach($systems as $system)
{
echo "<option value=\"{$system[0]}\">{$system[1]}</option>\n";
}
echo "</select></td>\n";
}
?>
</tr>
<tr>
<td><label for="description">Issue Description</label></td>
<td><textarea name="description" id="description" tabindex="<?php echo $header[0]++; ?>"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" id="submit" tabindex="<?php echo $header[0]++; ?>"></td>
</tr>
</table>
<?php echo "</form>\n";
Make sure the positions of the JavaScript is after the html code
Try to test the JavaScript function using browser console
If it did work try to submit and handle other errors :)
Am trying to update data in two tables and according to my codes below, only the first table 'student_information' is getting updated
leaving out the 'training_information' table not updated. The two tables are linked by IndexNo. I need guidance with my code on how I can update both the tables at once using my sample prepared statements.
Please follow also that I try to get the id value using the URL as given. This helps me view and update data about one distinct column with IndexNo in one operation.
URL: http://localhost/sample/updateStudentInfo.php?id=14
updateStudentInfo.php code as below
<?php
// Connect to MySQL database
function connect(){
try{
$dbConn = new PDO('mysql:host=localhost; dbname=hpcz', 'root', 'root');
$dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConn;
}
catch(PDOException $e){
echo $e->getMessage();
}
}
$dbh = connect();
//gets the value of the id from the URL
$id = htmlspecialchars($_GET["id"]);
$sql = "SELECT
student_information.IndexNo,
student_information.SurName,
student_information.FirstName,
student_information.MiddleName,
grade_obtained.Grade,
secondary_school.SchoolName
FROM student_information
LEFT JOIN training_information
ON student_information.IndexNo = training_information.IndexNo
WHERE student_information.IndexNo='$id'";
$result = $dbh->prepare($sql);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
//Initializing the id from URL with the IndexNo from the database
$id=$row["IndexNo"];
?>
<div><form method="post" >
<table width="500" border="1" >
<tr>
<td colspan="4">Update Student Information</td>
</tr>
<tr>
<td colspan="4"><div align="center"><strong>Applicant Information</strong></div></td>
</tr>
<tr>
<td><div align="right">Sur Name:</div></td>
<td><input type="text" name="surname" id="surname" value="<?php echo $row['SurName']; ?>"/></td>
<td><div align="right">First Name:</div></td>
<td><input type="text" text-input" name="fname" id="fname" value="<?php echo $row['FirstName']; ?>"/></td>
</tr>
<tr>
<td><div align="right">Middle Name:</div></td>
<td><input type="text" name="mname" id="mname" value="<?php echo $row['MiddleName']; ?>"/></td>
<td><div align="right">NRC/Passport No:</div></td>
<td><input type="text" name="nrc" id="nrc" value="<?php echo $row['NRCPassportNo']; ?>"/></td>
</tr>
<tr>
<td colspan="4"><div align="center"><strong>Training Information</strong></div></td>
</tr>
<tr>
<td><div align="right">Secondary School Attended:</div></td>
<td><select name="secondarysch" id="secondarysch" >
<option selected="selected"><?php echo $row['SchoolName']; ?></option>
</select></td>
<td><div align="right">Grade Obtained:</div></td>
<td><select name="grade" id="grade" >
<option selected="selected"><?php echo $row['Grade']; ?></option>
</select></td>
</tr>
<input type="hidden" name="id" id="id" value="<?php echo $row['IndexNo']; ?>"/>
<tr>
<td colspan="4"><div align="center"><button id="update">Update</div></td>
</tr>
</table>
<?php }?>
</form>
JavaScript code:
$(document).ready(function(){
$("#update").click(function(){
/*Applicant Information*/
var vid = $("#id").val();
var vsurname = $("#surname").val();
var vmname = $("#mname").val();
var vfname = $("#fname").val();
/*Training Information*/
var vsecondarysch = $("#secondarysch").val();
var vgrade = $("#grade").val();
/*Jquery post method */
$.post("http://localhost/sample/sendUpdateStudentInfo.php",
{
id : vid,
surname : vsurname,
mname : vmname,
fname : vfname,
secondarysch :vsecondarysch,
grade : vgrade
},
/*Handles response from server*/
function(response){
alert(response);
});
alert("You are here");
});
});
sendUpdateStudentInfo.php code:
<?php
$method = $_SERVER['REQUEST_METHOD'];
function connect(){
try{
$dbConn = new PDO('mysql:host=localhost; dbname=hpcz', 'root', 'root');
$dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConn;
}
catch(PDOException $e){
echo $e->getMessage();
}
}
/*Checks if method is HTTP POST*/
if(strtolower($method) == 'post'){
/*Applicant Information*/
$id = addslashes($_POST['id']);
$surname = addslashes($_POST['surname']);
$mname = addslashes($_POST['mname']);
$fname = addslashes($_POST['fname']);
/*Training Information*/
$secondarysch = addslashes($_POST['secondarysch']);
$grade = addslashes($_POST['grade']);
try {
$dbHandler = connect();
$dbHandler->beginTransaction();
$setInfo = "UPDATE student_information
SET
SurName = :vSurname,
MiddleName = :vMName,
FirstName = :vFName
WHERE
IndexNo = '$id'";
$stmt = $dbHandler->prepare($setInfo);
$stmt->bindParam(':vSurname', $surname);
$stmt->bindParam(':vMName', $mname);
$stmt->bindParam(':vFName', $fname);
$stmt->execute();
$stmt->closeCursor();
$setTrainingInfo = "UPDATE training_information
SET
SecondarySchoolID = :Secondarysch,
GradeObtainedID = :Grade
WHERE
IndexNo = '$id'";
$stmt = $dbHandler->prepare($setTrainingInfo);
$stmt->bindParam(':Secondarysch', $secondarysch);
$stmt->bindParam(':Grade', $grade);
$stmt->execute();
$stmt->closeCursor();
$dbHandler->commit();
echo "The Operation was Successful!!!!!!";
} catch (PDOException $e) {
$dbHandler->rollback();
die($e->getMessage());
}
}else{
echo "Oops! Make sure Method is POST";
}
?>
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.