1

I am trying to submit data into database but I am getting Undefined Index error.

I used isset function to check if I am getting any value and used print_r but I can’t get any value.

Though ajax is sending the request successfully, still php $_POST can’t get any value. It’s empty.

Here’s the error

Notice: Undefined index: email in C:\xampp\htdocs\msaccess\tst.php on line 5

Notice: Undefined index: username in C:\xampp\htdocs\msaccess\tst.php on line 6

Notice: Undefined index: fullname in C:\xampp\htdocs\msaccess\tst.php on line 7

Notice: Undefined index: password in C:\xampp\htdocs\msaccess\tst.php on line 8

Notice: Undefined index: repass in C:\xampp\htdocs\msaccess\tst.php on line 9
INSERT INTO submitform(fullname,email,username,userpassword,datesubmit) VALUES ('','','','','02/07/2014 02:45:53')
Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Field 'submitform.username' cannot be a zero-length string., SQL state S1000 in SQLExecDirect in C:\xampp\htdocs\msaccess\tst.php on line 13
success

This is my HTML

<div class="form">
                    <div class="form-header">
                        Create Your Account
                    </div>
                    <div class="form-body">
                        <div class="row line">
                            <div class="callit">Full Name: </div>
                            <div class="inputbox"><input class="fullname" type="text" id="fullname" placeholder="Full Name" /></div>
                        </div>
                        <div class="row line">
                            <div class="callit">Email: </div>
                            <div class="inputbox"><input class="email" type="email" id="email" placeholder="Email Address"  /></div>
                        </div>
                        <div class="row line">
                            <div class="callit">Username: </div>
                            <div class="inputbox"><input class="username" type="text" id="username" placeholder="Username" /></div>
                        </div>
                        <div class="row line">
                            <div class="callit">Password: </div>
                            <div class="inputbox"><input class="password" type="password" id="password" placeholder="Password" /></div>
                        </div>
                        <div class="row line">
                            <div class="callit">Retype Password: </div>
                            <div class="inputbox"><input class="repass" type="password" id="repass" placeholder="Retype Password"  /></div>
                        </div>
                    </div>
                    <div class="footer">
                        <button type="button" onclick="vals();">Create Account</button>
                        <div><span id="status"></span></div>
                    </div>
            </div>

This is my Javascript/ Ajax Call

<script type="text/javascript">
        function vals(){
            var e = document.getElementById('email').value;
            var fn = document.getElementById('fullname').value;
            var u = document.getElementById('username').value;
            var ps = document.getElementById('password').value;
            var rep = document.getElementById('repass').value;
            document.getElementById('status').innerHTML = e+' '+fn+' '+u+' '+ps+' '+rep+' ';

            var xmlhttp;
            if(window.XMLHttpRequest){
                xmlhttp = new XMLHttpRequest();
            }
            else{
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState==4 && xmlhttp.status == 200){
                    document.getElementById('status').innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("POST", "tst.php", true);
            xmlhttp.send("email="+e+"&fullname="+fn+"&username="+u+"&password="+ps+"&repass="+rep);
        }
    </script>

Here’s the tst.php file code

<?php
include_once('includes/db.php');


    $email = $_POST['email'];
    $username = $_POST['username'];
    $fullname = $_POST['fullname'];
    $password = $_POST['password'];
    $repass = $_POST['repass'];
    $date = date('m/d/Y H:i:s');
        $sql = "INSERT INTO submitform(fullname,email,username,userpassword,datesubmit) VALUES ('$fullname','$email','$username','$password','$date')";
        echo $sql;
        $rs = odbc_exec($conn, $sql);
        echo 'success';
?>
3
  • 1
    do a var_dump/print_r on $_POST - what does it show Commented Feb 7, 2014 at 2:24
  • Check the Network tab of Developer tools to see what parameters you're sending. Commented Feb 7, 2014 at 2:27
  • [email protected]&fullname=sam&username=myuser&password=sam&repass=sam Commented Feb 7, 2014 at 2:31

2 Answers 2

1

Updated Ajax code

 xmlhttp.open("POST", "tst.php", true);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send("email="+e+"&fullname="+fn+"&username="+u+"&password="+ps+"&repass="+rep);
Sign up to request clarification or add additional context in comments.

Comments

1

Add

        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

above

        xmlhttp.onreadystatechange = function(){

Comments

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.