-1

I am trying to use AJAX to pass a JavaScript array to a PHP array but the problem with me that I got no response in my PHP side. I make the request in the same page not to another page. The problem is that I got no response from the PHP side although I got alert "Success" from the AJAX side error in console: https://drive.google.com/file/d/10KgUcy4WIHTbKbWY6QJvyJi6c_26Qxz8/view?usp=sharing here is my PHP side which I suppose to get my requested data.

<?php
if( isset($_POST['myData']) )
{
console.log("i am here");}
?>

and here my script and AJAX request

<script type="text/javascript">
    
let arr = [1,2,3];
var str_array = arr;
var request = $.ajax({
  method: "POST",
  data: { myData : JSON.stringify(str_array) },
  dataType: "html"
});
request.done(function( msg ) {
  // ajax response
  alert("Success!!");
});
request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});


</script>

  • 1
    Shouldn't `dataType: "html"` be `dataType: "json"`? – evolutionxbox Jun 03 '21 at 10:25
  • ... and `print_r` be `echo` ..? – Teemu Jun 03 '21 at 10:27
  • @Teemu `print_r` formats the output https://www.php.net/manual/en/function.print-r.php – evolutionxbox Jun 03 '21 at 10:28
  • What makes you think that you _“got no response from the PHP side”_? You are not _doing_ anything with whatever content the server might have responded with, anywhere. – CBroe Jun 03 '21 at 10:28
  • @evolutionxbox dataType: "json" gives me failed alert – Mahmoud Hassan Jun 03 '21 at 10:28
  • 1
    @evolutionxbox Yes, I know, but how useful is the PHP format in JS? OP doesn't seem to explicitly use the response value in the example, but jQuery will mess with the PHP format. – Teemu Jun 03 '21 at 10:29
  • @evolutionxbox `dataType` is for what you expect to get back _from_ the server, so that jQuery can f.e. automatically parse a JSON response into the according object for you, without you having to do that in an extra, manual step. – CBroe Jun 03 '21 at 10:29
  • 1
    _“dataType: "json" gives me failed alert”_ - that’s because now jQuery tries to parse the print_r output (which is not JSON, not even close), as JSON for you. Just do a `console.log(msg)` inside the `done` callback first of all, and check what that gets you on the console. Still think you _“got no response from the PHP side”_ …? – CBroe Jun 03 '21 at 10:31
  • @CBroe sorry wrote old PHP code ... I use the echo to print anything but nothing happens – Mahmoud Hassan Jun 03 '21 at 10:32
  • do you see any errors in the browser console? – evolutionxbox Jun 03 '21 at 10:33
  • @evolutionxbox yes pls see my image link – Mahmoud Hassan Jun 03 '21 at 10:38
  • [Uncaught ReferenceError: $ is not defined?](https://stackoverflow.com/questions/2075337/uncaught-referenceerror-is-not-defined) – CBroe Jun 03 '21 at 10:40
  • @CBroe I have more code to execute in the PHP side but this my main core problem now that PHP side is not working. It does not enter the "if" statement. thats why I used print and echo statement to show you that no response – Mahmoud Hassan Jun 03 '21 at 10:41
  • 2
    This all makes very little sense. As long as you get an error saying that `$` is not defined, how could the AJAX request even happen in the first place? As long as _that_ error exists, you should have never gotten any “Success!!” alert in the first place. You need to stop giving us confusing and contradictory information first of all. – CBroe Jun 03 '21 at 10:43
  • @CBroe sorry Its really silly mistake from me. now the error is gone but still there is not any printing show from the php side – Mahmoud Hassan Jun 03 '21 at 10:45
  • 1
    Then please edit your question to show us what your JS code currently actually looks like. It is incredibly hard to try and piece things like this together, if we get some initial code shown, that then gets _modified_ based on several suggestions from different people, and all we get in between is vague descriptions of what you did plus “not working”. – CBroe Jun 03 '21 at 10:48

2 Answers2

0

Looking at Your code it should always give you a success. Even if no information is sent php will return an empty page with a 200.

The only time it fails is when the php page will return a error code in the header.

For testing purposes try:

<?php
var_dump($_POST);
?>

Also note that by omitting the url option you're sending the request to the current page so it would return the whole html of the page along with whatever the response you're trying to get.

Sleepwalker
  • 101
  • 6
0

If you want to send a request to PHP through ajax you have to create a PHP script file separately. I think we can't send requests to the same file. and here is the code that I use for ajax requests.

script.php this should be a separate PHP file.

<?php
 if( isset($_POST['myData']) )
   {
     echo json_encode($_POST['myData']);
   }
?>

Here is the jquery ajax code.

var str_array = [1,2,3]
$.ajax({
    type: "POST",
    url: "./script.php",
    data: {myData:str_array},
    dataType: "json",
    success: function (response) {
      console.log(response);
    },
    error:function(data) {
        console.log(data.responseText);
    },
    
 });