help me sir

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sajidlrk
Forum Newbie
Posts: 1
Joined: Mon Jun 27, 2016 6:34 am

help me sir

Post 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>
unkemolyte424
Forum Newbie
Posts: 1
Joined: Wed Sep 28, 2016 10:45 am

Re: help me sir

Post by unkemolyte424 »

Also interested in the solution anyone there?
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: help me sir

Post 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
Post Reply