Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Newsleeter - imie
ayo1001
post 25.05.2015, 14:19:13
Post #1





Grupa: Zarejestrowani
Postów: 74
Pomógł: 1
Dołączył: 29.03.2013

Ostrzeżenie: (0%)
-----


Cześć,

Jak dodać do tego kodu jeszcze pole $name ?

  1. <?php
  2. class Newsletter
  3. {
  4. private static $email;
  5. private static $datetime = null;
  6.  
  7. private static $valid = true;
  8.  
  9. public function __construct() {
  10. die('Init function is not allowed');
  11. }
  12.  
  13. public static function register($email) {
  14. if (!empty($_POST)) {
  15. self::$email = $_POST['signup-email'];
  16. self::$datetime = date('Y-m-d H:i:s');
  17.  
  18. if (empty(self::$email)) {
  19. $status = "error";
  20. $message = "The email address field must not be blank";
  21. self::$valid = false;
  22. } else if (!filter_var(self::$email, FILTER_VALIDATE_EMAIL)) {
  23. $status = "error";
  24. $message = "You must fill the field with a valid email address";
  25. self::$valid = false;
  26. }
  27.  
  28. if (self::$valid) {
  29. $pdo = Database::connect();
  30. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  31. $existingSignup = $pdo->prepare("SELECT COUNT(*) FROM newsletter WHERE newsletter_email_address='$email'");
  32. $existingSignup->execute();
  33. $data_exists = ($existingSignup->fetchColumn() > 0) ? true : false;
  34.  
  35. if (!$data_exists) {
  36. $sql = "INSERT INTO newsletter (newsletter_email_address, newsletter_date) VALUES (:email, :datetime)";
  37. $q = $pdo->prepare($sql);
  38.  
  39. $q->execute(
  40. array(':email' => self::$email, ':datetime' => self::$datetime));
  41.  
  42. if ($q) {
  43. $status = "success";
  44. $message = "You have been successfully subscribed";
  45. } else {
  46. $status = "error";
  47. $message = "An error occurred, please try again";
  48. }
  49. } else {
  50. $status = "error";
  51. $message = "This email is already subscribed";
  52. }
  53. }
  54.  
  55. $data = array(
  56. 'status' => $status,
  57. 'message' => $message
  58. );
  59.  
  60. echo json_encode($data);
  61.  
  62. Database::disconnect();
  63. }
  64. }
  65. }
Go to the top of the page
+Quote Post
phpion
post 25.05.2015, 14:23:27
Post #2





Grupa: Moderatorzy
Postów: 6 070
Pomógł: 860
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Analogicznie do wykorzystania:
  1. self::$email = $_POST['signup-email'];

Nie powiem żeby to był elegancki sposób (tym bardziej, że metoda przyjmuje parametr $email), ale w podobny sposób to ogarniesz.
Go to the top of the page
+Quote Post
ayo1001
post 25.05.2015, 14:42:00
Post #3





Grupa: Zarejestrowani
Postów: 74
Pomógł: 1
Dołączył: 29.03.2013

Ostrzeżenie: (0%)
-----


  1. <?php
  2. class Newsletter
  3. {
  4. private static $name;
  5. private static $email;
  6. private static $datetime = null;
  7.  
  8. private static $valid = true;
  9.  
  10. public function __construct() {
  11. die('Init function is not allowed');
  12. }
  13. public static function register($name, $email) {
  14. if (!empty($_POST)) {
  15. self::$name = $_POST['name-email'];
  16. self::$email = $_POST['signup-email'];
  17. self::$datetime = date('Y-m-d H:i:s');
  18.  
  19. if (empty(self::$email)) {
  20. $status = "error";
  21. $message = "The email address field must not be blank";
  22. self::$valid = false;
  23. } else if (!filter_var(self::$email, FILTER_VALIDATE_EMAIL)) {
  24. $status = "error";
  25. $message = "You must fill the field with a valid email address";
  26. self::$valid = false;
  27. }
  28.  
  29. if (empty(self::$name)) {
  30. $status = "error";
  31. $message = "The name field must not be blank";
  32. self::$valid = false;
  33. } else if (!filter_var(self::$name, FILTER_VALIDATE_NAME)) {
  34. $status = "error";
  35. $message = "You must fill the field with a name";
  36. self::$valid = false;
  37. }
  38.  
  39. if (self::$valid) {
  40. $pdo = Database::connect();
  41. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  42. $existingSignup = $pdo->prepare("SELECT COUNT(*) FROM newsletter WHERE newsletter_name='$name', newsletter_email_address='$email'");
  43. $existingSignup->execute();
  44. $data_exists = ($existingSignup->fetchColumn() > 0) ? true : false;
  45.  
  46. if (!$data_exists) {
  47. $sql = "INSERT INTO newsletter (newsletter_name, newsletter_email_address, newsletter_date) VALUES (:name, :email, :datetime)";
  48. $q = $pdo->prepare($sql);
  49.  
  50. $q->execute(
  51. array(':name' => self::$name, ':email' => self::$email, ':datetime' => self::$datetime));
  52.  
  53. if ($q) {
  54. $status = "success";
  55. $message = "You have been successfully subscribed";
  56. } else {
  57. $status = "error";
  58. $message = "An error occurred, please try again";
  59. }
  60. } else {
  61. $status = "error";
  62. $message = "This email is already subscribed";
  63. }
  64. }
  65.  
  66. $data = array(
  67. 'status' => $status,
  68. 'message' => $message
  69. );
  70.  
  71. echo json_encode($data);
  72.  
  73. Database::disconnect();
  74. }
  75. }
  76. }


Mam coś takiego, tylko problem w tym, że wyświetla mi się błąd przy wysyłaniu "An error occurred, please try again"
Go to the top of the page
+Quote Post
phpion
post 25.05.2015, 14:48:20
Post #4





Grupa: Moderatorzy
Postów: 6 070
Pomógł: 860
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




1. Na pewno $_POST['name-email']? Logiczniej byłoby $_POST['signup-name'].
2. Nie:
  1. $existingSignup = $pdo->prepare("SELECT COUNT(*) FROM newsletter WHERE newsletter_name='$name', newsletter_email_address='$email'");

tylko:
  1. $existingSignup = $pdo->prepare("SELECT COUNT(*) FROM newsletter WHERE newsletter_name='$name' AND newsletter_email_address='$email'");

Moim zdaniem w sumie powinieneś sprawdzać tutaj tylko adres e-mail, a nie parę e-mail + imię, czyli powinno być chyba samo:
  1. $existingSignup = $pdo->prepare("SELECT COUNT(*) FROM newsletter WHERE newsletter_email_address='$email'");

3. Na pewno masz w tabeli newsletter kolumnę newsletter_name?
Go to the top of the page
+Quote Post
ayo1001
post 25.05.2015, 15:15:54
Post #5





Grupa: Zarejestrowani
Postów: 74
Pomógł: 1
Dołączył: 29.03.2013

Ostrzeżenie: (0%)
-----


Mam jeszcze taki plik send.php

Było:

  1. <?php
  2. require 'inc/Database.class.php';
  3. require 'inc/Newsletter.class.php';
  4.  
  5. if (!empty($_POST)) {
  6. $email = $_POST['signup-email'];
  7.  
  8. Newsletter::register($email);
  9. }


Tak jest:

  1. <?php
  2. require 'inc/Database.class.php';
  3. require 'inc/Newsletter.class.php';
  4.  
  5. if (!empty($_POST)) {
  6. $name = $_POST['signup-name'];
  7. $email = $_POST['signup-email'];
  8.  
  9. Newsletter::register($name, $email);
  10. }


Teraz mam:

  1. <?php
  2. class Newsletter
  3. {
  4. private static $name;
  5. private static $email;
  6. private static $datetime = null;
  7.  
  8. private static $valid = true;
  9.  
  10. public function __construct() {
  11. die('Init function is not allowed');
  12. }
  13. public static function register($name, $email) {
  14. if (!empty($_POST)) {
  15. self::$name = $_POST['signup-name'];
  16. self::$email = $_POST['signup-email'];
  17. self::$datetime = date('Y-m-d H:i:s');
  18.  
  19. if (empty(self::$email)) {
  20. $status = "error";
  21. $message = "The email address field must not be blank";
  22. self::$valid = false;
  23. } else if (!filter_var(self::$email, FILTER_VALIDATE_EMAIL)) {
  24. $status = "error";
  25. $message = "You must fill the field with a valid email address";
  26. self::$valid = false;
  27. }
  28.  
  29. if (empty(self::$name)) {
  30. $status = "error";
  31. $message = "The name field must not be blank";
  32. self::$valid = false;
  33. } else if (!filter_var(self::$name, FILTER_VALIDATE_NAME)) {
  34. $status = "error";
  35. $message = "You must fill the field with a name";
  36. self::$valid = false;
  37. }
  38.  
  39. if (self::$valid) {
  40. $pdo = Database::connect();
  41. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  42. $existingSignup = $pdo->prepare("SELECT COUNT(*) FROM newsletter WHERE newsletter_name='$name' AND newsletter_email_address='$email'");
  43. $existingSignup->execute();
  44. $data_exists = ($existingSignup->fetchColumn() > 0) ? true : false;
  45.  
  46. if (!$data_exists) {
  47. $sql = "INSERT INTO newsletter (newsletter_name, newsletter_email_address, newsletter_date) VALUES (:name, :email, :datetime)";
  48. $q = $pdo->prepare($sql);
  49.  
  50. $q->execute(
  51. array(':name' => self::$name, ':email' => self::$email, ':datetime' => self::$datetime));
  52.  
  53. if ($q) {
  54. $status = "success";
  55. $message = "You have been successfully subscribed";
  56. } else {
  57. $status = "error";
  58. $message = "An error occurred, please try again";
  59. }
  60. } else {
  61. $status = "error";
  62. $message = "This email is already subscribed";
  63. }
  64. }
  65.  
  66. $data = array(
  67. 'status' => $status,
  68. 'message' => $message
  69. );
  70.  
  71. echo json_encode($data);
  72.  
  73. Database::disconnect();
  74. }
  75. }
  76. }


i mam problem z: "An error occurred, please try again"

Okej, ostatnim problemem jest:

  1. if (empty(self::$name)) {
  2. $status = "error";
  3. $message = "The name field must not be blank";
  4. self::$valid = false;
  5. } else if (!filter_var(self::$name, FILTER_VALIDATE_EMAIL)) {
  6. $status = "error";
  7. $message = "You must fill the field with a name";
  8. self::$valid = false;
  9. }


Jak dać sprawdzanie po imieniu ?

Problem rozwiązany, dzięki!

Ten post edytował ayo1001 25.05.2015, 15:11:58
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 16.04.2024 - 13:11