0

Would like to ask for a help. I'm having an issue about getting the right data that I wanted. Maybe there's something lack in my code. I always get null.

I didn't received any error on the console tab.

Here's my ajax

$(document).ready(function() {
  $(".fsubmit").click(function() {    
   var emailval = $("#email").val().trim();
   
   $.ajax({
    url: '/test.php',
    type: 'POST',
    dataType: 'json',
    data: 'email='+emailval,
    success: function(data) { 
     console.log(data);
     return false;
    }
   });
  });
 });

Maybe you could help me fix the PHP. Still learning some stuff. Kindly put some details for lesson please.

Here's my PHP code

<?php 

    $date = new DateTime();
    $datelog = $date->format('d.m.Y h:i:s');
    $data = $_POST['data'];

    $message = '[' . $datelog . '] - email: ' . json_encode($data);

    echo($message);
?>

I appreciate your answer!

Kobe Bryan
  • 317
  • 1
  • 3
  • 16

3 Answers3

0

try this:

$(document).ready(function() {
    $(".fsubmit").click(function() {    
        var emailval = $("#email").val().trim();

        $.ajax({
            url: '/test.php',
            type: 'POST',
            dataType: 'json',
            data: {email:emailval},
            success: function(data) {   
                console.log(data);
                return false;
            }
        });
    });
});

then in php:

<?php 

    $date = new DateTime();
    $datelog = $date->format('d.m.Y h:i:s');
    $data = isset($_POST['email'])?$_POST['email']:'';

    $message = '[' . $datelog . '] - email: ' . json_encode($data);

    echo($message);
?>

You should always check for the posted elements if its isset or not.

Mohammad
  • 3,449
  • 6
  • 48
  • 75
0

Not allowed to comment. Here's a little edit to Mohammad's answer. isset() returns true for blank form fields. Use empty() instead.

this runs in my system ..

ajax call

    $(document).ready(function(e) {
        $(".fsubmit").click(function() {  
            e.preventDefault();
            console.log("whooowww");
            var emailval = $("#email").val().trim();

            $.ajax({
                url: '/arrayfilter.php',
                type: 'POST',
                dataType: 'json',
                data: 'email='+emailval,
                success: function(data) {   
                    console.log(data);
                    return false;
                }
            });
        });
    });

html form

<form>
    <input type="text" name="user-input" id="email"/>
    <button type="submit">Send</button>
</form>

php

if($_POST){
    if(!empty($_POST['email']))
        echo "$_POST[email]";
    }
}
sujan basnet
  • 190
  • 2
  • 12
0

The JS

I can see - at least - one major flaw in your JS code. You are passing data: 'email='+emailval to the request. This would be fine (well - possibly not, since you are not URL-encoding your message) for a GET, but with the post I'd doubt, that jQuery can make some decent JSON out of email=Some arbitrary text. At least it will make your JS vulnerable to injections. There are answers on how to send JSON to a webservice with jQuery, please see here.

The PHP

$_POST['data'] (if this is correct) will contain a string. But according to the PHP documentation

json_encode — Returns the JSON representation of a value

I don't think that this is what you want to do. You have a string, and you'll probably want to get an object from your JSON string (actually at the moment, I doubt that it's really a JSON string what you are passing). You can achieve this with json_decode, which

json_decode — Decodes a JSON string

I'd suggest the following PHP code, which decodes the JSON into an array and then outputs the value of email

$data = json_decode($_POST['data'], true);

$message = '[' . $datelog . '] - email: ' . $data["email"];

For this to work, $_POST['data'] will have to look like this

{'email': 'Your message'}

or the like.

Community
  • 1
  • 1
Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57