-4

I am still a noobie and still learning, but I can't understand why the data from an AJAX call is not being passed successfully into the PHP.

Where have I gone wrong here? I have literally spent hours on hours and I can't understand why the AJAX call hasn't posted this into the PHP. I believe there must be something wrong with either the data attribute from " data: name+email" in the ajax call OR there is something wrong in the PHP side retrieving the data post from AJAX....

I am totally confused... Thanks for your help (and go easy on me!)

Thanks

<?php

include_once 'db.php';

$fullname = $_POST['name'];
$email = $_POST['email'];


//this is the new stuff we are putting in
mysqli_select_db('table1');

if(isset($_POST['email']))
{
$checkdata = "SELECT signup_email_of_user FROM table1 WHERE signup_email_of_user = '$email'";
  $query = mysqli_query($conn, $checkdata);
  if(mysqli_num_rows($query) > 0)
  {
    echo "err";
    exit();
      }

  else {
    $sql = "INSERT INTO table1 (signup_name_of_user, signup_email_of_user) VALUES ('$fullname', '$email')";

    mysqli_query($conn, $sql);
    echo 'ok';
    die;



  }
}

 ?>
    function submitform1(){
        var reg = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
        var name = $('#signup_name_of_user').val();
        var email = $('#signup_email_of_user').val();
          if(name.trim() == '' ){
            alert('Please enter your name.');
            $('#signup_name_of_user').focus();
            return false;
        }else if(email.trim() == '' ){
            alert('Please enter your email.');
            $('#signup_email_of_user').focus();
            return false;
        }else if(email.trim() != '' && !reg.test(email)){
            alert('Please enter valid email.');
            $('#signup_email_of_user').focus();
            return false;
          }else{
            $.ajax({
                type:'POST',
                url:'test_signup.php',
                data: name+email,
                beforeSend: function () {
                    $('.btn-light').attr("disabled","disabled");
                    $('.sign-up').css('opacity', '.5');
                },
                success: function(msg){
                    if(msg == 'ok'){
                        $('#signup_name_of_user').val('');
                        $('#signup_email_of_user').val('');

                        $('.statusMsg').html('<span style="color:green;">Thanks for contacting us, we\'ll get back to you soon.</p>');
                    }else{
                        $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                    }
            
                }
            });
        }
    }
Jeremy1948
  • 45
  • 2
  • data: name+email, theres the problem – user3647971 Jan 13 '18 at 15:34
  • 1
    Your code is vulnerable to [SQL injection attacks](https://en.wikipedia.org/wiki/SQL_injection). You should use prepared statements with bound parameters, via either the [mysqli](https://secure.php.net/manual/en/mysqli.prepare.php) or [PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php) driver. [This post](https://stackoverflow.com/q/60174/6634591) has some good examples. – Luca Kiebel Jan 13 '18 at 15:44
  • He hasn't even gotten to write that part yet, he's not getting data. I'm sure he'll fix it. :) – user3647971 Jan 13 '18 at 15:46

2 Answers2

3

This is indeed wrong:

data: name+email

Values are sent as key/value pairs. All you're sending is a value without a key. How would the server-side code know how to retrieve that value without a key? Instead, consider something like this:

data: { 'name': name, 'email': email }

Then server-side you can retrieve the values by their keys:

$_POST['name']

or:

$_POST['email']

Note: The keys don't need to be the same name as the variables which hold the values. They just seem to be reasonably applicable names in this case. You could just as easily do this:

data: { 'first': name, 'another': email }

and retrieve the values with the updated keys:

$_POST['first']

As your form grows in complexity, there are a variety of ways to create an object in JavaScript to define the keys and values. You could serialize an entire <form> in a single line of code, for example. Or perhaps define more complex objects in JSON, serialized, and then de-serialize them server-side. But starting with some simple key/value pairs would work just fine here.

David
  • 208,112
  • 36
  • 198
  • 279
0

Your data seems to be malformed, try this:

var data = {};
data.name = $('#signup_name_of_user').val();
data.email = $('#signup_email_of_user').val();

and in the ajax: data: data,

According to jQuery documentation data attribute accepts following formats: Type: PlainObject or String or Array

where the PlainObject is the one we need here:

var data = {'objectmember': 'objectvalue'};
user3647971
  • 1,069
  • 1
  • 6
  • 13