0

I'm trying to send a Javascript variable to a php file. Everething is ok in the JS side, I receive a msg that data is sent, but in the php side nothing happen, here is my JS code:

$(".add-to-cart").click(function(event){


    event.preventDefault();

$.ajax({
type: "POST",// see also here
url: 'c.php',// and this path will be proper
data: {
       source1: "some text",
       source2: "some text 2"}
}).done(function( msg )
      {
       alert( "Data Saved: " + msg );// see alert is come or not
     });

});

and the php code:

<?php
$src1= $_POST['source1'];  
$src2= $_POST['source2'];     

echo $src1; 
echo $src2;
?>
3
  • 1
    check for errors and look at your console. Commented May 27, 2016 at 13:39
  • Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server? Commented May 27, 2016 at 14:11
  • Everything is ok in browser's developer tools (JS side), and I,ve already included the jQuery library. the problem is in the php side Commented May 27, 2016 at 15:17

2 Answers 2

1

Try it with:

method: "POST"

Not instead or with the type param too. From jquery docs:

From the docs

You should use type if you're using versions of jQuery prior to 1.9.0.

But it doesn't meantion type is not working in newer versions, at least this is what I experienced.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

1) create a file output.log

In you PHP file

$data = file_get_contents('php://input');
$stream = json_encode($data);
$request = json_encode($_REQUEST);
file_put_contents('output.log', "$stream\n$request);

If non output, then is something wrong between your web page and php. probably routeS?

As you send a object, probably you get a stream

4 Comments

I got this : "" [] in the output file ?!
Because you are not receiving any data. can you press 12 --> go to console tab and try this pastebin.com/c8udThgc
I tried the code in the console tab, I receive this: Object {readyState: 1} source1source1, But I'm still can't get data in the php file
that is a problem. a ready state 1 is opened comm. but real response only at state 4 , even state 2 es when sends

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.