Form submit using js/AJAX - validation problem

Discussion in 'JavaScript' started by EvinDesign, Mar 24, 2008.

  1. #1
    Hi,

    I have created a simple ajax validation for my form,

    What I´m trying to do is to let the HTML file validate against the PHP using the AJAX/JS, this validation works fine now (even tested with CAPTCHA).

    But my problem is that I have to press the submit button twice when all the validation rules have passed. The problem lies in the variable arden, it is only when arden is set to "ja" when the function kolla() returns true and the form gets sent.

    I attached all of the code which probably is unnecessary but maybe it can give you a better overview,

    I´m sure this one is easy.. anyone?


    the HTML:

    <form action="" onsubmit="return kolla();" method="post">

    the PHP validation file:
    $yt = $_GET['ytKod'];

    echo "vilken.push('namn'); \n";

    if ($yt == ''){
    echo "gg = '1'; \n";
    echo "obj.push('Skriv ett namn'); \n";
    }else{
    echo "obj.push('tack'); \n";

    }


    the JS:
    var vilken = new Array();
    var obj = new Array();
    var getten;
    var ajax = new sack();
    var arden = new String;

    function valid(sel)
    {
    //var branschKod = document.getElementById(sel).value;
    ajax.requestFile = 'valid.php?'+sel; // Specifying which file to get
    ajax.onCompletion = skapaYrken; // Specify function that will be executed after file has been found
    ajax.runAJAX(); // Execute AJAX function


    }


    function skapaYrken()
    {
    var gg = null;
    var x;

    eval(ajax.response); // Executing the response from Ajax as Javascript code

    for (x in vilken){
    var divven = document.getElementById('errorsDiv_'+vilken[x]);
    divven.innerHTML = obj[x];
    }
    if (gg == null){
    arden = "ja"
    }else {
    arden = "nej";
    }
    }




    function kolla(){



    var namn = document.getElementById('namn').value;
    var arende = document.getElementById('arende').value;
    var mess = document.getElementById('mess').value;
    var security_code = document.getElementById('security_code').value;
    getten = 'ytKod='+namn
    +'&ytKid='+arende
    +'&ytKed='+mess
    +'&security_code='+security_code;

    valid(getten);

    if(arden == "ja"){
    return true;
    }else {
    return false;
    }

    }
     
    EvinDesign, Mar 24, 2008 IP
  2. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello, try this out.

    if (gg == null){
    [B]arden = "ja"[/B]
    }else {
    arden = "nej";
    }
    Code (markup):
    Replace the bold portion with
    arden = 'ja';
    Code (markup):
     
    -NB-, Mar 25, 2008 IP
  3. EvinDesign

    EvinDesign Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for trying but that didn´t help..

    Please guys, anyone? any suggestions?

    See the problem really is that the script checks for a variable arden that should be set to "ja" if something happens.. right?

    It does get set, the problem is that the script recognizes the arden var only the second time the user clicks the button (runs the script)
     
    EvinDesign, Mar 25, 2008 IP