0

I have a html form below:

    <section id="main" style="float: right;  text-align: left;  width: 83%;">   
        <article>
            <form>
                <p>Name:   <div><input id="name" type=text/></p></div><br \>
                <p>Phone:   <div><input id="phone" type=text/></p></div><br \>
                <p>Address:   <div><input id="address" type=text/></p></div><br \>
                <p>Username:   <div><input id="username" type=text/></p></div><br \>
                <p>Password:   <div><input id="password" type="password"/></p></div><br \>
                <p>Email:   <div><input id="email" type="text"/></p></div>
                <div><input id="submitDB" type="submit" value="Register"/></div>
            </form>
            <script type="text/javascript">
                      $('#submitDB').on('click', function() {
                          event.preventDefault();

                          $.ajax({
                               url: "registerDB.php",
                               data: { name: $('#name').val(), phone: $('#phone').val(), address: $('#address').val(), username: $('#username').val(), password: $('#password').val(), email: $('#email').val()},
                               type: "POST"
                          }).done(function(data) {
                              if(data === "true"){
                                  alert(data);
                              }
                              else
                              {
                                  alert(data);
                              }
                          });

                      });
           </script>
        </article>
    </section>

This form should submit the inputs and call a "registerDB.php" file which contains

    <?php
        require_once("config.php");
        require_once("MysqliDb.php");

        $DB = new MysqliDb($DBCREDS['HOSTNAME'], $DBCREDS['USERNAME'], $DBCREDS['PASSWORD'], $DBCREDS['DATABASE']);

        $name = $_POST["name"];
        $phone = $_POST['phone'];
        $address = $_POST['address'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];



        $data = Array ("name" => $name,
                       "phone" => $phone,
                       "address" => $address,
                       "email" => $email,
                    );

        var_dump($data);
       if($DB->insert('member', $data))
        {
                $user = Array ( 
                        "username" => $username,
                        "password" => $password,
                        );

                        var_dump($user);
                        die();
            if($DB->insert('member_login', $user))
            {
                echo "You are now registered!";
            }
        }   
        else
        {
            echo "Please Fill All Data And Resubmit";
        }





    ?>

However the username and password variables are not passing through. Is there something I am missing here? This does not have to be super "secure" I am working on a project for my System Analysis class and basically am working on making a website for a presentation.

0

4 Answers 4

2

You need to add the name attribute to your inputs. Such as:

<form id="myForm">
    <p>Name:   <div><input id="name" name="name" type=text/></p></div><br \>
    <p>Phone:   <div><input id="phone" name="phone" type=text/></p></div><br \>
    <p>Address:   <div><input id="address" name="address" type=text/></p></div><br \>
    <p>Username:   <div><input id="username" name="username" type=text/></p></div><br \>
    <p>Password:   <div><input id="password" name="password" type="password"/></p></div><br \>
    <p>Email:   <div><input id="email" name="email" type="text"/></p></div>
    <div><input id="submitDB" type="submit" value="Register"/></div>
</form>

And use the following to submit the form #myForm with jquery. (make 'myForm' as the id of your form)

$("#myForm").ajaxForm({url: 'registerDB.php', type: 'post'})

TIP: use ajaxForm so as not to reinvent the wheel when creating ajax forms.

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

1 Comment

Awesome, glad it did!
2

You need to add the name attribute to your form inputs:

<form>
    <p>Name:   <div><input id="name" name="name" type=text/></p></div><br \>
    <p>Phone:   <div><input id="phone" name="phone" type=text/></p></div><br \>
    <p>Address:   <div><input id="address" name="address" type=text/></p></div><br \>
    <p>Username:   <div><input id="username" name="username" type=text/></p></div><br \>
    <p>Password:   <div><input id="password" name="password" type="password"/></p></div><br \>
    <p>Email:   <div><input id="email" name="email" type="text"/></p></div>
    <div><input id="submitDB" type="submit" value="Register"/></div>
</form>

Comments

1

You are sending data with GET method in $.ajax:

type: "GET"

And in PHP, you are expecting:

$name = $_POST["name"];

Make it

type: "POST"

1 Comment

Yes, I noticed that after posting. That was an error on my part for not checking my code before uploading ( I was playing around with it to see if it was my POST that wasnt allowing it to pass). Thank you though!
0

I tested your HTML on my server and had no problems sending it to a basic PHP file. My PHP looked like this:

<?php
print_r($_POST);
?>

I suspect you're having an error with your PHP, perhaps with the connection to the SQL database. Try turning on error reporting to get some more information.

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.