Enabling Submit just disables it...

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Enabling Submit just disables it...

Post by paqman »

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:

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
        }
    }
}
and the ajax php file:

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
    }
 
and the php file with the form:

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');">&nbsp;<img src="icon/hidden.gif" id="callingoutput"></td></tr>
 
<tr><td>&nbsp;</td></tr>
<tr><td colspan=2 style="text-align: right;"><input type="submit" name="submit" id="submit" value="Submit"></td></tr>
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!
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Enabling Submit just disables it...

Post by kaszu »

element = receivedText.split('|');
Split function is returning array of strings, that's why element[3] will be non-empty string, which will make button disabled. Try:

Code: Select all

element = receivedText.split('|');
element[3] = (element[3] == 'true' ? true : false); //convert String to Boolean
//or you can use eval, but that's not a good idea IMO
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Re: Enabling Submit just disables it...

Post by paqman »

...aha of course... it was being set as 'true' or 'false' not true or false. Thanks!!!
Post Reply