L
L
Lavrov952018-02-01 00:22:58
PHP
Lavrov95, 2018-02-01 00:22:58

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,
    });
});


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;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
VelAnna, 2018-02-01
@Lavrov95

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

S
Senbonzakuraa, 2018-02-01
@Senbonzakuraa

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?

V
Vasiliy_M, 2018-02-01
@Vasiliy_M

dude, you screwed up. 3 theme with your govnokodom.
read this - phpfaq.ru/debug

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question