Answer the question
In order to leave comments, you need to log in
What is wrong with my codes, not being transferred to the database?
index.php
<form method="post">
<input id="name" type="text" name="name" class="form-control" placeholder="Name">
<input id="button" type="button" value="Load">
</form>
$('#button').click(function(){
var name = $('#name').val();
$.ajax({
url: 'User.php',
method:"POST",
data: 'name='+name,
});
});
<?php
include 'Database.php';
include_once 'Session.php';
class User
{
private $db;
public function __construct()
{
$this->db = new Database();
}
public function userRegistration()
{
$name = $_POST['name'];
$sql = "INSERT INTO users(name) VALUES(:name)";
$query = $this->db->pdo->prepare($sql);
$query->bindValue(':name', $name);
$result = $query->execute();
return $result;
}
}
if(isset($_POST['name'])) {
$register = new User();
$result = $register->userRegistration();
echo $result;
}
Answer the question
In order to leave comments, you need to log in
At least write the error that appears when you try to submit the form via ajax.
In general, for starters, it’s worth checking the performance of your code directly, without ajax. You just need to add an action to the form and make a button type = "submit"
If you have an error in php, then you will immediately see it. And when php is tested and debugged, it will be possible to do the execution via ajax
Although I am new to this business, even for me it hurts. It's not very clear if you have jq connected and why isn't Ajax wrapped in a script tag?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question