I've been trying to solve this issue I have regarding the communication between jQuery, php and mySQL.
Long story short, I want to insert the information in my "registration form" if you will, into my database through PHP.
This is my form, in login.php:
<form action="login.php" method="post">
<input type="text" name="username" id="username">
<br><br>
<input type="password" name="password">
<br><br>
<input type="text" name="email">
<br><br>
<input type="submit" value="Register" class="submitRegistration"/>
</form>
And I want this to be triggered when I press the Register button, this is in head.php.
$(document).ready(function(){
$(".submitRegistration").click(function(){
var clickBtnValue = $(this).val();
var username = $("#username").val();
var password = $("#password").val();
var email = $("#email").val();
var ajaxurl = 'ajaxdisk.php';
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
alert(response);
});
});
});
This is the database connection in ajax.php and the function I want to run
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "swag";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//I have suspicions that this is where things go wrong, because it checks if
//$_POST['action'] != NULL before it is defined, I want it to
//do this WHEN the button is hit, not when the page loads.
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'Submit':
submit();
break;
case 'Register':
register($conn);
break;
}
}
else {
print("Error");
}
function submit() {
echo "The submit function is called.(unfinished)";
exit;
}
function register($cpnn) {
echo "The register function is called.";
$sql = "INSERT INTO MyGuests (username, password, email)
-- Here i want the values from the textboxes to be, so it gets put into the database
VALUES (username, password, email)";
if ($cpnn->query($sql) === TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
I probably have messed it up somehow from the countless hours of trying to solve this, but I really hope one of you can make any sense out of it and recognize the error.
Thanks.
Changes are stated by comment:-
Your html file code will be like below:-
<form method="post">
<input type="text" name="username" id="username">
<br><br>
<input type="password" name="password" id="password"> <!-- id required -->
<br><br>
<input type="text" name="email" id="email"> <!-- id required -->
<br><br>
<input type="submit" value="Register" class="submitRegistration"/>
</form>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type = "text/javascript">
$(document).ready(function(){
$(".submitRegistration").click(function(e){// e for event
e.preventDefault(); // to stop form submission
var clickBtnValue = $(this).val();
var username = $("#username").val();
var password = $("#password").val();
var email = $("#email").val();
$.post('ajaxdisk.php', {'action': clickBtnValue,"username":username,"password":password,"email":email}, function (response) {
alert(response);
});
});
});
</script>
Your php file(ajaxdisk.php) code will be like below:-
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "swag";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['action'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
echo "The register function is called.";
$sql = 'INSERT INTO `MyGuests` (username, password, email) VALUES ("'.(string)$username.'","'.(string)$password.'","'.(string)$email.'")';
if ($conn->query($sql)) {
echo "New record created successfully";
}
else {
echo "Error: ". $conn->error;
}
}
?>
Note:- the problem is in your switch
code (i am unable to find out what). Also your values are coming as string but in query it is not treated as string (this is also in-understandable for me why this happening)
This code will work for you for sure. Check once.
My actual code:-