Newbie Help! Form posts twice.
Hello Family,
I am a newbie to JQuery but not new to programming as such.
I have this big form to validate. The form is divided into two parts, one for new users and the other for registered users.
However there are 10 mandatory fields that both the users should fill.
Once the user fills the 10 mandatory fields, then he/she has to click on a link which bifurcates the form. If he/she clicks 'New User' links then another 10 fields are shown and if he/she clicks 'Existing User' then only the username and password fields are shown.
I have downloaded the latest jquery version 1.3.
Currently working on validating username and password. What I need is when the user fills the 10 mandatory fields he/she has to provide username and password. The username and password are incorrect the user get an error message.
My problem is when the username and password are correct, my form posts the data twice, meaning I get two entries in the database.
My check.js file:
-
$(document).ready(function() {
$('.newpgerrmsg').hide();
$('#formsubmitpre p').hide();
$("#mkz").click(function() { // this is the form button id, which on click does the validation
var LogProcess = $("#LoginProcess").val();
var vtype = $("#vtype").val();
var make = $("#make").val();
var model = $("#modelid").val();
var version = $("#versionid").val();
var month = $("#month").val();
var year = $("#year").val();
var city = $("#city").val();
var other_city = $("#other_city").val();
var mileage = $("#mileage").val();
var askingprice = $("#askingprice").val();
var tyresprice = $("#tyresprice").val();
var bodycolor = $("#bodycolor").val();
var fueltype = $("#fueltype").val();
var regusr_uname = $("#logusername").val();
var regusr_pword = $("#logpassword").val();
<!-- 10 Mandatory Fields Validation Here -->
if(LogProcess=="userexist"){
if(regusr_uname==""){
$("#usrrow").fadeIn(1000).show();
$("input#regusr_uname").focus();
return false;
}
else if(regusr_pword==""){
$("#pwdrow").fadeIn(1000).show();
$("input#regusr_pword").focus();
return false;
}
else{
//alert('Form Submitted');
//return false;
$('#formsubmitpre p').show();
$.post("http://pc-3/autonagar/vehicles/vehicleformpost_new.php", {
LogProcess: LogProcess,
vtype: vtype,
make: make,
modelid: model,
versionid: version,
month: month,
year: year,
city: city,
other_city: other_city,
mileage: mileage,
askingprice: askingprice,
tyresprice: tyresprice,
bodycolor: bodycolor,
fueltype: fueltype,
logusername: regusr_uname,
logpassword: regusr_pword
},
function(data) {
$("#formsubmitpre p").html(data);
});
}
}
return false;
}); // submit function ends.
}); // document ready function ends.
My vehicleformpost_new.php file:
-
<?
session_start();
error_reporting(E_ALL);
$url = SITE_URL."/vehicles/vehicleformpost_details.php";
$logpro = $_REQUEST['LogProcess'];
if($logpro=="userexist"){
/* ******************* */
$usr = $_REQUEST['logusername'];
$pwd = $_REQUEST['logpassword'];
$iplastlogin=$_SERVER['REMOTE_ADDR'];
$BANNEDIP;
$sql = sprintf("SELECT * FROM users WHERE username='%s' AND password='%s'", mysql_real_escape_string($usr), mysql_real_escape_string($pwd));
$qry = mysql_query($sql);
$num = mysql_num_rows($qry);
if($num>0){
$row=mysql_fetch_object($qry);
if($row->valied=='N'){
$msg = "Sorry! Your account is not activated. Contact admin!";
}
else if($row->block_status=='Y'){
$msg = "Sorry! Your account has been blocked by the admin!";
}
else if(stristr($BANNEDIP,$iplastlogin)){
$msg = "Sorry! Your ip has been banned by the admin!";
}
else{
if(is_array($_REQUEST)){
foreach($_REQUEST as $var=>$valu){
$$var = $valu;
$_SESSION['NewVehData'][$var]=$$var;
echo "<script>window.location.href='".$url."';</script>";
}
}
$msg = "";
}
}
else{
$msg = "Username and password does not match!";
}
echo $msg;
/* ******************* */
}
?>
My vehicleformpost_details.php file:
-
<?
session_start();
error_reporting(E_ALL);
$curdate = gmdate("Y-m-d");
$iplastlogin=$_SERVER['REMOTE_ADDR'];
$expiryDate = "DATE_ADD(\"$curdate\", INTERVAL \"$LISTING_DAYS\" DAY)";
$logprocess = $_SESSION['NewVehData']['LogProcess'];
$uty = $_SESSION['NewVehData']['user'];
$vty = $_SESSION['NewVehData']['vtype'];
$mk = $_SESSION['NewVehData']['make'];
$mdl = $_SESSION['NewVehData']['modelid'];
$vrn = $_SESSION['NewVehData']['versionid'];
$mnt = $_SESSION['NewVehData']['month'];
$yer = $_SESSION['NewVehData']['year'];
$city = $_SESSION['NewVehData']['city'];
$ocity = $_SESSION['NewVehData']['other_city'];
$mile = $_SESSION['NewVehData']['mileage'];
$askpr = $_SESSION['NewVehData']['askingprice'];
$tyrpr = $_SESSION['NewVehData']['tyresprice'];
$bclr = $_SESSION['NewVehData']['bodycolor'];
$fuel = $_SESSION['NewVehData']['fueltype'];
// IF THE USER IS A REGISTERED USER THEN,
if($logprocess=="userexist"){
/* STEP ONE - If the user exists, then get all the user details **************** */
$sql = sprintf("SELECT * FROM users WHERE username='%s' AND password='%s'", mysql_real_escape_string($log_usr), mysql_real_escape_string($log_pwd));
$qry = mysql_query($sql);
$res = mysql_fetch_object($qry);
/* STEP TWO - Insert the record into admaster table **************** */
$put_sql = "INSERT INTO adtbl SET
vtype='".$vty."',
make='".$mk."',
modelid='".$mdl."',
versionid='".$vrn."',
month='".$mnt."',
year='".$yer."',
fueltype='".$fuel."',
bodycolor='".$bclr."',
feature='Y',
mileage='".$mile."',
askingprice='".$askpr."',
city='".$city."',
other_city='".$ocity."',
fname='".$res->firstname."',
lname='".$res->lastname."',
add1='".$res->address1."',
add2='".$res->address2."',
phone='".$res->phone."',
phone01='".$res->phone01."',
phone02='".$res->phone02."',
mobile='".$res->mobile."',
mobile01='".$res->mobile01."',
mobile02='".$res->mobile02."',
email='".$res->email."',
zip='".$res->zip."',
uid='".$res->id."',
posted=now(),
updated=now(),
expireddate = ($expiryDate),
approve='N',
pay_stat='Pending'";
$put_qry = mysql_query($put_sql);
$newrecordid = mysql_insert_id();
// Create Session Vars
$_SESSION['NewVehData']['adid'] = $newrecordid;
$_SESSION["CUSER"]=$res->id;
$_SESSION["CUSER_Username"]=$res->username;
$_SESSION["UsErNaMe"]=$res->firstname." ".$res->lastname;
$_SESSION["UsRemail"] = $res->email;
$_SESSION["UsErFNaMe"] = $res->firstname;
$_SESSION["UsErLNaMe"] = $res->lastname;
$_SESSION["userlastlog"] = $res->lastlogintime;
// Redirect to Payment Gateway
//redirect(MEMBERS."/redir_ccav.php");
echo $sql;
echo "<br><br>";
echo $put_sql;
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
}
?>
Please help!
Keyur