Enabling Submit just disables it...
Posted: Sun Jul 05, 2009 12:13 am
Hey guys I'm out of ideas for this one. I hope I'm overlooking something really obvious... I'm using AJAX right now, to validate the uniqueness of a field in a form. If the field isn't unique, an image's source is changed to indicate it is not valid, and if it is unique, it changes to indicate that (that part works just fine). I'm trying to prevent the user from submitting the form if the result is not valid, but that's when I run into this problem. With the code:
and the ajax php file:
and the php file with the form:
When the page loads, the submit key is enabled. As soon as I change the 'calling' field, and the AJAX function is called, the image changes to good or bad, as expected, but the submit button is disabled either way (even if it is valid the first time). I've checked if I had the true/false reversed, but it is disabled both times...
Help!
Code: Select all
//send a data request
function sendRequest(data,output,submit)
{
async.open("GET", "ajax/ajax.php?" + data + "&object=" + output + "&submit=" + submit, true);
async.onreadystatechange = getData;
async.send(null);
}
//get the data sent back
function getData()
{
if(async.readyState == 4 && async.status == 200)
{
var receivedText = async.responseText; //data as a string
if(receivedText .indexOf('|' != -1))
{
element = receivedText.split('|');
document.getElementById(element[0]).src = element[1]; //set the image source as good or bad
document.getElementById(element[2]).disabled = element[3]; //enable/disable the submit button
}
}
}Code: Select all
$valid = 0;
$extra = "";
if(strlen($_GET["ignore"]) > 0)
{
$extra = " and id != '".$_GET["ignore"]."'";
}
$find = mysql_query("SELECT * from ".$_GET["table"]." where ".$_GET["field"]."='".$_GET["value"]."'".$extra);
if(mysql_num_rows($find) > 0)
$valid = 0;
else
{
//CHECK IT IS VALID
if(strpos($_GET["value"], " ") === false)
$valid = 1;
}
if($valid == "1")
{
echo $_GET["object"]."|icon/good.gif|".$_GET["submit"]."|false"; //unique - good
}
else
{
echo $_GET["object"]."|icon/alert_high.gif|".$_GET["submit"]."|true"; //invalid characters
}
Code: Select all
<tr><td>Calling Name: </td>
<script type="text/javascript" src="ajax/basic.js"></script>
<input type="text" name="calling" id="calling" value="<? echo $out_calling; ?>" onKeyUp="sendRequest('existing&table=pageindex&ignore=<? echo $theid; ?>&field=calling&value=' + this.value, 'callingoutput', 'submit');"> <img src="icon/hidden.gif" id="callingoutput"></td></tr>
<tr><td> </td></tr>
<tr><td colspan=2 style="text-align: right;"><input type="submit" name="submit" id="submit" value="Submit"></td></tr>Help!