Jump to content

Need assistance in debugging why form isn't submitting


Recommended Posts

I am trying to figure out why a form isn't submitting into a MySQL database. I am using JWuery in between but that isn't the problem. I have combed over the code several times, but cannot see any errors. Any suggestions on debugging? There are quite a few form fields. Here is the code.

 

[Form code]


<form id="ContactForm" action="">

<p>
<label>Title: <span>(optional)</span></label>
<input id="Title" name="Title_t" maxlength="120" type="text" autocomplete="off"/>

</p>
<p>
<label>First Name:<span class="red"><sup>*</sup></label>
<input id="FName" name="FName_t" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>
<span class="error" style="display:none;"></span>
</p>
<p>
<label>Last Name:<span class="red"><sup>*</sup></label>
<input id="LName" name="LName_t" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>
<span class="error" style="display:none;"></span>
</p>
<p>
<label>Name of Organization:<span class="red"><sup>*</sup></label>
<input id="OrgName" name="OrgName_t" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>
<span class="error" style="display:none;"></span>
</p>
<p>
<label>Name of Event:<span class="red"><sup>*</sup></label>
<input id="EventName" name="EventName_t" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>
<span class="error" style="display:none;"></span>
</p>

<p>
<label>Address:<span class="red"><sup>*</sup></label>
<input id="Address1" name="Address_Name_t" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>
<span class="error" style="display:none;"></span>
</p>

<p>
<label>Address: <span>(optional)</span></label>
<input id="Address2" name="Address_Name2_t" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>

</p>

<p>
<label>City:<span class="red"><sup>*</sup></label>
<input id="City" name="City_t" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>
<span class="error" style="display:none;"></span>
</p>

<p>
<label>State:<span class="red"><sup>*</sup></label>
<input id="State" name="State_t" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>
<span class="error" style="display:none;"></span>
</p>

<p>
<label>Zip:<span class="red"><sup>*</sup></label>
<input id="Zip" name="Zip_n" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>
<span class="error" style="display:none;"></span>
</p>



<p>
<label>Fax:<span>optional</span></label>
<input id="Fax" name="Fax_n" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>

</p>
<p>
<label>Start Date/Start Time:<span class="red"><sup>*</sup></label>
<input id="StartDate" name="StartDate_d" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>

</p>
<p>
<label>End Date/End Time:<span class="red"><sup>*</sup></label>
<input id="EndDate" name="EndDate_d" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>

</p>
<p>
<label>Cost of Event: <span>(optional)</span></label>
<input id="Cost" name="Cost_n" maxlength="120" type="text" autocomplete="off"/>

</p>

<p>
<label>Website:<span>(optional)</span></label>
<input id="Website" name="Website_t" class="inplaceError" maxlength="120" type="text" autocomplete="off"/>
</p>

<p>
<label>Description of Organization:<span class="red"><sup>*</sup><br /> <span>300 characters allowed</span></label>
<textarea id="Description" name="Description_t" class="inplaceError" cols="6" rows="5" autocomplete="off"></textarea>
<span class="error" style="display:none;"></span>
</p>

<p class="submit">
<input id="send" type="button" value="Submit"/>
<span id="loader" class="loader" style="display:none;"></span>
<span id="success_message" class="success"></span>
</p>
<input id="newcontact" name="newcontact" type="hidden" value="1"></input>
<p align="left"><span class="red"><sup>*</sup></span> Indicates a required field</p>
</form>

 

 

Submit Code


<?php
require_once("db.php"); /* Database Class */
//require_once('utils/is_email.php'); /* Email Validation Script */

/* Handle Ajax Request */
if(isset($_POST['newcontact'])){
$contact = new Contact();
unset($contact);
}
else{
header('Location: /');
}

/* Class Contact */
class Contact{

private $db; /* the database obj */

private $errors = array(); /* holds error messages */
private $num_errors; /* number of errors in submitted form */

public function __construct(){
$this->db = new DB();
if(isset($_POST['newcontact']))
$this->processNewMessage();
else
header("Location: /");
}

public function processNewMessage(){

$Title = $_POST['Title_t'];
$FName = $_POST['FName_t'];
$LName = $_POST['LName_t'];
$OrgName = $_POST['OrgName_t'];
$EventName = $_POST['EventName_t'];
$AddressName = $_POST['Address_Name_t'];
$AddressName2 = $_POST['Address_Name2_t'];
$City = $_POST['City_t'];
$State = $_POST['State_t'];
$Zip = $_POST['Zip_n'];
$StartDate = $_POST['StartDate_d'];
$EndDate = $_POST['EndDate_d'];
$Website = $_POST['Website_t'];
$Cost = $POST ['Cost_n'];
$Description = $POST['Description_t'];

/* Server Side Data Validation */

/* Email Validation */
//if(!$email || mb_strlen($email = trim($email)) == 0)
//$this->setError('email','required field');
//else{
//if(!is_email($email))
//$this->setError('email', 'invalid email');
//else if(mb_strlen($email) > 120)
//$this->setError('email', 'too long! 120');
//}

/* First Name Validation */
if(!$FName || mb_strlen($FName = trim($FName)) == 0)
$this->setError('FName_t', 'required field');
else if(mb_strlen(trim($FName)) > 120)
$this->setError('FName_t', 'too long! 120 characters');

/* Last Name Validation */
if(!$LName || mb_strlen($LName = trim($LName)) == 0)
$this->setError('LName_t', 'required field');
else if(mb_strlen(trim($LName)) > 120)
$this->setError('LName_t', 'too long! 120 characters');

/* Organization Validation */
if(!$OrgName || mb_strlen($OrgName = trim($OrgName)) == 0)
$this->setError('OrgName_t', 'required field');
else if(mb_strlen(trim($OrgName)) > 120)
$this->setError('OrgName_t', 'too long! 120 characters');

/* Organization Validation */
if(!$EventName || mb_strlen($EventName = trim($EventName)) == 0)
$this->setError('EventName_t', 'required field');
else if(mb_strlen(trim($EventName)) > 120)
$this->setError('EventName_t', 'too long! 120 characters');

/* Address Validation */
if(!$AddressName || mb_strlen($AddressName = trim($AddressName)) == 0)
$this->setError('Address_Name_t', 'required field');
else if(mb_strlen(trim($AddressName)) > 120)
$this->setError('Address_Name_t', 'too long! 120 characters');

/* City Validation */
if(!$City || mb_strlen($City = trim($City)) == 0)
$this->setError('City_t', 'required field');
else if(mb_strlen(trim($City)) > 120)
$this->setError('City_t', 'too long! 120 characters');

/* State Validation */
if(!$State || mb_strlen($State = trim($State)) == 0)
$this->setError('State_t', 'required field');
else if(mb_strlen(trim($State)) > 12)
$this->setError('State_t', 'too long! 12 characters');

/* Zip Validation */
if(!$Zip || mb_strlen($Zip = trim($Zip)) == 0)
$this->setError('Zip_n', 'required field');
else if(mb_strlen(trim($Zip)) > 12)
$this->setError('Zip_n', 'too long! 12 characters');


/* Start Date Validation */
if(!$StartDate || mb_strlen($StartDate = trim($StartDate)) == 0)
$this->setError('StarteDate_d', 'required field');
else if(mb_strlen(trim($StartDate)) > 12)
$this->setError('StartDate_d', 'too long! 12 characters');

/* End Time Validation */
if(!$EndDate || mb_strlen($EndDate = trim($EndDate)) == 0)
$this->setError('EndDate_d', 'required field');
else if(mb_strlen(trim($EndDate)) > 12)
$this->setError('EndDate_d', 'too long! 12 characters');


/* Description Validation */
$Description = trim($Description);
if(!$Description || mb_strlen($Description = trim($Description)) == 0)
$this->setError('Description_t','required field');
elseif(mb_strlen($Description) > 300)
$this->setError('Description_t', 'too long! 300 characters');

/* Errors exist */
if($this->countErrors() > 0){
$json = array(
'result' => -1,
'errors' => array(
array('name' => 'Title_t' ,'value' => $this->error_value('Title_t')),
array('name' => 'FName_t' ,'value' => $this->error_value('FName_t')),
array('name' => 'LName_t' ,'value' => $this->error_value('LName_t')),
array('name' => 'OrgName_t' ,'value' => $this->error_value('OrgName_t')),
array('name' => 'EventName_t' ,'value' => $this->error_value('EventName_t')),
array('name' => 'Address_Name_t' ,'value' => $this->error_value('Address_Name_t')),
array('name' => 'Address_Name2_t' ,'value' => $this->error_value('Address_Name2_t')),
array('name' => 'City_t' ,'value' => $this->error_value('City_t')),
array('name' => 'State_t' ,'value' => $this->error_value('State_t')),
array('name' => 'Zip_n' ,'value' => $this->error_value('Zip_n')),
array('name' => 'StartDate_d' ,'value' => $this->error_value('StartDate_d')),
array('name' => 'EndDate_d' ,'value' => $this->error_value('EndDate_d')),
array('name' => 'Website_t' ,'value' => $this->error_value('Website_t')),
array('name' => 'Cost_n' ,'value' => $this->error_value('Cost_n')),
array('name' => 'Description_t' ,'value' => $this->error_value('Description_t')),
)
);
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
}
/* No errors, insert in db*/
else{
if(($ret = $this->db->dbNewMessage($Title)) > 0){
$json = array('result' => 1);
if(SEND_EMAIL)
$this->sendEmail($Title);
}
else
$json = array('result' => -2); /* something went wrong in database insertion */
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
}
}

public function sendEmail($FName,$LName,$EventName,$OrgName,$Description){
/* Just format the email text the way you want ... */
$message_body = "Hi, ".$FName."(".$OrgName." - ".$EventName.") sent you a message from everyeveryevery.com\n"

."Description: "."\n"
.$Description_t;
$headers = "From: ".EMAIL_FROM_NAME." <".EMAIL_FROM_ADDR.">";

return mail(EMAIL_TO,MESSAGE_SUBJECT,$message_body,$headers);
}

public function setError($field, $errmsg){
$this->errors[$field] = $errmsg;
$this->num_errors = count($this->errors);
}

public function error_value($field){
if(array_key_exists($field,$this->errors))
return $this->errors[$field];
else
return '';
}

public function countErrors(){
return $this->num_errors;
}
};
?>

 

Submit to Database


<?php
require_once("config.php"); /* Configuration File */

class DB{

private $link;

public function __construct(){
$this->link = mysqli_connect(DB_SERVER, DB_USER, DB_PASS,DB_NAME);
if (mysqli_connect_errno())
exit();
}

public function __destruct() {
mysqli_close($this->link);
}

public function dbNewMessage($Title,$FName,$LName,$OrgName,$EventName,$AddressName,$AddressName2,$City,$State,$Zip,$StartDate,$EndDate,$Website,$Cost,$Description){
$Title = mysqli_real_escape_string($this->link,$Title);
$FName = mysqli_real_escape_string($this->link,$FName);
$LName = mysqli_real_escape_string($this->link,$LName);
$OrgName = mysqli_real_escape_string($this->link,$OrgName);
$EventName = mysqli_real_escape_string($this->link,$EventName);
$AddressName = mysqli_real_escape_string($this->link,$AddressName);
$AddressName2 = mysqli_real_escape_string($this->link,$AddressName2);
$City = mysqli_real_escape_string($this->link,$City);
$State = mysqli_real_escape_string($this->link,$State);
$Zip = mysqli_real_escape_string($this->link,$Zip);
$StartDate = mysqli_real_escape_string($this->link,$StartDate);
$EndDate = mysqli_real_escape_string($this->link,$EndDate);

$Website = mysqli_real_escape_string($this->link,$Website);
$Cost = mysqli_real_escape_string($this->link,$Cost);
$Description= mysqli_real_escape_string($this->link,$Description);



mysqli_autocommit($this->link,FALSE);

$query = "INSERT INTO Events(ID,Admin_n,Title_t,FName_t,LName_t,OrgName_t,EventName_t,Address_Name_t,Address_Name2_t,City_t,State_t,Zip_n,StartDate_d,EndDate_d,Website_t,Cost_n,Description_t)
VALUES('NULL','0','$Title','$FName','$LName','$OrgName','$EventName','$AddressName','$AddressName2','$City','$State','$Zip','$StartDate','$EndDate','$Website','$Cost','$Description')";
mysqli_query($this->link,$query);

if(mysqli_errno($this->link))
return -1;
else{
mysqli_commit($this->link);
return 1;

}
}
};
?>

 

MySQL

 



[color=#4f76ac]CREATE[/color] [color=#4f76ac]TABLE[/color] [color=#4f76ac]IF[/color] [color=#4f76ac]NOT[/color] [color=#4f76ac]EXISTS[/color] `Events` (
`ID` [color=#4f76ac]int[/color](3) [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color] AUTO_INCREMENT,
`Admin_n` [color=#4f76ac]int[/color](12) [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`Title_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`FName_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`LName_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`OrgName_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`EventName_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`Address_Name_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`Address_Name2_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`City_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`State_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`Zip_n` [color=#4f76ac]int[/color](7) [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`StartDate_d` [color=#4f76ac]datetime[/color] [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`EndDate_d` [color=#4f76ac]datetime[/color] [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`Website_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`Cost_n` [color=#4f76ac]bigint[/color](20) [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`Description_t` text [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color],
`Date_Added` timestamp [color=#4f76ac]NOT[/color] [color=#4f76ac]NULL[/color] [color=#4f76ac]DEFAULT[/color] [color=#4f76ac]CURRENT_TIMESTAMP[/color] [color=#4f76ac]ON[/color] [color=#4f76ac]UPDATE[/color] [color=#4f76ac]CURRENT_TIMESTAMP[/color],
[color=#4f76ac]PRIMARY[/color] [color=#4f76ac]KEY[/color] (`ID`),
FULLTEXT [color=#4f76ac]KEY[/color] `Title_t` (`Title_t`),
FULLTEXT [color=#4f76ac]KEY[/color] `FName_t` (`FName_t`),
FULLTEXT [color=#4f76ac]KEY[/color] `LName_t` (`LName_t`),
FULLTEXT [color=#4f76ac]KEY[/color] `OrgName_t` (`OrgName_t`),
FULLTEXT [color=#4f76ac]KEY[/color] `EventName_t` (`EventName_t`),
FULLTEXT [color=#4f76ac]KEY[/color] `Address_Name_t` (`Address_Name_t`),
FULLTEXT [color=#4f76ac]KEY[/color] `Address_Name2_t` (`Address_Name2_t`),
FULLTEXT [color=#4f76ac]KEY[/color] `City_t` (`City_t`),
FULLTEXT [color=#4f76ac]KEY[/color] `State_t` (`State_t`),
FULLTEXT [color=#4f76ac]KEY[/color] `Website_t` (`Website_t`),
FULLTEXT [color=#4f76ac]KEY[/color] `Description_t` (`Description_t`)
) ENGINE=MyISAM [color=#4f76ac]DEFAULT[/color] CHARSET=latin1 AUTO_INCREMENT=7 ;

 

JQuery



$(document).ready(function() {
contact.initEventHandlers();
});
var contact = {
initEventHandlers : function() {
/* clicking the submit form */
$('#send').bind('click',function(event){
$('#loader').show();
setTimeout('contact.ContactFormSubmit()',500);
});
/* remove messages when user wants to correct (focus on the input) */
$('.inplaceError',$('#ContactForm')).bind('focus',function(){
var $this = $(this);
var $error_elem = $this.next();
if($error_elem.length)
$error_elem.fadeOut(function(){$(this).empty()});
$('#success_message').empty();
});
/* user presses enter - submits form */
$('#ContactForm input,#ContactForm textarea').keypress(function (e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
$("#send").click();
return false;
}
else
return true;
});
},
ContactFormSubmit : function() {
$.ajax({
type : 'POST',
url : 'PHP/contact.php?ts='+new Date().getTime(),
dataType : 'json',
data : $('#ContactForm').serialize(),
success : function(data,textStatus){
//hide the ajax loader
$('#loader').hide();
if(data.result == '1'){
//show success message
$('#success_message').empty().html('Message sent');
//reset all form fields
$('#ContactForm')[0].reset();
//envelope animation
$('#envelope').stop().show().animate({'marginTop':'-175px','marginLeft':'-246px','width':'492px','height':'350px','opacity':'0'},function(){
$(this).css({'width':'246px','height':'175px','margin-left':'-123px','margin-top':'-88px','opacity':'1','display':'none'});
});
}
else if(data.result == '-1'){
for(var i=0; i < data.errors.length; ++i ){
if(data.errors[i].value!='')
$("#"+data.errors[i].name).next().html('<span>'+data.errors[i].value+'</span>').fadeIn();
}
}
},
error : function(data,textStatus){}
});
}
};

That's really not how you should use objects. At all. In fact, what you've done is pretty much the opposite of OOP, which is far, far more than just shoving code in a class.

 

As far as debugging ideas go, 'not submitting' is a bit vague. Are you certain the form is posting? Are you certain your validation is working? Are you certain legit data is getting to your db create/insert code?

 

print_r and exit at logical points (just after when you should get the form, before and after individual validation steps, before your queries). See if what you expect is what's happening.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.