Page 1 of 1

help me sir

Posted: Mon Jun 27, 2016 6:47 am
by sajidlrk
Hello sir,
I have wrote form validation code in javascript, it is working fine on register.php
In index.php I loaded register.php in a div through ajax. When I am trying to submit empty form then form validation code does not work. And register.php disappear from container div.
I would like to say when I submit empty form that do not disappear from container div till I fill a value and click on submit button.
My program code is written bellow. If anybody help me then I will be great full of him/her.

Register.php

Code: Select all

<?php
$page=$_SERVER['SCRIPT_NAME'];
if(isset($_POST['username']) && !empty($_POST['username'])){
    echo 'OK';
}
?>
<script type="text/javascript">
function check(){
    if(document.getElementById('username').value == ""){
        document.getElementById('username').style.borderColor="red";
        return false;   
    }    
}
</script>
<form action="<?php echo $page;?>" method="POST" onsubmit="return check();">
Your name: <input type="text" id="username" name="username" />
<input type="submit" />
</form>
Index.php

Code: Select all

<script type="text/javascript">
    function load(){
        if(window.XMLHttpRequest){
            xmlHttp=new XMLHttpRequest();
        }
        else{
            xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
        }
        xmlHttp.onreadystatechange= function(){
            if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
                document.getElementById('container').innerHTML=xmlHttp.responseText;
            }    
        }
        xmlHttp.open('GET','register.php' ,true);
        xmlHttp.send();   }

</script>
<a href="#" onclick="load();" >register</a>
<br />
<div id="container"></div>

<style>
#container{
    width: 350px;
    padding: 40px;
    background-color: beige;
}
</style>

Re: help me sir

Posted: Thu Oct 06, 2016 2:08 am
by unkemolyte424
Also interested in the solution anyone there?

Re: help me sir

Posted: Thu Oct 06, 2016 3:44 am
by pbs
First Your javascript validation functinss should be on index.php page, as you are loading form using AJAX, register.php should only have HTML
form.

Second, you need to submit form using AJAX only.

You can check online for some example