简体   繁体   中英

Data of jQuery array won't show up on a database after transferring its data to a PHP array in another file

I am sending values of a jQuery array to another PHP array from {file.js} to {file.php} but the transferred data won't show up on the database An empty row in the targeted column will show up in the the table of the DataBase while the ID will occur

The script that I wrote on file.js is the following:

var emailText;
var emailArray = new Array();
$("#emailAddress").keypress(function (event){
    if(event.which === 13){
        var emailText = $(this).val();
        $(this).val("");
        emailArray.push(emailText);
        $('ul').prepend('<li>' + emailText + '<span><i class="fa fa-trash"></i></span>');
        $.post(
            "http://localhost/main_dir/dir/file.php", 
            emailArray,
            function(){
            alert('Your email has been added to the database successfully');
           }
        );
    }
});

And the code that I wrote on file.php is the following:

<?php

require_once 'dbhandler.php';

$add_email = array("");

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $add_email = $_POST['emailArray'];

    $sql = "INSERT INTO databse (new_added_email) VALUES ('$add_email');";
    $result = mysqli_query($conn, $sql);

}


?>

1st: I don't know what is the purpose of using array for emails while ID must be unique and you must have only one email input So no need for array in your case

2nd: As I posted on the comment data should post between curly braces {}

3rd: Search for any kind of validation to validate the data posted to the database

4th: It will be better if you post the email to the php/database then when returned prepend the email to the user

In js

$("#emailAddress").keypress(function (event){
    if(event.which === 13){
        var $this = $(this);
        var emailText = $this.val();
        if(validateEmail(emailText)){
         $.post(
            "http://localhost/main_dir/dir/file.php", 
            {email : email},
            function(data){
              console.log(data);
              $this.val("");
              $('ul').prepend('<li>' + data + '<span><i class="fa fa-trash"></i></span>');
              alert('Your email has been added to the database successfully');
           }
        );
       }else{
          alert(emailText+' Not valid email');
       }
    }
});

function validateEmail(email) {
     const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
     return re.test(email);
}

In php

<?php

require_once 'dbhandler.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $add_email = $_POST['email'];
    // check for email validation 
    // if(email_is_valid){
      $sql = "INSERT INTO databse (new_added_email) VALUES ('$add_email');";
    
      if($result = mysqli_query($conn, $sql)){
         echo $add_email;
      }
    // } 
}


?>

I added function validateEmail(email) in js to check email validation you'll need another one for php validation just search for it then use it in php file before dealing with the database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM