0

as said above the require attribute does not work for me , i have searched already a bit but the answers i found here and elsewhere on the web could not help me, would be great if someone could help me

the application is nothing special , just a little thing for me to learn php and jquery, here it is :

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Sig Generator</title> 
    <script type="text/javascript" src ="jquery-2.0.3.min.js"></script>
    <script type="text/javascript" src="sig.js"></script>
</head>
<body>  
    <div id="legalform" align ="center">

            Rechtsform auswählen:<br><br>
            <select id="selection" size="1">        
            </select><br><br>

            <input id="sendLegalForm" type="button" value ="Weiter"/>           
    </div>  

    <div id="fields" align="center">
            <form id="fieldsForm" > 
            <br>
            <br>    
            <input id="sendFields" type="submit" value="Weiter"/>   
            </form>

            <br>

    </div> 

    <div id="display" >


        <textarea  id="textarea" cols="30" rows="20"></textarea>

    </div>

</body>
</html>

the jquery

$(document).ready(function() {
    var $selection ="";

     $.ajax({
         type:'GET',
         url:'http://localhost/php/sig.php?legalforms=true',
         dataType:'json',
         success: function (data){
            $("#display").hide();       
            var selection = [];
            for(var i = 0; i<data.length; i++){
                selection.push('<option>', data[i],'</option>');
            }
            $("#selection").html(selection.join(''));            
            $("#fields").hide();
         },
         error: function(jqXHR,textStatus,errorThrown){  
            console.log(jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
         }
     });

     $("#sendLegalForm").click(function () {    
         selection = $('#selection').val();
         $.ajax({
             type:'GET',
             url:'http://localhost/php/sig.php?selectedLegalform='+ selection,
             dataType:'json',
             success: function (data){  

                 $("#legalform").hide();
                 $("#fields").show();
                 var fieldnames =[];
                 for(property in data.namesToSubmit){
                    fieldnames.push(property);
                 }                   
                 var fields=[];
                 for(var i=0; i<data.textfieldHeaders.length; i++){               
                 fields.push(data.textfieldHeaders[i],'<br>','<input name="',fieldnames[i],'" type="text"',data.namesToSubmit[fieldnames[i]] == "required"?"required":"",'>','<br/>');                                                               
                 }                

                 fields.push("<br>", 'Pflichtfelder (*)');
                 $("#fieldsForm").prepend(fields.join(''));              
             },
             error: function(jqXHR,textStatus,errorThrown){
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
             }
         });
     });

      $("#sendFields").click(function(){     
         $.ajax({
             type:'POST',
             url: 'http://localhost/php/sig.php?fieldsend='+selection,
             data: $("#fieldsForm").serialize(),
             success: function (data){                  
                 $('#fields').hide();
                 $('#display').show();
                 $("#textarea").html(data);
             },
             error: function(jqXHR,textStatus,errorThrown){
                alert('Bitte alle Pflichtfelder ausfüllen.');
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
             }  
        });
        return false;
     });

});

and the php file

<?php

header('Content-type: application/json;  charset=UTF8');
header('Access-Control-Allow-Origin: *');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: 0");   

if (isset($_GET['legalforms'])) {       
    $legalform = array( 'Aktiengesellschaft',
                        'GmbH');

    echo json_encode($legalform);   
}   else if(isset($_GET['selectedLegalform'])) {
        $selection = $_GET['selectedLegalform'];

        $fieldArray = array();

        if ($selection == 'GmbH'){

                $fieldArray['namesToSubmit'] = array(   'companyName',                                                      
                                                        'location',
                                                        'phone',
                                                        'fax',
                                                        'web',
                                                        'registryCourt',
                                                        'registryNumber',
                                                        'ceo'); 

                $fieldArray['textfieldHeaders'] = array('Firmenname und Rechtsform*', 
                                                        'Anschrift*',
                                                        'Telefon',
                                                        'Fax',
                                                        'Webseite',
                                                        'Registergericht*',
                                                        'Registernummer*', 
                                                        'Geschäftsführer*');

        }

        if($selection =='Aktiengesellschaft'){

                $fieldArray['namesToSubmit'] = array(   'companyName'=>'required', 
                                                        'location' =>'required',
                                                        'phone'=>null,
                                                        'fax'=>null,
                                                        'web' => null,
                                                        'registryCourt'=>'required',
                                                        'registryNumber'=>'required',
                                                        'board'=>'required',
                                                        'chairmanboard'=>'required');

                $fieldArray['textfieldHeaders'] = array('Firmenname und Rechtsform*',                                                                                                       'Anschrift*',
                                                        'Telefon',
                                                        'Fax',
                                                        'Webseite',
                                                        'Registergericht*',
                                                        'Registernummer*',
                                                        'Vorstand*',
                                                        'Aufsichtsratsvorstzender*');   
        }   
        echo json_encode($fieldArray);

    } else if ($_GET['fieldsend']) {
        $selection = $_GET['fieldsend'];        

        $signatur = "--\n"; 
        foreach($_POST as $key => $value) {
            if($selection == 'Aktiengesellschaft'){
            // do something..
            }       
        }       
        echo json_encode($signatur);
    }

?>
2
  • 3
    Maybe you are testing in a browser which can't handle the require attribute (e.g. IE9 or lower). Commented Jul 10, 2013 at 11:37
  • i´m using chrome so that should not be the problem , and when i inspect a element in chrome i can see that the html is ok generatet with required at the end , but it still does not work Commented Jul 10, 2013 at 11:41

1 Answer 1

3

You are listening to the click event of a button, and not on the submit event of the form. Thus, no form validation is triggered [see fiddle].

Sign up to request clarification or add additional context in comments.

4 Comments

oh god , thats a remnant of some tests i have done before, how could i not see it..anyway thank you , it works now
just a quick question , now after i input text in the fields and i click on the button , the page redirect me to the first selection instend to the textarea with the input, any idea why ?
@Adrian be sure to have called event.preventDefault() like in my fiddle.
i did, code lookes like this now : $("#sendFields").on('submit',function(e){ e.preventDefault(); $.ajax({.... do i have to change my ajax maybe ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.