skip to Main Content

I am trying to send form data to a php script, but the script is not receiving the data.
I don’t understand why, Javascript should be fine php should be fine.
Testing the data right before the ajax request shows the result, but checking the POST data from server side returns empty.

This is my ajax code:

var request = $.ajax({
        method: "POST",
        url: 'contact-mail.php',
        data: { nume: nume.value, telefon: telefon.value, email: email.value, message: message.value},
        error: function(err, status) { console.log("error contact " + err);}
    });
    request.done(function(msg) {
        alert(msg);
        document.querySelectorAll(".sdc-cardboard-form input").forEach(input => {
            input.value = "";
        });

    });
    request.fail(function( jqXHR, textStatus ) {
        alert( "Request failed: " + textStatus );
    });

This is how I get the data in my PHP script:

$name = $_POST['nume'];
$name = addslashes($name);
$tel = $_POST['telefon'];
$tel = addslashes($tel);
$email = $_POST['email'];
$email = addslashes($email);
$message = $_POST['message'];
$message = addslashes($message);

echo "Server side: ".$name." ".$tel." ".$email." ".$message;

Echo returns only "Server side:" part.

I am using the same method to send data to server on another website.
Anyone see’s any error in my code? Is there something I am missing?
Jquery 3+
PHP 7.2

Update, question. Could it be htaccess rewrite rules which would trim everything from .php onwards?

Here are my htaccess rewrite rules:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

I haven’t read much on htacess, so I am just making a guess.

2

Answers


  1. Since you are using mod-rewrite to redirect the file extensions you will need to do that in your AJAX request. Change:

    url: 'contact-mail.php', 
    

    to

    url: 'contact-mail', 
    
    Login or Signup to reply.
  2. Due to .htaccess rewrite rules (Apache mod_rewrite : https://httpd.apache.org/docs/2.4/rewrite/intro.html ) all the url for those PHP files will be update

    means example.com/test.php will be example.com/test

    that is why your AJAX request did not find the URL and give you an error.

    url: 'contact-mail.php'

    sould be

    url: 'contact-mail'

    please check, if this works or not. if it did not work, reply or comment.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search