What's wrong with my codes not transmitted in 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,
});
});


User.php

<?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;
}
June 8th 19 at 17:13
3 answers
June 8th 19 at 17:15
Solution
You at least write the error that appears when trying to submit the form via ajax.

In General to start is to check the efficiency of your code directly without ajax. Just need to add to the form action and button make type="submit"
If you have an error in php, so you will see it immediately. And when will be tested and debugged php, can be done executing through ajax
php is working, checked - Lew.Larson commented on June 8th 19 at 17:18
POST www.chance.localhost/User.php 500 (Internal Server Error)
send @ jquery.min.js:2
ajax @ jquery.min.js:2
(anonymous) @ app.js:30
dispatch @ jquery.min.js:2
y.handle @ jquery.min.js:2 - Lew.Larson commented on June 8th 19 at 17:21
then what's wrong with your "code"? - marilyne_Roh commented on June 8th 19 at 17:24
{readyState: 4, getResponseHeader: d, getAllResponseHeaders: d, setRequestHeader: ƒ, overrideMimeType: ƒ, ...}
abort
:
ƒ (e)
always
:
ƒ ()
catch
:
ƒ (e)
done
:
ƒ ()
fail
:
ƒ ()
getAllResponseHeaders
:
ƒ ()
getResponseHeader
:
ƒ (e)
overrideMimeType
:
ƒ (e)
pipe
:
ƒ ()
progress
:
ƒ ()
promise
:
ƒ (e)
readyState
:
4
responseText
:
""
setRequestHeader
:
ƒ (e,t)
state
:
ƒ ()
status
:
500
statusCode
:
ƒ (e)
statusText
:
"Internal Server Error"
then
:
ƒ (t,r,i)
__proto__
:
Object
- Lew.Larson commented on June 8th 19 at 17:27
try to specify method and type
$.ajax({
 url: 'User.php',
 type: "POST",
 data: 'name='+name,
});
- citlalli65 commented on June 8th 19 at 17:30
thanks :D - Lew.Larson commented on June 8th 19 at 17:33
June 8th 19 at 17:17
Though I am a beginner, but even for me it hurts. Not particularly clear whether you have jq and why Ajax the script tag is wrapped?
skills everything is in order, I put the short code - Lew.Larson commented on June 8th 19 at 17:20
and, well, in that case I apologize. - Lew.Larson commented on June 8th 19 at 17:23
June 8th 19 at 17:19
dude, you zaepal. 3. thread with your bad code.
read it here - phpfaq.ru/debug

Find more questions by tags PHPAJAX