0

Problem: Trouble receiving what is being sent to my PHP document.

Javascript:

$('#form_id').submit(function(event){
    event.preventDefault();
    event.stopPropagation();
    var message;
    var myRegExp = validation stuff
    var urlToValidate = document.getElementById("url").value;
    if (!myRegExp.test(urlToValidate)){
        }else{
    var code = (urlToValidate).slice(-22)
    var request = new XMLHttpRequest();

    request.addEventListener('readystatechange', function(event){
    if (this.readyState == 4){
        if (this.status ==200){
            console.log (this.status);
        }else{
            console.log('Server replied with HTTP status ' + this.status);
        }
    }
});

request.open('POST', 'php/submit.php', true);
request.setRequestHeader('Content-Type', 'text/plain');

request.send("code=" + code);
}   
});

Then I'm using this code on my php/submit.php:

if (!empty($_POST['code'])) { 
    $code = $_POST['code'];
    echo $code; 
};

I feel like I'm not using the right tag names for PHP because I'm new to all of this. I'll note that I'm using form id but getting the value from an input.

Ramblings I'm trying to send a user input that has been validated and sliced to mySQL database.

I achieved the string I wanted with javascript and passed it to a variable.

I'm trying to send it to a separate php file in another folder with a request.send(the_javaS_variable).

Now in the console I can see the variable holds the correct text value and I see it sending with state 4 and 200.

But it never shows up on the submit.php page.

11
  • 2
    You cannot use console.log in php, that is a javascript thing. Commented Dec 9, 2014 at 23:02
  • try using 'die(var_dump($_POST));' in your php to see if you have any values being sent over Commented Dec 9, 2014 at 23:06
  • 4
    Why are you using JQuery and not using the JQuery AJAX functions. Commented Dec 9, 2014 at 23:06
  • 1
    @Scott he is already doing an echo $code; what use woudl dieing do. Commented Dec 9, 2014 at 23:07
  • 1
    Try removing this line request.setRequestHeader('Content-Type', 'text/plain'); as plain text header will stop PHP from looking for posted data. Commented Dec 9, 2014 at 23:09

1 Answer 1

1

try this and remove the console.log()

$('#form_id').submit(function(event){
    var myRegExp = validation stuff
    var urlToValidate = document.getElementById("url").value;
    if (!myRegExp.test(urlToValidate)){
        // failed
    }else{

        var code = 'code='+(urlToValidate).slice(-22);

        $.post('php/submit.php', code, function() {
            //   success stuff
        });

    }
    return false;
});
Sign up to request clarification or add additional context in comments.

1 Comment

I just wanted to say that I ended up scrapping everything I had and learned a bit about AJAX. I used this code when I rewrote everything and it works, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.