0

I'm new here. I wanted to know how can i get code from ajax and use it in php.

 <script>
        let arrOf = {};
        arrOf.name = "Grig";
        arrOf.age = 28;
        $.ajax({
            type : "POST",  
            url  : "insert.inc.php",
            data : { arrOf : arrOf },
            success: function(res){  
                     console.log("worked");
                    }
        });
      </script>

I tried this, but it is wrong.

 <?php
         $arrOf = $_POST['arrOf'];  // Undefined index: arrOf
         echo $arrOf ;
    ?>

I want to have variable like js in php arrOf

Julie
  • 11
  • 2
  • You're posting, so use `$_POST[]`. – KIKO Software Feb 12 '21 at 11:22
  • I changed it, but had same error – Julie Feb 12 '21 at 11:23
  • 1
    That code should not produce that error. It isn't clear how you are even seeing the error since you don't inspect `res` in the success function and you have no `error` function. – Quentin Feb 12 '21 at 11:24
  • `insert.inc.php` — Files with `inc` in the name as conventionally designed to be used solely as includes in other files and not accessed by clients directly. You might want to rethink your naming conventions to avoid confusion. – Quentin Feb 12 '21 at 11:25
  • The part where I echo it i didn't get the value of arrOf. When i echo it i see. Notice: Undefined index: arrOf in C:\xampp\htdocs\new\insert.inc.php on line 299 – Julie Feb 12 '21 at 11:27
  • Restating what's already in the question doesn't clarify anything for us. It only makes us restate our observations; the code in the question *does not reproduce this problem*. The code in the question won't give an "Undefined index" notice, it will produce an "Array to string conversion" notice (since you're trying to `echo` an array). You need to add more details to your question. – El_Vanja Feb 12 '21 at 11:50
  • I wanted to pass js object to phpand send it to sql. I found that it will be possible to pass object owing to ajax or json. i tried both. buti coudn't anyway pass object to use it. – Julie Feb 12 '21 at 13:12

0 Answers0