I created this form with PHP and MySQL. Whenever I submit the form and click update button it is showing unsucess. I don't know what is going wrong and I need help finding the problem. I've attached the code below.
<?php
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
$con = mysqli_connect("localhost","root","") or die("unable to connect");
mysqli_select_db($con,'studentdb');
if(isset($_GET['registernumber']))
$registernumber = html_entity_decode($_GET['registernumber']);
$query= "SELECT * FROM studenttble WHERE registernumber='$registernumber'";
$query_run = mysqli_query($con,$query);
while($row = mysqli_fetch_array($query_run, MYSQL_ASSOC))
{
$IID = $row['IID'];
$studentname = $row['studentname'];
$fathername = $row['fathername'];
$registernumber = $row['registernumber'];
$year = $row['$year'];
$dept = $row['$dept'];
$section = $row['section'];
$gender = $row['gender'];
$password = $row['password'];
$email = $row['email'];
$dob = $row['dob'];
$raddress = $row['raddress'];
$mnumber = $row['mnumber'];
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" id="form1" class="myform">
<table align="center">
<tr>
<td>
<input name="estudentname" type="text" id="studentname" class="wp-form-control wpcf7-text" value="<?php echo $studentname?>" />
</td>
</tr>
<br>
<tr>
<td>
<input name="efathername" type="text" id="fathername" class="wp-form-control wpcf7-text" value="<?php echo $fathername?>" />
</td>
</tr>
<tr>
<td>
<input type="hidden" name="registernumber" id="registernumber" class="wp-form-control wpcf7-text" value="<?php echo $registernumber?>" placeholder="Register Number" />
</td>
</tr>
<tr>
<td>
<input type="text" name="egender" id="gender" class="wp-form-control wpcf7-text" value="<?php echo $gender?>" />
</td>
</tr>
<tr>
<td><input type="text" name="eemail" type="email" id="email" class="wp-form-control wpcf7-text" value="<?php echo $email?>" />
</td>
</tr>
<tr>
<td>
<h3 class="table">Date Of Birth: <input type="text" name="edob" id="dob" type="date" class="dropdown-menu-left" value="<?php echo $dob?>" max="2017-02-08"/></td></tr>
</h3>
<tr>
<td> <input type="text" class="wp-form-control wpcf7-textarea" name="eraddress" cols="30" rows="10" value="<?php echo $raddress?>"></textarea>
</td>
</tr>
<tr>
<td> <input name="emnumber" type="text" id="mnumber" pattern="[789][0-9]{9}" class="wp-form-control wpcf7-text" max="10" value="<?php echo $mnumber?>" />
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="update" class="wpcf7-submit" value="update">
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['update']))
{
$con = mysqli_connect("localhost","root","") or die("unable to connect");
mysqli_select_db($con,'studentdb');
$estudentname = mysql_real_escape_string($_POST['estudentname']);
$efathername = mysql_real_escape_string($_POST['efathername']);
$egender = mysql_real_escape_string($_POST['egender']);
$eemail = mysql_real_escape_string($_POST['eemail']);
$edob = mysql_real_escape_string($_POST['edob']);
$eraddress = mysql_real_escape_string($_POST['eraddress']);
$emnumber = mysql_real_escape_string($_POST['emnumber']);
$targetid = mysqli_real_escape_string($_POST['registernumber']);
mysqli_select_db('studentdb');
$query = "UPDATE studenttble SET studentname ='$estudentname', fathername ='$efathername', gender ='$egender', email ='$eemail', dob ='$edob', raddress ='$eraddress', mnumber ='$emnumber' WHERE registernumber ='$targetid'";
$retval = mysqli_query( $query, $conn );
if($retval)
{
echo "sucess";
}
else
{
echo "unsucess";
}
}
?>
Related
I have combobox that loads the value from database. I want when user select value from the combobox the form should load the corresponding values from database into html form. Here is code I am trying:
<!DOCTYPE html>
<html>
<head>
<title>Obracun plata</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link href="layout.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<?php
error_reporting(0);
mysql_connect("localhost", "root", "");
mysql_select_db("zaposleni");
$query = "SELECT * FROM radnici;";
$rezultat = mysql_query($query);
?>
<div id="wrapper">
<div id="logo">
<h1>Obračun plata</h1>
<p>Income calculator</p>
</div>
<div id="menu">
<ul>
<li>Pocetna</li>
<li>Podaci o radnicima </li>
<li>Spisak radnih mjesta</li>
<li>Obracun</li>
<li>Spisak gradova</li>
<li>Spisak mjeseci</li>
</ul>
</div>
<div id="page">
<div id="content">
<br/>
<td>Radnici:</td>
<td>
<select name="Radnik">
<?php
error_reporting(0);
mysql_connect("localhost", "root", "");
mysql_select_db("zaposleni");
$query = "SELECT * FROM radnici;";
$rezultat = mysql_query($query);
for($data = mysql_fetch_row($rezultat); $data != null; $data = mysql_fetch_row($rezultat))
echo "<option value='" . $data[0] . "'>" . $data[1] . " " . $data[2];
?>
</select>
<p> </p>
<?php
error_reporting(0);
if($_POST["Radnik"] == "")
echo "<div style='color:red'>Niste izabrali radnika</div>";
else
{
mysql_connect("localhost", "root", "");
mysql_select_db("zaposleni");
$query1 = "SELECT * FROM radnici WHERE id ='" . $_POST["Radnik"] . "';";
$query2 = "SELECT grad.naziv FROM grad, radnici WHERE radnici.grad_id=grad.id AND radnici.id='" . $_POST["Radnik"] . "';";
$query3 = "SELECT radno_mjesto.naziv FROM radno_mjesto, radnici WHERE radnici.radno_mjesto_id=radno_mjesto.id AND radnici.id='" . $_POST["Radnik"] . "';";
$rezultat1 = mysql_query($query1);
$rezultat2 = mysql_query($query2);
$rezultat3 = mysql_query($query3);
$data1 = mysql_fetch_row($rezultat1);
$data2 = mysql_fetch_row($rezultat2);
$data3 = mysql_fetch_row($rezultat3);
$prezime = $data1[1];
$ime = $data1[2];
$brtelefona = $data1[3];
$mail = $data1[4];
$brtekuceg = $data1[5];
$strucnasprema = $data1[6];
$godradnogstaza = $data1[7];
$adresa = $data1[8];
$grad = $data2[0];
$radnomjesto = $data3[0];
}
?>
<form action="podaci_o_radnicima.php" method="post">
<table>
<tr>
<td>Prezime:</td>
<td><input name="prezime" type="text" size="45" maxlength="45" value="<?php echo $prezime?>" readonly="readonly" /></td>
</tr>
<tr>
<td>Ime:</td>
<td><input type="text" name="ime" maxlength="45" size="45" value="<?php echo $ime ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Broj telefona:</td>
<td><input type="text" name="broj_telefona" maxlength="45" size="45" value=" <?php echo $brtelefona ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" maxlength="45" size="45" value="<?php echo $mail ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Broj tekuceg:</td>
<td><input type="text" name="broj_tekuceg" maxlength="45" size="45" value="<?php echo $brtekuceg ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Strucna sprema:</td>
<td><input type="text" name="strucna sprema" maxlength="45" size="45" value="<?php echo $strucnasprema ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Godina radnog staza:</td>
<td><input type="text" name="god_radnog_staza" maxlength="45" size="45" value="<?php echo $godradnogstaza ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Adresa:</td>
<td><input type="text" name="adresa" maxlength="45" size="45" value="<?php echo $adresa ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Grad:</td>
<td><input type="text" name="grad" maxlength="45" size="45" value="<?php echo $grad ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Radno mjesto:</td>
<td><input type="text" name="radno_mjesto" maxlength="45" size="45" value="<?php echo $radnomjesto ?>" readonly="readonly"/></td>
</tr>
</table>
<table>
<tr><td></td><td colspan="4" align="center"><input type="submit" value="Prikazi"> </td></tr>
</table>
</form>
</div>
</body>
</html>
I'm trying to a add a tire to a database of tires from a form. If I use a var_dump, the query is right, but it isn't inserting anything into the database. Here is my code.
<?php
require('../includes/database_connection.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);
$idtires = '';
$size = $_POST["size"];
$idbrand = $_POST["idbrand"];
$price = $_POST["price"];
$mileage = $_POST["mileage"];
$idterrain = $_POST["idterrain"];
$idvehicle = $_POST["idvehicle"];
$idtiremodel = $_POST["idtiremodel"];
function add_tire($idtires, $size, $idbrand, $price, $mileage, $idterrain, $idvehicle, $idtiremodel) {
global $db;
$query = "INSERT INTO tires
(idtires, size, idbrand, price, mileage, idterrain, idvehicle, idtiremodel)
VALUES
($idtires, '$size', '$idbrand','$price', '$mileage', '$idterrain', '$idvehicle', '$idtiremodel')";
var_dump($query);
// var_dump($db);
$db->exec($query);
}
add_tire($idtires, $size, $idbrand, $price, $mileage, $idterrain, $idvehicle, $idtiremodel);
include('add.php');
?>
HERE IS ADD.PHP form:
<table>
<form method='post' action='addatire.php' name='addform' id='addform'>
<tr>
<td>
<p>
<label for='size'>Size:</label><br />
</p>
</td>
<td>
<p>
<input type='text' name='size' required value="<?php echo(isset($size['size'])); ?>"/>
</p>
</td>
</tr>
<tr>
<td>
<p>
<label for='idbrand'>Brand ID:</label><br/>
</p>
</td>
<td>
<p>
<input type='text' name='idbrand' required value="<?php echo(isset($idbrand['idbrand'])); ?>"/>
</p>
</td>
</tr>
<tr>
<td>
<p>
<label for='price'>Price:</label><br/>
</p></td>
<td><p>
<input type='text' name='price' required value="<?php echo(isset($price['price'])); ?>"/>
</p></td>
</tr>
<tr>
<td>
<p>
<label for='mileage'>Mileage:</label><br/>
</p></td>
<td><p>
<input type='text' name='mileage' required value="<?php echo(isset($mileage['mileage'])); ?>"/>
</p></td>
</tr>
<tr>
<td>
<p>
<label for='idterrain'>Terrain ID:</label><br/>
</p></td>
<td><p>
<input type='text' name='idterrain' required value="<?php echo(isset($idterrain['idterrain'])); ?>"/>
</p></td>
</tr>
<tr>
<td>
<p>
<label for='idvehicle'>Vehicle ID:</label><br/>
</p></td>
<td><p>
<input type="text" name="idvehicle" required value="<?php echo(isset($idvehicle['idvehicle'])); ?>"/>
</p></td>
</tr>
<tr>
<td>
<p>
<label for='idtiremodel'>Tire Model ID:</label><br/>
</p></td>
<td><p>
<input type="text" name="idtiremodel" required value="<?php echo(isset($idtiremodel['idmodel'])); ?>"/>
</p></td>
</tr>
<tr>
<td><p>
<input type="submit" name="action" value="Add A Tire"/>
</p></td>
</tr>
</form>
</table>
<?php echo get_tires(); ?>
</div>
?>
HERE is the database connection:
<?php
if ($_SERVER['HTTP_HOST'] == "localhost" OR $_SERVER['HTTP_HOST'] == "127.0.0.1") {
// login locally
$dsn = 'mysql:host=localhost;dbname=?';
$username = ?;
$password = ?;
} else {
// login online
$dsn = 'mysql:host=localhost;dbname=?';
$username = '?';
$password = '?';
}
try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
?>
My usernames and passwords aren't really ? though, I'm just using those here for the sake of privacy and safety.
You're probably getting an error on your insert and just not seeing it:
$db->exec($query) or die(print_r($db->errorInfo(), true));
As a customer, I have no problem in displaying my details/profile but I can't update/edit my profile even though I have clicked the "Save" button. Am I missing something here?
edit_customer_profile.php
<?php
header('Content-Type: text/html; charset=utf-8');
include 'connection.php';
session_start();
if(isset($_SESSION['CustomerID'])) {
$customerID = $_SESSION['CustomerID'];
$customername = $_SESSION['CustomerName'];
$customers = mysql_query("select * from customer where CustomerID='".$customerID."'");
$customer = mysql_num_rows($customers);
if($customer== 1){
$row = mysql_fetch_assoc($customers);
$email = $row['CustomerEmail'];
$contactno = $row['CustomerContactNo'];
$class = $row['CustomerClass'];
$campus = $row['CustomerCampus'];
$intake = $row['CustomerIntake'];
if(isset($_POST['submit'])){
$_var1 = $_POST['new_name'];
$_var2 = $_POST['new_email'];
$_var3 = $_POST['new_contactno'];
$_var4 = $_POST['new_campus'];
$_var5 = $_POST['new_intake'];
$_var6 = $_POST['new_class'];
$query1 = "UPDATE customer
SET CustomerName='$_var1', CustomerEmail='$_var2', CustomerContactNo='$_var3', CustomerCampus='$_var4', CustomerIntake='$_var5', CustomerClass='$_var6'
WHERE CustomerID='$customerID'";
}
}
}
?>
Below is the form
<form method = "post" action=">
<tr>
<td width="170">Name:</td>
<td><input type="text" name="new_name" size="30" value="<?php echo $customername ?>" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="new_email" size="30" value="<?php echo $email ?>" /></td>
</tr>
<tr>
<td>Contact No:</td>
<td><input type="text" name="new_contactno" size="30" value="<?php echo $contactno ?>" /></td>
</tr>
<tr>
<td>Campus:</td>
<td><input type="text" name="new_campus" size="30" value="<?php echo $campus ?>" /></td>
</tr>
<tr>
<td>Intake:</td>
<td><input type="text" name="new_intake" size="30" value="<?php echo $intake ?>" /></td>
</tr>
<tr>
<td>Class:</td>
<td><input type="text" name="new_class" size="30" value="<?php echo $class ?>" /></td>
</tr>
<tr>
<td align="right"><input type="submit" size="30" name="submit" value="Save" /></td>
</tr>
</form>
You haven't run the update query.
Run your update query with
$query1 = "UPDATE customer
SET
CustomerName='$_var1',
CustomerEmail='$_var2',
CustomerContactNo='$_var3',
CustomerCampus='$_var4',
CustomerIntake='$_var5', CustomerClass='$_var6'
WHERE CustomerID='$customerID'";
mysql_query($query1);
Note: Use mysqli_* or pdo_* functions instead of mysql_ functions, which is going to deprecated.
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
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,