values not fetched in textboxes from database in php - php

I want all details of user to be fetched in textboxes to edit profile of user accordingly based on username stored in session I don't want which statement is wrong I am not able to fetch values in textboxes. Please help me.
Profile.php
<?php
include('connection.php');
//include('validation1.php');
session_start();
if( !empty($_SESSION['username']) && isset($_SESSION['username']))
{
$username=$_SESSION['username'];
$checkinfo = mysql_query("SELECT * FROM profile WHERE username=".'"$username"');
//$result=mysql_fetch_array($checkinfo);
while($result = mysql_fetch_array($checkinfo,MYSQL_ASSOC)){
$name =($result['name']);
$address =($result['address']);
$contact =($result['contact']);
$state =($result['state']);
$city = ($result['city']);
$username =($result['uname']);
$oldpass = ($result['opass']);
}
}
if (isset($_POST['submit']))
{
$name =($_POST['name_txt']);
$address =($_POST['address_txt']);
$contact =($_POST['contact_txt']);
$gender = ($_POST['gender']);
$country = ($_POST['country']);
$state =($_POST['state_txt']);
$city = ($_POST['city_txt']);
$username =($_POST['uname_txt']);
$oldpass = ($_POST['opass_txt']);
$newpass = ($_POST['npass_txt']);
$insquery="UPDATE INTO profile(name, address, contact,gender, country, state, city,username,opassword,npassword) VALUES ('$name','$address','$contact','$gender','$country','$state','$city','$username','$oldpass','$newpass')";
$result=mysql_query($insquery);
// header("location:display.php");
if(!$insquery)
{
echo "Error Save [".mysql_error()."]";
}
/*else
{
// header("location:display.php");
}*/
}
?>
<body>
<form id="form1" name="form1" method="post" action="">
<div align="center">
Registration Form
</div>
<div align="right">
<?php if(isset($_SESSION['username']))
{
$s="Hello,".$_SESSION["username"];
$r=$_SESSION["userrole"];
echo $s;
} ?><a href='logout.php' id='logout'>Logout</a>
</div>
<table>
<tr>
<td><label>Name:</label></td>
<td><input name="name_atxt" type="text" id="name_atxt" value="<?php echo $name; ?>" /></td>
</tr>
<tr>
<td><label>Address:</label></td>
<td><textarea name="address_txt" cols="40" rows="4" value="<?php echo $address; ?>"></textarea></td>
</tr>
<tr>
<td><label>Contact:</label></td>
<td><input name="contact_txt" type="text" id="contact_ntxt" value="<?php echo $contact; ?>" /></td>
</tr>
<tr>
<td><label>Gender:</label>
<td>
<input type=radio name=gender value="male" id=male >Male</br>
<input type=radio name=gender value="female" id=female >Female</br></td>
</td>
</tr>
<tr>
<td><label>Country:</label></td>
<td><select name="country_select" id="country_select">
<option value="0">--select a country--</option>
<option value="India">India</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
<option value="Australia">Australia</option>
</select></td>
</tr>
<tr>
<td><label>State:</label></td>
<td><input name="state_atxt" type="text" id="state_atxt" value="<?php echo $state; ?>"/></td>
</tr>
<tr>
<td><label>City:</label></td>
<td><input name="city_atxt" type="text" id="city_atxt" value="<?php echo $city; ?>" /></td>
</tr>
<tr>
<td><label>Username:</label></td>
<td><input name="uname_txt" type="text" id="uname_txt" value="<?php echo $username; ?>" /></td>
</tr>
<tr>
<td><label>Old Password:</label></td>
<td><input name="opass_txt" type="password" id="opass_txt" value="<?php echo $oldpass; ?>" /></td>
</tr>
<tr>
<td><label>New Password:</label></td>
<td><input name="npass_txt" type="text" id="npass_txt" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit" id="btn1" /></td>
<td><input name="reset_btn" type="reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
I want values already stored in database should be fetched in textbox of particular user who is in session and it is not fetching values in textbox.

$checkinfo = mysql_query("SELECT * FROM profile WHERE username=".'"$username"');
should be
$checkinfo = mysql_query("SELECT * FROM profile WHERE username='$username'");
Also your UPDATE syntax is incorrect

SQL UPDATE Statement:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Related

Cannot get array after unserialize()

I am not getting my orignal array after using unserialize().
I am retrieving array from database and want to display it.
I have 2 arrays, created them from checkbox input, then I serialize() them and stored it in database.
Create.php
<table>
<tr>
<td>Aadhar</td>
<td><input type="checkbox" name="original[]" value="Aadhar"/></td>
<td><input type="checkbox" name="xerox[]" value="Aadhar"/></td>
</tr>
<tr>
<td>Pan Card</td>
<td><input type="checkbox" name="original[]" value="Pan Card"/></td>
<td><input type="checkbox" name="xerox[]" value="Pan Card"/></td>
</tr>
<tr>
<td>Address</td>
<td><input type="checkbox" name="original[]" value="Address"/></td>
<td><input type="checkbox" name="xerox[]" value="Address"/></td>
</tr>
<tr>
<td>Light Bill</td>
<td><input type="checkbox" name="original[]" value="Light Bill"/></td>
<td><input type="checkbox" name="xerox[]" value="Light Bill"/></td>
</tr>
<tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
Create2.php(this page is just like confirmation page..before user submit to the database.)
<?php
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$company = $_POST['company'];
$original_list = $_POST['original'];
$xerox_list = $_POST['xerox'];
?>
<form action="create2.php" method="POST">
<table cellpadding="10">
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $name; ?>" readonly="readonly" ></td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="mobile" value="<?php echo $mobile; ?>" readonly="readonly" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $email; ?>" readonly="readonly" /></td>
</tr>
<tr>
<td>Company</td>
<td><input type="text" name="company" value="<?php echo $company; ?>" readonly="readonly" /></td>
</tr>
<tr>
<input type="hidden" name="original_list" value="<?php $original_list ?>" />
<input type="hidden" name="xerox_list" value="<?php $xerox_list ?>" />
<td>Documents Selected</td>
<td>
<?php
echo "<b>ORIGINAL</b><br />";
foreach($original_list as $value)
{
echo $value."<br />";
}
?>
</td>
<td>
<?php
echo "<b>XEROX</b><br />";
foreach($xerox_list as $value)
{
echo $value."<br />";
}
?>
</td>
</tr>
<tr>
<td><input type="submit" value="Confirm" /></td>
</tr>
</table>
</form>
</body>
Create2.php (insert in database)
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$company = $_POST['company'];
$original = serialize($_POST['original_list']);
$xerox = serialize($_POST['xerox_list']);
echo $sql = "INSERT INTO users (name,mobile,email,company,original,xerox)
VALUES ('$name','$mobile','$email','$company','$original','$xerox')";
mysqli_query($con,$sql);
So when I try to unserialize, and display the array using foreach, this is what I get:
Warning: Invalid argument supplied for foreach()
Code:
$original = unserialize($rows['original']);
foreach($original as $value)
{
echo $value."<br />";
}

Update sql table on php using html forms

I have been trying to update a record on my table by using an html form. I am able to create and delete a record successfully but I am unable to update it. I am not sure what I have done wrong. Could it be the SQL query syntax? or is my save button not calling my condition statement? I would appreciate any advice given.
ps. I am aware that my SQL database is open to SQL injection. It will be implemented soon!
<?php
include('partregister2.php');
$epr='';
$msg='';
if(isset($_GET['epr']))
$epr=$_GET['epr'];
//+++++++++++++++ UPDATE PARTICIPANTS RECORD +++++++++++++++++
if($epr=='saveup'){
$Name=$_POST['name'];
$Surname=$_POST['surname'];
$Date_of_Birth=$_POST['dob'];
$Age_at_Camp=$_POST['age'];
$Branch=$_POST['branch'];
$Gender=$_POST['gender'];
$Address=$_POST['address'];
$Contact_No=$_POST['contactNo'];
$Next_of_Kin=$_POST['nextKin'];
$Kin_ContactNo=$_POST['kinContact'];
$Attendance_Camp=$_POST['attendCamp'];
$Attendance_School=$_POST['attendSchool'];
$Comments=$_POST['comments'];
$event_name_FK=$_POST['Event_Name'];
$Room_Name_FK=$_POST['Room_Name'];
$a_sql = mysql_query("UPDATE participants SET Name='$Name',Surname='$Surname',Date_of_Birth ='$Date_of_Birth',Age_at_Camp ='$Age_at_Camp',Branch ='$Branch',Gender ='$Gender',Address ='$Address',
Contact_No ='$Contact_No',Next_of_Kin ='$Next_of_Kin',Kin_ContactNo = '$Kin_ContactNo',Attendance_Camp ='$Attendance_Camp',Attendance_School ='$Attendance_School',Comments ='$Comments',event_name_FK ='$event_name_FK',Room_Name_FK ='$Room_Name_FK' WHERE partID='$id'");
if(a_sql)
header("location:index.php");
else
$msg='Error : '.mysql_error();
}
?>
<html>
<head>
</head>
<body>
<?php
if($epr=='update'){
$id=$_GET['id'];
$row=mysql_query("SELECT * FROM participants WHERE partID='$id'");
$st_row=mysql_fetch_array($row);
?>
<h2 align="center">Update Participant Records</h2>
<form method="POST" action='index.php?epr=saveup'>
<table align="center">
<tr>
<td>First Name:</td>
<td><input type='text' name ='name' value="<?PHP echo $st_row['Name'] ?>"/></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type='text' name ='surname' value="<?PHP echo $st_row['Surname'] ?>"/></td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><input type='date' name ='dob' value="<?PHP echo $st_row['Date_of_Birth'] ?>"/></td>
</tr>
<tr>
<td>Age at Camp:</td>
<td><input type='text' name ='age' value="<?PHP echo $st_row['Age_at_Camp'] ?>"/></td>
</tr>
<tr>
<td>Branch:</td>
<td><select name='branch' value="<?PHP echo $st_row['Branch'] ?>"/>
<option></option>
<option>Brixton</option>
<option>North London</option>
<option>East London</option>
<option>Southall</option>
<option>Leicester</option>
<option>Newport</option>
<option>Liverpool</option></td>
</tr>
</select>
<tr>
<td>Gender:</td>
<td>Male<input type="radio" value="male" name="gender" value="<?PHP echo $st_row['Gender'] ?>"/>
Female<input type="radio" value="female" name="gender" value="<?PHP echo $st_row['Gender'] ?>" /><td/>
</tr>
<tr>
<td>Address:</td>
<td><input type='text' name ='address' value="<?PHP echo $st_row['Address'] ?>"/></td>
</tr>
<tr>
<td>Contact No:</td>
<td><input type='text' name ='contactNo' value="<?PHP echo $st_row['Contact_No'] ?>"/></td>
</tr>
<tr>
<td>Next of Kin:</td>
<td><input type='text' name ='nextKin' value="<?PHP echo $st_row['Next_of_Kin'] ?>"/></td>
</tr>
<tr>
<td>Kin's Contact No:</td>
<td><input type='text' name ='kinContact' value="<?PHP echo $st_row['Kin_ContactNo'] ?>"/></td>
</tr>
<tr>
<td>Attendance at Camp:</td>
<td><input type='text' name ='attendCamp' value="<?PHP echo $st_row['Attendance_Camp'] ?>"/></td>
</tr>
<tr>
<td>Attendance at Sunday School:</td>
<td><input type='text' name ='attendSchool' value="<?PHP echo $st_row['Attendance_School'] ?>"/></td>
</tr>
<tr>
<td>Comments:</td>
<td><input type='text' name ='comments' value="<?PHP echo $st_row['Comments'] ?>"/></td>
</tr>
<tr>
<td>Event Name:</td>
<td><select name='Event_Name' value="<?PHP echo $st_row['event_name_FK'] ?>">
<?php
$res = mysql_query("SELECT * FROM events");
while($row=mysql_fetch_array($res))
{
?>
<option>
<?php echo $row["Event_Name"]; ?>
</option>
<?php } ?>
</tr>
</select>
<tr>
<td>Allocate Room:</td>
<td><select name='Room_Name' value="<?PHP echo $st_row['Room_Name_FK'] ?>">
<?php
$res = mysql_query("SELECT * FROM rooms");
while($row=mysql_fetch_array($res))
{
?>
<option>
<?php echo $row["Room_Name"]; ?>
</option>
<?php } ?>
</td>
</select>
</tr>
<td></td>
<tr>
<td></td>
<td><input type ='submit' name='save'/></td>
</tr>
</table>
</form>
<?php } else{
?>
</body>
</html>
I think you forgot to add $
$a_sql = mysql_query("UPDATE participants SET Name='$Name',Surname='$Surname',Date_of_Birth ='$Date_of_Birth',Age_at_Camp ='$Age_at_Camp',Branch ='$Branch',Gender ='$Gender',Address ='$Address',
Contact_No ='$Contact_No',Next_of_Kin ='$Next_of_Kin',Kin_ContactNo = '$Kin_ContactNo',Attendance_Camp ='$Attendance_Camp',Attendance_School ='$Attendance_School',Comments ='$Comments',event_name_FK ='$event_name_FK',Room_Name_FK ='$Room_Name_FK' WHERE partID='$id'");
if($a_sql) //here
header("location:index.php");
else
$msg='Error : '.mysql_error();
After executing sql query, you have used a_sql variable without $ sign inside if condition, i have improved your code.
if($a_sql)
header("location:index.php");
else
$msg='Error : '.mysql_error();

How to create a jquery function to prevent duplicate entries to the database

I have created a form which takes the data and inserts it in the database. onlick of create button i want the function to check database whether the entry with employee_id already exists. if exists i want it to display the data already exists do you still want to insert, i am noob in this can anybody help me with this. the form is
<form id="myForm" name="myForm" action='insert.php' method='post' >
<input type='hidden' name='st' value=0>
<table style="text-align:center; width:100%">
<tr>
<td style="text-align:right"><label>Select SE/AE:</label></td>
<td style="text-align:left">
<?php include("configs.php");
$sql = "SELECT DISTINCT seae FROM se_ae ";?>
<select name="seae">
<option value="" selected></option>
<?php foreach ($dbo->query($sql) as $row) { ?>
<option value="<?php echo $row['seae']; ?>">
<?php echo $row['seae']; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td style="text-align:right"><label>Select Brand:</label></td>
<td style="text-align:left">
<?php //include("configs.php");
$sql = "SELECT DISTINCT `brand` FROM se_ae ";?>
<select name="brand">
<option value="" selected> </option>
<?php foreach ($dbo->query($sql) as $row) { ?>
<option value="<?php echo $row['brand']; ?>">
<?php echo $row['brand']; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td style="text-align:right"><label>Select Territory:</label></td>
<td style="text-align:left">
<?php //include("configs.php");
$sql = "SELECT DISTINCT `territory` FROM se_ae ";?>
<select name="territory">
<option value="" selected></option>
<?php foreach ($dbo->query($sql) as $row) { ?>
<option value="<?php echo $row['territory']; ?>">
<?php echo $row['territory']; ?></option>
<?php }?>
</select>
</td>
</tr>
<tr>
<td style="text-align:right"><label for="name">Employee Name:</label></td>
<td style="text-align:left">
<input type="text" id="name" name="name"/>
</td>
</tr>
<tr>
<td style="text-align:right"><label for="number">Employee ID:</label></td>
<td style="text-align:left">
<input type="text" id="number" name="number" />
</td>
</tr>
<tr>
<td style="text-align:right"><label for="email"> Email:</label></td>
<td style="text-align:left">
<input type="text" id="email" name="email" />
</td>
</tr>
<tr>
<td style="text-align:right"><label for="contact"> Contact:</label></td>
<td style="text-align:left">
<input type="text" id="contact" name="contact" />
</td>
</tr>
<tr>
<td style="text-align:right"><label for="exist"> Exist:</label></td>
<td style="text-align:left">
<input type="radio" id="exist" name="exist" value="Active"/>Active
<input type="radio" id="exist" name="exist" value="NA"/>NA
</td>
</tr>
<tr>
<td style="text-align:right" class='swMntTopMenu'>
<input style="background-color:rgb(255,213,32)" name="Reset" type="reset" value="Reset">
</td>
<td style="text-align:left" class='swMntTopMenu'>
<input style="background-color:rgb(255,213,32)" name="submit" type="submit" value="Create">
</td>
</tr>
</table>
</form>
You can use the Jquery validation
$(function () {
$("#resturantRegistration").validate({
rules: {
number: {
required: true,
remote:"user/checkEmployee"
}
},
messages: {
number: {
required: "Please enter Employee id",
remote: $.validator.format("Employee id already in use")
}
}
});
});
PHP function
function checkEmployee(){
$emailid = $_GET['number'];
if(!empty($emailid)){
$emailRes = $this->users->is_email_available($emailid);
if ($emailRes == ''){
echo 'false';
} else {
echo 'true';
}
}
}
Model function to check with database
/**
* Check if email available for registering
*
* #param string
* #return bool
*/
function is_email_available($email)
{
$this->db->select('1', FALSE);
$this->db->where('LOWER(email)=', strtolower($email));
$this->db->or_where('LOWER(new_email)=', strtolower($email));
$query = $this->db->get($this->table_name);
return $query->num_rows() == 0;
}
above code is follow the codeigniter MVC framework you can customize this in core PHP as well

avoiding duplicate entries and caching entered form values

i have some code that controls duplicate entries in particular the USER ID. it checks in the database at submit and if that USER ID exists already it gives that notification. now the problem is when i submit and if that USER ID entered already exists in the database, all the other entries on the form are cleared, prompting me to re_enter all the other details again. i find this annoying and retrogressive. i want some help on how better i can do it such that only the USER ID text box returns empty, keeping other details safe/unchanged or indeed alternatively keeping/buffering/caching all details previously entered so that i can also review the duplicate USER ID before changing it.
new_user.php
<h1 align="center">Create New User</h1>
<p align="center" style="color:#F00"><?php if(isset($_GET['dup'])){ echo "That User ID Already Exists!"; } ?> </p>
<form id="form1" method="post" action="add_user.php">
<table width="100%">
<tr>
<td width="204"><div align="right">User ID:</div></td>
<td width="286">
<input type="text" name="user_id" id="user_id" />
</td>
</tr>
<tr>
<td><div align="right">Full Names:</div></td>
<td>
<input type="text" name="fname" id="fname" />
</td>
</tr>
<tr>
<td><div align="right">Gender:</div></td>
<td><select id="sex" name="sex">
<option selected="selected" value="male">Male</option>
<option name="female">Female</option>
</select></td>
</tr>
<tr>
<td><div align="right">NRC Number:</div></td>
<td>
<input type="number" name="nrcno" id="nrcno" min="1" />
</td>
</tr>
<tr>
<td><div align="right">Phone Number:</div></td>
<td>
<input type="number" name="cellno" id="cellno" />
</td>
</tr>
<tr>
<td><div align="right">Email Address:</div></td>
<td>
<input type="email" name="emailad" id="emailad" />
</td>
</tr>
<tr>
<td><div align="right">Position Held:</div></td>
<td>
<input type="text" name="posh" id="posh" />
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="create" id="create" value="Add User" /></td>
</tr>
</table>
</form>
add_user.php
<?php
$user_id=$_POST['user_id'];
$fname = $_POST['fname'];
$sex= $_POST['sex'];
$name= $_POST['name'];
$nrcno = $_POST['nrcno'];
$cellno= $_POST['cellno'];
$emailad = $_POST['emailad'];
$posh = $_POST['posh'];
require("get_func.php");
checkID($id);
include("connect.php");
mysql_select_db("ceec", $con);
$query = "INSERT INTO user VALUES ('$user_id', '$fname', '$sex','$name', '$nrcno', '$cellno', '$emailad', '$posh')";
if (mysql_query($query)){
header("Location: success.php");
}
else {echo "Nada" . mysql_error(); }
mysql_close($con);
?>
get_func.php
<?php
function checkID($id){
include_once("connect.php");
mysql_select_db("ceec",$con);
$query = "SELECT * FROM user WHERE user_id = '$id'";
$result= mysql_query($query);
if($row = mysql_fetch_array($result))
{
header("Location: new_user.php?dup=true");
break;
}
else {}
}
?>
<input type="text" name="user_id" id="user_id"
<?php if(isset($_POST['user_id'])){echo htmlentities($_POST['user_id'];} ?>/>

How to Insert form values into MySQL database without refreshing the form and losing the field values of the form?

Hi,
I have a form and I want to insert its values into a MySQL database without refreshing the form and losing its values.
By clicking on the 'print' button the user should be able to generate a report from the data they entered in the form, but the form keeps refreshing on submit. So I would like to know how I can store the data in the database without having the form refreshed (i.e. using Ajax)?
Below is my form code:
<div class="commentpost"></div>
<form name="entry" id="entry" method="POST" action="">
<table border="1" bgcolor="Silver">
<tr>
<td>
</td>
<td>
<h2><b>Candidate Entry</b><h2>
</td>
</tr>
<tr>
<td>
Code
</td>
<td>
<input type="text" name="vouchno" value="New" onkeypress="return isNumberKey(event)" readonly="readonly" size="8" maxlength="8">
Date
<input type="text" id="vouchdt" name="vouchdt" id="popupDatepicker" tabindex="1" value="<?php echo (isset($_POST['vouchdt']) ? $_POST['vouchdt'] : ''); ?>"></td>
<td>
<input type="text" id='councode' name="councode" size="1" maxlength="2" value="<?php echo (isset($_POST['councode']) ? $_POST['councode'] : ''); ?>"><input type="text" id="counvouch" name="counvouch" size="8" value="<?php echo (isset($_POST['counvouch']) ? $_POST['counvouch'] : ''); ?>" maxlength="8">
<div id="cam">
</div>
<input type=button value="Configure..." onClick="webcam.configure()">
<input type=button value="Take Snapshot" onClick="take_snapshot()">
<div id="upload_results" style="background-color:#eee;"></div>
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
<input type="text" id="name" name="name" value="<?php echo (isset($_POST['name']) ? $_POST['name'] : ''); ?>" maxlength="40" size="45" tabindex="2">
</td>
</tr>
<tr>
<td>
Address
</td>
<td>
<textarea name="add" id="add" row="3" cols="40" tabindex="3">
<?php echo (isset($_POST['vouchdt']) ? $_POST['vouchdt'] : ''); ?>
</textarea>
</td>
</tr>
<tr>
<td>
City
</td>
<td>
<input type="text" id="city" name="city" size="20" maxlength="20" value="<?php echo (isset($_POST['city']) ? $_POST['city'] : ''); ?>" tabindex="4">
Pin
<input type="text" id="pin" name="pin" size="6" maxlength="6" value="<?php echo (isset($_POST['pin']) ? $_POST['pin'] : ''); ?>" tabindex="5">
</td>
</tr>
<tr>
<td>
Nationality
</td>
<td>
<input type="text" id="ntn" name="ntn" value="<?php echo (isset($_POST['ntn']) ? $_POST['ntn'] : ''); ?>" size="10" maxlength="10" tabindex="6">
</td>
</tr>
<tr>
<td>
Mobile No
</td>
<td>
<input type="text" id="mob" name="mob" value="<?php echo (isset($_POST['mob']) ? $_POST['mob'] : ''); ?>" tabindex="7">
</td>
</tr>
<tr>
<td>
Date of Birth
</td>
<td>
<input type="text" id="dob" name="dob" value="<?php echo (isset($_POST['dob']) ? $_POST['dob'] : ''); ?>" id="popupDatepicker2" tabindex="8">
</td>
<td>
Age
</td>
<td width="9px">
<input type="text" id="age" name="age" size="3" maxlength="3" value="<?php echo (isset($_POST['age']) ? $_POST['age'] : ''); ?>" tabindex="9">
</td>
<td width="10px">
Sex
</td>
<td>
<select id="sex" name="sex" tabindex="12">
<?php
if(isset($_POST[sex])==m){
?>
<option value="<?php echo (isset($_POST['sex']) ? $_POST['sex'] : ''); ?>">Male</option>":
<?php }else{?>
<option value="<?php echo (isset($_POST['sex']) ? $_POST['sex'] : ''); ?>">Female</option>":
<?php }?>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</td>
</tr>
<tr>
<td>
Religion
</td>
<td>
<input type="text" id="rel" name="rel" value="<?php echo (isset($_POST['rel']) ? $_POST['rel'] : ''); ?>" size="20" maxlength="20" tabindex="11">
</td>
<td>
Martial Status
</td>
<td>
<select id ="status" name="status" tabindex="12">
<option value="">--select--</option>
<option value="1">Married</option>
<option value="2">Unmarried</option>
</select>
</td>
</tr>
<tr>
<td>
Passport No.
</td>
<td>
<input type="text" id="pass" name="pass" value="" size="15" maxlength="15" tabindex="13">
</td>
<td>
Place of Issue
</td>
<td>
<input type="text" id="poi" name="poi" size="20" maxlenght-20 tabindex="14">
</td>
<td>
Date of Issue
</td>
<td>
<input type="text" id="doi" name="doi" id="popupDatepicker4" tabindex="15">
</td>
</tr>
<tr>
<td>
Profession
</td>
<td>
<input type="text" id="prof" name="prof" size="20" maxlenght="20" value="" tabindex="16">
</td>
<td>
Amount
</td>
<td>
<input type="text" id="amt" name="amt" value="" size ="8" onblur="calculateText()" style="background-color:transparent; color:blue; text-align:right" tabindex="17">
</td>
</tr>
<tr>
<td>
Payment
</td>
<td>
<select id="pay" name="pay" tabindex="12">
<option value="">--select--</option>
<option value="f">Full</option>
<option value="p">Part</option>
<option value="n">None</option>
</select>
</td>
<td>
Received
</td>
<td>
<input type="text" id="resc" name="resc" value="" size ="8" onblur="calculateText()" style="background-color:transparent; color:green; text-align:right" tabindex="18">
</td>
</tr>
<tr>
<td>
Agent
</td>
<td>
<input type="tetx" id="agnt" name="agnt" value="" size="40" maxlength="40" tabindex="21">
</td>
<td>
Balance
</td>
<td>
<input type="text" id="bal" name="bal" readonly="readonly" value="" size ="8" style="background-color:transparent; color:red; text-align:right" onblur="calculateText()" tabindex="19">
</td>
</tr>
<tr>
<td>
Mofa No.
</td>
<td>
<input type="text" id="mofa" name="mofa" value="" size="20" maxlength="20" tabindex="22">
</td>
</tr>
<tr>
<td>
Remarks
</td>
<td>
<input type="text" id="rem" name="rem" size="60" maxlength="60" value="" tabindex="23">
</td>
</tr>
<table border="0" align="center">
<tr>
<td>
<input type="submit" name="save" value="Save"><input type="hidden" name="task" value="addComments" />
<td>
<input type="submit" name="print" value="Print">
</td>
<td>
<input type="submit" name="close" value="Cancel">
</td>
<td>
<input type="reset" name="Add" value="Add">
</td>
</tr>
</table>
</table>
</form>
</body>
</html>
Here is my JavaScript code:
<script type="text/javascript">
jQuery(document).ready(function($){
$("#entry").submit(function(){
ctask = this.task.value;
cvouchdt = this.vouchdt.value;
ccouncde = this.councode.value;
ccounvouch = this.counvouch.value;
cname = this.name.value;
ccity = this.city.value;
cpin = this.pin.value;
cntn = this.ntn.value;
cmob = this.mob.value;
cdob = this.dob.value;
cage = this.age.value;
csex = this.sex.value;
crel = this.rel.value;
cstatus = this.status.value;
cpass = this.pass.value;
crel = this.rel.value;
cpoi = this.poi.value;
cdoi = this.doi.value;
cprof = this.prof.value;
camt = this.amt.value;
cpay = this.pay.value;
cpass = this.pass.value;
crecd = this.recd.value;
cagnt = this.agnt.value;
cbal = this.bal.value;
cmofa = this.mofa.value;
crem = this.rem.value;
save = this.save;
if(cname=="" || ccounvouch=="" || ccouncde=="") { $("#errAll").html('<p>Invalid Captcha. Please try again.</p>'); }
$.post("submit.php", {task: ctask, name: cname, email: cemail, url: curl, message: cmessage}, function(data){
if(data=='0') { $("#errAll").html('<p>Please don\'t leave the requierd fields.</p>'); }
else if(data=='1') { $("#errAll").html('<p>Invalid Email Address, Please try again.</p>'); }
else { submitter.value="Value Saved"; save.disabled=false;} //$(data).appendTo($(".commentpost")); }
});
return false;
});
});
</script>
Here is my PHP code:
<?php
//include_once( 'config.php' );
require("includes/dbconnect.php");
$getmaxvou = mysql_query("SELECT MAX(`vouchno`) as `maxid` FROM `candidate` ") or die(mysql_error());
$max = mysql_fetch_array($getmaxvou);
$maxv =$max["maxid"]+1;
if(isset($_POST['task']) && $_POST['task'] == 'addComments')
{
$a = 1;
$date = mysql_real_escape_string($_POST["vouchdt"]);
$date = strtotime($date);
$date = date('Y-m-d',$date);
$cname = trim($_POST["name"]);
$add = trim($_POST["add"]);
$city = trim($_POST["city"]);
$pin = trim($_POST["pin"]);
$nations = trim($_POST["ntn"]);
$mob = trim($_POST["mob"]);
$dob = mysql_real_escape_string($_POST["dob"]);
$dob = strtotime($dob);
$dob = date('Y-m-d',$dob);
$age = trim($_POST["age"]);
$sex = trim($_POST["sex"]);
$rel = trim($_POST["rel"]);
$pass = trim($_POST["pass"]);
$status = trim($_POST["status"]);
$poi = trim($_POST["poi"]);
$doi = mysql_real_escape_string($_POST["doi"]);
$doi = strtotime($doi);
$doi = date('Y-m-d',$doi);
$prof = trim($_POST["prof"]);
$amt = trim($_POST["amt"]);
$pay = trim($_POST["pay"]);
$bal = trim($_POST["bal"]);
$recd = trim($_POST["resc"]);
$agnt = trim($_POST["agnt"]);
$mofa = trim($_POST["mofa"]);
$rem = trim($_POST["rem"]);
$councode = trim($_POST["councode"]);
$counvouch = trim($_POST["counvouch"]);
{
if (isset($_POST["code"])) {
$sql_check = mysql_query("SELECT * FROM candiidate WHERE code ='$councode' AND counvouch='$counvouch'");
if (mysql_num_rows($sql_check) > 0) {
$a = 0;
print '<script type="text/javascript">';
print 'alert("Code Already Exist For the Country")';
PRINT '</script>';
}
}
if($a ==1){
mysql_query("INSERT INTO `candidate`(vouchno, vouchdt, `name`, `add`, `city`, `pin`, `nationality`, mobile, dob, `religion`, `passport`, `profession`, amt, recd, bal, payment, `agent`, `mofa`, `age`, `gender`, `martial`, `poi`, doi, councode, counvouch, `rem` )
Values
('$maxv', '$date', '$cname', '$add', '$city', '$pin', '$nations', '$mob', '$dob', '$rel', '$pass', '$prof', '$amt', '$recd', '$bal', '$pay', '$agnt', '$mofa', '$age', '$sex', '$status', '$poi', '$doi', '$councode', '$counvouch', '$rem' )") or die(mysql_error());
echo ' <div class="commentbox">
<div class="commentboxt">Value Inserted </div>
</div>';
}
}
}?>
Change form tag to
<form name="entry" id="entry" method="GET" action="javascript:">
that will stop the page from refreshing.
Then look into jquery ajax http://api.jquery.com/jQuery.ajax/
For example.
var string ="FORMVALUE1=" + $('#FORMVALUE1ID').val() + "FORMVALUE2=" + $('#FORMVALUE2ID').val() + "REAPEAT FOR ALL YOUR VALUES";
$.ajax({
type: "GET",
url: "YOURPHPFILE.php",
data: string,
});
Then just get the form values in the php file by using $_GET['FORMVALUE1'];
You first seperate your PHP file where you insert or do some action.
PHP Action File: YourPHPPostFile.php
HTML File: index.html or whatever name you have.
Then in PHP file write all your code but in the begining add check that is
if(isset($_POST['task']) && $_POST['task'] == 'addComments')
{//Your all PHP Code here}
On the other hand in your html file make a AJAX request to this file and use POST Method in AJAX request and include your data within Request Parameters.
One more thing if you don't want to lose your Form data which is filled you can use:
<form name="entry" id="entry" method="GET" onsubmit="calltoAjax();return false;">
Now whenever user submit form then it will not reset the form and data also stored in your database.
Thanks,

Categories