Everything works perfectly as is with the following files but in process.php
if I echo the commented out messages after if (mysql_query($sql))
, then form.php
page goes to process.php
page which is prevented in my_script.js
- in other words some jQuery features from my_script.js
don't work no more. Sometimes reverting back to the original echo lines still does not bring back the jQuery features until I erase and recreate the process.php
file.
This is my first complete form, but what's happening is kinda spooky...am I missing something somewhere?
form.php
<?php
//filename - form.php
?>
<html>
<head><title>Registration Form: jQuery</title></head>
<body>
<form id="myForm" action="process.php" method="POST">
Username: <input type="text" name="username"/><br />
Password: <input type="password" name="pass"/><br />
First Name: <input type="text" name="fname"/><br />
Last Name: <input type="text" name="lname"/><br />
<button id="submit">register</button>
</form>
<div id="ack"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
<script type="text/javascript" src="script/my_script.js"></script>
</body>
</html>
db.php
<?php
//filename - db.php
// mysql_connect for process.php -
$conn = mysql_connect('myhost.mysql.com', 'formdb_user', 'pwd');
$db = mysql_select_db('formdb');
?>
process.php
<?php
//filename - process.php
//capture errors
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
//initialize
//global $message;
//connect to formtable
include_once ('db.php');
//tried this file with mysqli method and parameters and it didin't solve the problem!
$username = mysql_real_escape_string ($_POST['username']);
$password = mysql_real_escape_string (md5($_POST['pass']));
$fname = mysql_real_escape_string ($_POST['fname']);
$lname = mysql_real_escape_string ($_POST['lname']);
$sql = "INSERT INTO `formtable` (`username`, `password`, `fname`, `lname`) VALUES ('$username', '$password', '$fname', '$lname')";
if (mysql_query($sql))
echo "Inserted Successfully";
//echo "Thank You! We'll Review Your Application.";
else
echo "Insertion Failed";
//echo "Submit Failed - Please Refresh Page And Try Again.";
//this method didn't help either - echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
?>
my_script.js
$("#submit").click( function() {
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info) {
$("#ack").empty();
$("#ack").html(info);
clear();
});
$("#myForm").submit( function() {
return false;
});
});
function clear() {
$("#myForm :input").each( function() {
$(this).val("");
});
}
EDIT
Everything works w/o any problem with the following:
if (mysql_query($sql))
echo "Inserted Successfully";
//echo "Thank You! We'll Review Your Application.";
else
echo "Insertion Failed";
//echo "Submit Failed - Please Refresh Page And Try Again.";
Problem starts when I use the following. Form data gets inserted to table but I get the success message on another page - the form's action page, process.php, which per the code in jQuery file, my_script.js, should not happen.
if (mysql_query($sql))
//echo "Inserted Successfully";
echo "Thank You! We'll Review Your Application.";
else
//echo "Insertion Failed";
echo "Submit Failed - Please Refresh Page And Try Again.";
Further when I try to reset back to the previous mode, sometimes things do not start working back correctly right away, takes a few minutes...