diff --git a/contact_add_edit.php b/contact_add_edit.php
index 72cc92f..fc65124 100644
--- a/contact_add_edit.php
+++ b/contact_add_edit.php
@@ -35,13 +35,10 @@ default:
$page_shop_log = PAGE_SHOP_LOG . "?shop_id=$shop_id";
+// setup the proper form action and form values .. not that $_GET is such a brilliant approach :)
if($_GET['contact_id'] == 'new_contact'){
- /* Discover if previous contact creation attempt was abandoned
- There should be at least a first and last name, if not we use
- previous contact_id, update it and start fresh
- */
mysql_select_db($database_YBDB, $YBDB);
// Find previous contact_id
@@ -50,35 +47,10 @@ if($_GET['contact_id'] == 'new_contact'){
$result = mysql_fetch_assoc($query);
$previous_contact_id = $result['previous_contact_id'];
- // If full_name is empty we will use this contact_id
- $sql = "SELECT CONCAT(first_name, ' ', last_name) as full_name FROM contacts WHERE contact_id=" . $previous_contact_id. ";";
- $query = mysql_query($sql, $YBDB) or die(mysql_error());
- $result = mysql_fetch_assoc($query);
-
- $full_name = $result['full_name'];
- //adds contact if new_contact is selected .. it's " " not ""
- if ($full_name != " ") {
-
- $new_contact_id = $previous_contact_id + 1;
-
- $insertSQL = sprintf("INSERT INTO contacts (date_created) VALUES (%s)",
- GetSQLValueString('current_time', "date"));
- $Result1 = mysql_query($insertSQL, $YBDB) or die(mysql_error());
-
- $contact_id = $new_contact_id;
- $contact_id_entry = 'new_contact';
-
- } else {
-
- $insertSQL = sprintf("UPDATE contacts SET date_created=%s WHERE contact_id=" . $previous_contact_id,
- GetSQLValueString('current_time', "date"));
- $Result1 = mysql_query($insertSQL, $YBDB) or die(mysql_error());
-
- $contact_id = $previous_contact_id;
- $contact_id_entry = 'new_contact';
-
- }
+ $new_contact_id = $previous_contact_id + 1;
+ $contact_id = $new_contact_id;
+ $contact_id_entry = 'new_contact';
} elseif(isset($_GET['contact_id'])) {
@@ -90,27 +62,97 @@ if($_GET['contact_id'] == 'new_contact'){
$contact_id_entry = -1;
}
-$editFormAction = "?contact_id={$contact_id}&shop_id={$shop_id}";
+$editFormAction = "?contact_id={$contact_id}&shop_id={$shop_id}";
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
- $updateSQL = sprintf("UPDATE contacts SET first_name=%s, middle_initial=%s, last_name=%s, email=%s,
- DOB=%s, phone=%s, address1=%s, address2=%s, city=%s,
- `state`=%s, zip=%s, pass=ENCODE(%s,'yblcatx') WHERE contact_id=%s",
- GetSQLValueString($_POST['first_name'], "text"),
- GetSQLValueString($_POST['middle_initial'], "text"),
- GetSQLValueString($_POST['last_name'], "text"),
- GetSQLValueString($_POST['email'], "text"),
- GetSQLValueString($_POST['DOB'], "date"),
- GetSQLValueString($_POST['phone'], "text"),
- GetSQLValueString($_POST['address1'], "text"),
- GetSQLValueString($_POST['address2'], "text"),
- GetSQLValueString($_POST['city'], "text"),
- GetSQLValueString($_POST['state'], "text"),
- GetSQLValueString($_POST['zip'], "text"),
- GetSQLValueString($_POST['password'], "text"),
- GetSQLValueString($_POST['contact_id'], "int"));
+
+ /* Discover if submitted contact creation attempt is new.
+ There should be at least a first and last name.
+ */
+ mysql_select_db($database_YBDB, $YBDB);
+
+ $query = 'SELECT MAX(contact_id) as contact_id FROM contacts;';
+ $sql = mysql_query($query, $YBDB) or die(mysql_error());
+ $result = mysql_fetch_assoc($sql);
+ $submitted_contact_id = $result['contact_id'] + 1;
+
+ /*
+ $handler->debug("submitted_contact_id",$submitted_contact_id - 1);
+ $handler->debug("$_POST",$_POST['contact_id']);
+ exit();
+ */
+
+ // contact already exists it is less than $submitted_contact_id
+ if($submitted_contact_id > $_POST['contact_id']) {
+ $submitted_contact_id = $_POST['contact_id'];
+ }
+
+ // if contact already exists, $submitted_contact_id now equals $_POST['contact_id], and it isn't new_contact
+ if ($submitted_contact_id != $_POST['contact_id'] || $_POST === 'new_contact') {
+ $submitted_contact_id = $_POST['contact_id'];
+ } else {
+
+ // If full_name is empty we will use this contact_id
+ $sql = "SELECT CONCAT(first_name, ' ', last_name) as full_name FROM contacts WHERE contact_id=" . $submitted_contact_id . ";";
+ $query = mysql_query($sql, $YBDB) or die(mysql_error());
+ $result = mysql_fetch_assoc($query);
+ $full_name = $result['full_name'];
+ }
+
+ //adds contact if new_contact is selected .. it's " " not ""
+ if (empty($full_name)) {
+ $contact_id_entry = 'new_contact';
+ }
+
+ if ( $contact_id_entry === 'new_contact' ) {
+
+ // Get the actual contact_id because it may have changed on multiple terminals
+ $query = 'SELECT MAX(contact_id) as contact_id FROM contacts;';
+ $sql = mysql_query($query, $YBDB) or die(mysql_error());
+ $result = mysql_fetch_assoc($sql);
+ $submitted_contact_id = $result['contact_id'] + 1;
+
+ // Insert new contact information into a new record
+ $updateSQL = 'INSERT INTO contacts (contact_id, first_name, middle_initial, last_name, email,' .
+ ' phone, address1, address2, city, state, DOB, receive_newsletter, waiver, pass, zip)' .
+ ' VALUES (' .
+ $submitted_contact_id . ', ' .
+ '"' . $_POST['first_name'] . '", ' .
+ '"' . $_POST['middle_initial'] . '", ' .
+ '"' . $_POST['last_name'] . '", ' .
+ '"' . $_POST['email'] . '", ' .
+ '"' . $_POST['phone'] . '", ' .
+ '"' . $_POST['address1'] . '", ' .
+ '"' . $_POST['address2'] . '", ' .
+ '"' . $_POST['city'] . '", ' .
+ '"' . $_POST['state'] . '", ' .
+ '"' . $_POST['DOB'] . '", ' .
+ '"' . $_POST['email_list'] . '", ' .
+ 1 . ', ' .
+ 'ENCODE("' . $_POST['password'] . '",' . '"yblcatx"), ' .
+ '"' . $_POST['zip'] . '");';
+ } else {
+
+ // Update existing contact record
+ $updateSQL = sprintf("UPDATE contacts SET first_name=%s, middle_initial=%s, last_name=%s, email=%s,
+ DOB=%s, phone=%s, address1=%s, address2=%s, city=%s,
+ `state`=%s, zip=%s, pass=ENCODE(%s,'yblcatx') WHERE contact_id=%s",
+ GetSQLValueString($_POST['first_name'], "text"),
+ GetSQLValueString($_POST['middle_initial'], "text"),
+ GetSQLValueString($_POST['last_name'], "text"),
+ GetSQLValueString($_POST['email'], "text"),
+ GetSQLValueString($_POST['DOB'], "date"),
+ GetSQLValueString($_POST['phone'], "text"),
+ GetSQLValueString($_POST['address1'], "text"),
+ GetSQLValueString($_POST['address2'], "text"),
+ GetSQLValueString($_POST['city'], "text"),
+ GetSQLValueString($_POST['state'], "text"),
+ GetSQLValueString($_POST['zip'], "text"),
+ GetSQLValueString($_POST['password'], "text"),
+ GetSQLValueString($submitted_contact_id, "int"));
+ }
mysql_select_db($database_YBDB, $YBDB);
$Result1 = mysql_query($updateSQL, $YBDB) or die(mysql_error());
@@ -163,7 +205,7 @@ if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
// Find out if any selections are in the database,
// to decide whether an INSERT or DELETE needs to be done
- $sql = "SELECT selection FROM selections WHERE contact_id=" . $_POST['contact_id'] . ";";
+ $sql = "SELECT selection FROM selections WHERE contact_id=" . $submitted_contact_id . ";";
$query = mysql_query($sql, $YBDB) or die(mysql_error());
$selections = [];
while ($result = mysql_fetch_assoc($query)) {
@@ -174,13 +216,13 @@ if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
if ( is_null($selections[$interest_id]) ) { //INSERT
if( !is_null($interest_checked[$selection]) ) {
$sql = "INSERT INTO selections (contact_id, selection, selection_value)
- VALUES (" . $_POST['contact_id'] . "," . $interest_id . ",1);";
+ VALUES (" . $submitted_contact_id . "," . $interest_id . ",1);";
$result = mysql_query($sql, $YBDB) or die(mysql_error());
}
} else { //DELETE
if( is_null($interest_checked[$selection]) ) {
$sql = "DELETE FROM selections WHERE selection=" . $interest_id .
- " AND contact_id=" . $_POST['contact_id'] . ";";
+ " AND contact_id=" . $submitted_contact_id . ";";
$query = mysql_query($sql, $YBDB) or die(mysql_error());
}
}
@@ -189,11 +231,11 @@ if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
// insert as update? But it works.
if ($_POST['comments']) {
$sql = "INSERT INTO selections (contact_id, selection, selection_value)
- VALUES (" . $_POST['contact_id'] . ", 1,'" . $_POST['comments'] . "');";
+ VALUES (" . $submitted_contact_id . ", 1,'" . $_POST['comments'] . "');";
$result = mysql_query($sql, $YBDB) or die(mysql_error());
}
- if ($_POST['contact_id_entry'] == 'new_contact' || $_POST['contact_id_entry'] == $_POST['contact_id']){
+ if ($_POST['contact_id_entry'] == 'new_contact' || $_POST['contact_id_entry'] == $submitted_contact_id){
//navigate back to shop that it came from
$pagegoto = PAGE_SHOP_LOG . "?shop_id={$shop_id}&new_user_id={$contact_id}";
@@ -201,7 +243,7 @@ if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
}
-}
+} // Submitted
mysql_select_db($database_YBDB, $YBDB);
$query_Recordset1 = "SELECT *, DECODE(pass,'yblcatx') AS passdecode FROM contacts WHERE contact_id = $contact_id";
@@ -222,7 +264,15 @@ $totalRows_Recordset1 = mysql_num_rows($Recordset1);
|
- |
+
+
+ |
|
@@ -293,7 +343,7 @@ $totalRows_Recordset1 = mysql_num_rows($Recordset1);
- I agree
+ I agree
@@ -309,25 +359,33 @@ $totalRows_Recordset1 = mysql_num_rows($Recordset1);
-
+ >
+
diff --git a/js/contact.js b/js/contact.js
index 318823a..7921ad0 100644
--- a/js/contact.js
+++ b/js/contact.js
@@ -126,6 +126,10 @@ $(function(){
if (success === "Success") {
var email_list = $("#email_list_toggle").val();
+
+ // send email_list val to $_POST
+ $("#email_list").val(email_list);
+
var waiver = waiver_checkbox.prop("checked");
if (!email_list) {
email_list = 0;
@@ -137,6 +141,9 @@ $(function(){
} else if (waiver === false) {
waiver = 0;
}
+
+ $.post("json/contact.php", {most_recent_contact_id: 1});
+
// update receive_newsletter and waiver in the database
$.post("json/contact.php", {contact_id: contact_id, email_list: email_list, waiver: waiver });
@@ -253,6 +260,10 @@ $(function(){
// beginning or stored state
$.post("json/contact.php", {contact_id: contact_id, email_list_value: 1 }, function(data) {
+
+ if (data === "") {
+ data = 1;
+ }
$("#email_list_toggle").noUiSlider({
orientation: "horizontal",
diff --git a/json/contact.php b/json/contact.php
index 22003be..41c38d2 100644
--- a/json/contact.php
+++ b/json/contact.php
@@ -7,6 +7,7 @@ $email_list_connector = EMAIL_LIST_CONNECTOR;
$email_list_connector_password = EMAIL_LIST_CONNECTOR_PASSWORD;
$ssl_certificate = SSL_CERTIFICATE;
+
// update waiver
if( isset($_POST['waiver']) ) {
@@ -30,12 +31,11 @@ $ssl_certificate = SSL_CERTIFICATE;
// update email_list
if( isset($_POST['email_list']) ) {
-
+
$email_list = $_POST['email_list'];
$query = "UPDATE contacts SET receive_newsletter=" . $email_list .
" WHERE contact_id=" . $_POST['contact_id'] . ";";
- $result = mysql_query($query, $YBDB) or die(mysql_error());
-
+ $result = mysql_query($query, $YBDB) or die(mysql_error());
}
// return email_list value
@@ -80,5 +80,12 @@ $ssl_certificate = SSL_CERTIFICATE;
}
+ if(isset($_POST['most_recent_contact_id'])) {
+ $query = 'SELECT MAX(contact_id) as contact_id FROM contacts;';
+ $sql = mysql_query($query, $YBDB) or die(mysql_error());
+ $result = mysql_fetch_assoc($sql);
+ echo $result['contact_id'];
+ }
+
?>
\ No newline at end of file