Good evening,
I am working on a form processor that determines which form was sent to it processes and emails / sometimes needs to return results to my jquery for further client side processing. The issue i am running into is that the first time the script is run it returns the array, but any subsequent runs return no data.
The PHP
switch ($form){
case 'cta':
echo json_encode(array('callback' => 'cta'));
//echo"";
break;
case "questionaire":
echo json_encode(array('callback' => 'quetionaire'));
break;
case "contact":
echo json_encode(array('callback' => 'contact'));
}
The Jquery
function doEmail(form){
$.ajax({
data: $(form).serialize(),
type: 'POST',
url: 'core/process.php',
dataType: 'json',
success: function(data){
$(form +' input, textarea').each(function(){$(this).val("") })
$('#lightbox').fadeOut(600)
$('#responder').fadeOut(600)
alert('form name '+ data.callback);
}
})
}
function inputProcessor(event, form){
var $name = $(form + ' input#name').val();
var $phone = $(form + ' input#phone').val();
var $email = $(form + ' input#email').val();
var $msg = $(form + ' textarea#msg').val();
var $i = 0;
var $i1 = 0;
$(form +' [req=yes]').each(function() {
console.log('step 1')
$i++;
$(this).css('background-color', '');
if(!$(this).val().length) {
event.preventDefault();
$(this).css('background-color', '#ffd7d7');
}
});
$(form +' [req=yes]').each(function() {
console.log('step 2')
if($(this).val().length > 0){
event.preventDefault();
$i1++;
}
});
//if($i1 === $i){
var result = regex.test($email)
console.log('step 3')
if (result === true){
event.preventDefault();
console.log('Testing Email')
doEmail(form);
}else{
$(form +' input#email').css('background-color', '#ffd7d7');
}
//}
}
It should also be noted that this site never refreshes all the content resides in hidden divs that are revealed when the "page" link is clicked.
Thanks in advance
Edit "I actually have to exit the browser and restart it in order to get a new array"
Edit 2
1 of 3 Forms
<form id="cta" method="post">
<input type="text" req="yes" id="name" class="input" name="name" placeholder="Full Name"/><br>
<input type="text" req="yes" id="phone" class="input" name="phone" placeholder="Phone Number"/><br>
<input type="text" req="yes" id="email" class="input" name="email" placeholder="Email Address"/><br>
<input type="hidden" name="form" value="cta">
<span id="submit_btn">Submit entry</span>
</form>
2 of 3 Forms
<form id="contact_form" class=contact_form method="post">
<input type="text" req="yes" id="name" class="input3" name="name" placeholder="Name"/>
<br> <br>
<input type="text" req="yes" id="phone" class="input3" name="phone" placeholder="Phone"/>
<br><br>
<input type="text" req="yes" id="email" class="input3" name="email" placeholder="Email"/>
<br><br>
<textarea id="msg" name="msg" placeholder="Type your message here" class="input3" rows="4"></textarea><br>
<input type="hidden" name="form" value="contact">
<span class="contact_button" id="SubmitContact"><div id="contact_submit">Send message</div></span>
</form>
3 of 3 forms
<form id="response" class=form1 method="post">
<input type="text" req="yes" id="name" class="input2" name="name" placeholder="Name"/>
<br>
<input type="text" req="yes" id="phone" class="input2" name="phone" placeholder="Phone"/>
<br>
<input type="text" req="yes" id="email" class="input2" name="email" placeholder="Email"/>
<br>
<textarea id="msg" name="msg" placeholder="Type your message here" class="input2" rows="4"></textarea><br>
<br>
<input type="hidden" name="form" value="questionaire">
<span class="submit_button" id="Submit"><div id="submit">Send message</div></span>
</form>
Ok so i answered my own question after getting a night of sleep and realizing that i needed to unset the form variable.
var result = regex.test($email)
console.log('step 3')
if (result === true){
event.preventDefault();
console.log('Testing Email')
formId = $(form +' input#formId').val();
if(formId === 'cta'){
$('#cta_form').children().fadeOut(600)
window.location.href = 'docs/medical-alert.pdf';
}
doEmail(form, formId);
}else{
$(form +' input#email').css('background-color', '#ffd7d7');
}
$(form).remove()