I have a form with radios and trying to validate starting with jq/$.ajax and then posting info in php.
form.php
<tr>
<td class="h"><span class="txt_wht">t1</span></td>
<input type="hidden" name="gm[0]" value="t1" />
<td class="vs"><span class="txt_yel_sm">vs</span></td>
<td class="a"><span class="txt_wht">t2</span></td>
<input type="hidden" name="gm[1]" value="t2" />
<td><input type="radio" name="g1" value="a" /></td>
<td><input type="radio" name="g1" value="b" /></td>
<td><input type="radio" name="g1" value="c" /></td>
</tr>
<tr>
<td class="h"><span class="txt_wht">t1</span></td>
<input type="hidden" name="gm[0]" value="t1" />
<td class="vs"><span class="txt_yel_sm">vs</span></td>
<td class="a"><span class="txt_wht">t2</span></td>
<input type="hidden" name="gm[1]" value="t2" />
<td><input type="radio" name="g2" value="a" /></td>
<td><input type="radio" name="g2" value="b" /></td>
<td><input type="radio" name="g2" value="c" /></td>
</tr>
validade.js
$(document).ready(function () {
//global vars
form.submit(function () {
if (validate1() && validate2()) {
//vars
var exst_email = $('#exst_email').attr('value');
var g1 = $("input[name='g1']:checked").attr('value');
//more vars
$.ajax({
type: "post",
url: "scripts/sbmt.php",
data: "exst_email=" + exst_email,
success: function (exst_rsp) {
//if its not exist
if (exst_rsp == "not_exist") {
valInfo.text("Error1!");
valInfo.addClass("error");
email_reg_err = true;
} else {
valInfo.text("");
valInfo.removeClass("error");
email_reg_err = false;
}
//more validation with error msgs
//validate 1 & validate 2 functions
and sbmt.php
session_start();
// connect to db
require_once('conn.php');
if($stmt = $mysqli->prepare('SELECT email FROM table WHERE email=?')){
$stmt->bind_param("s", $_POST['exst_email']);
$stmt->execute();
$stmt->store_result();
$numRows = $stmt->num_rows();
if($numRows < 1){
echo 'not_exist';
exit();
}
$stmt->close();
}
//more validation
Now,
Due to Dreamweaver, I know that I don't have typing errors or unclosed tags.
My validate 1 & 2 functions are working fine.
Due to firebug,
a) I know that my data are posted in sbmt.php
b) I get no response back to display the error msg.
I am validating 3 more forms (textareas only if it matters) using the exact same way (differentjs/php files) and they are working fine!
Any suggestion on what may be wrong here?
SOLVED
I don't know why but here's the fix:
I wasn't passing to sbmt.php the <input type="hidden">
values since I didn't use them at the moment.
But I desided to post them as well to see if it does any good. And it worked!
Within you submit function you can try:
form.submit(function(e){
e.preventDefault(); // to prevent the form from submitting itself
// you all validation code
});
I think $_POST['email']
in your PHP
code should be $_POST['exst_email']
, because your posted data in AJAX
contains key as exst_email
here data: "exst_email="+ exst_email,
but not email
.
So, if you PHP
is right then you should send data as data: "email="+ exst_email