How to take the parameters from the form via ajax and pass in a php class?

index.php

<form method="post" class="mt-2">
 <div class="form-group">
 <input type="text" name="name" class="form-control" placeholder="Name">
</div>
 <div class="form-group">
 <input type="text" name="surname" class="form-control" placeholder="Surname">
</div>
 <div class="form-group">
 <input type="email" name="email" class="form-control" aria-describedby="emailHelp" placeholder="Email">
</div>
 <div class="form-group">
 <input type="text" name="phone" class="form-control" placeholder="Phone Number">
</div>
 <div class="form-group">
 <input type="password" name="password" class="form-control" placeholder="Password">
</div>
 <div class="form-group">
 <input type="password" name="reTypePassword" class="form-control" placeholder="Re-Type Password">
</div>
 <button id="sign-up" type="submit" name="signUp" class="btn btn-primary btn-block">Sign Up</button>
</form>

the <script>
 $(document).on('click', '#sign-up', function(){
{
$.ajax({
url:'User.php
method:"POST",
dataType:"text"
});
}
});
 </script>


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 = $_GET['name'];
 $surname = $_GET['surname'];
 $email = $_GET['email'];
 $password =$_GET['password'];
 $reTypePassword = $_GET['reTypePassword'];
 $phone = $_GET['phone'];

 if ($name == "" || $surname == "" || $email == "" || $password == "" || $phone == "") {
 $message = "<div class="alert alert-danger">պետք է բոլոր դաշտերը լինեն լրացված:</div>";
 return $message;
}

 if (strlen($surname) < 3) {
 $message = "<div class="alert alert-danger">օգտագործողի անունը պետք լինի բաղկացած է առնվազն 3 տառից:</div>";
 return $message;
 } elseif (preg_match('/[^a-z0-9_-]+/i', $surname)) {
 $message = "<div class="alert alert-danger">Օգտագործողի անունը պետք է միայն պարունակի տառեր, թվեր կամ գիծ:</div>";
 return $message;
}

if(!is_numeric($phone)){
 $message = "<div class="alert alert-danger">Հեռախոսահմարը պետք է միայն պարունակի պարունակի թվեր:</div>";
 return $message;
}

 if($password !== $reTypePassword){
 $message = "<div class="alert alert-danger">Գաղտնաբարերը չեն համընկնում:</div>";
 return $message;
}

 $sql = "INSERT INTO users(name, surname, email, password, phone) VALUES(:name, :surname, :email, :password, :phone)";
 $query = $this->db->pdo->prepare($sql);
 $query->bindValue(':name', $name);
 $query->bindValue(':surname', $surname);
 $query->bindValue(':email', $email);
 $query->bindValue(':password', $password);
 $query->bindValue(':phone', $phone);
 $result = $query->execute();

 if ($result) {
 $message = "<div class="alert alert-success">Շնորհակալություն, դուք գրանցվեցիք Color School կայքում:</div>";
 return $message;
 } else {
 $message = "<div class="alert alert-danger">կներեք, առաջացավ խնդիրներ ձեր տվյալները տեղադրելուց:</div>";
 return $message;
}
}
}
June 8th 19 at 17:17
3 answers
June 8th 19 at 17:19
$(document).on('submit', 'form', function(event){
event.preventDefault();
$.ajax({
url:'User.php',
method:"POST",
 data: $("form").serialize(),
dataType:"text"
});
});

But the url parameter you have the wrong value. Apparently is a router, and the URL will be different, definitely not User.php. Perhaps '/user/registration'
click zameniti to submit the same need? - Mohamed_Lind75 commented on June 8th 19 at 17:22
I have in the Classes folder User.php class in this class eats userRegistartion function I need to pass parameters to functions userRegistartion - Lew.Larson commented on June 8th 19 at 17:25
well , in General, Yes, and change the selector (the button and have the form to specify) - violet_Dibbe commented on June 8th 19 at 17:28
I understand how you have the files and functions are called, but we must look at how routing is implemented. You will be the link to transfer request, and your engine already recognizes which link you send, and send what you need class and method. Your files is not enough to uniquely identify which link should send the request. - violet_Dibbe commented on June 8th 19 at 17:31
June 8th 19 at 17:21
$(document).on('submit', 'form', function () {
...
}
June 8th 19 at 17:23
...
public function userRegistration()
{
 $name = $_POST['name'];
 $surname = $_POST['surname'];
 $email = $_POST['email'];
 $password = $_POST['password'];
 $reTypePassword = $_POST['reTypePassword'];
 $phone = $_POST['phone'];
...

The form submits POST data. Accordingly, to take them You must use variable $_POSTinstead of $_GET in the above example.

Find more questions by tags PHPAJAX