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.
You need to add the name
attribute to your input
s. 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.
You are sending data with GET
method in $.ajax
:
type: "GET"
And in PHP, you are expecting:
$name = $_POST["name"];
Make it
type: "POST"
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>
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.