0
$('.addFormForm').submit(function(){
        //requête ajax permettant l'envoie de formulaire au controleur
        $.ajax({
            type : "POST",
            //chargement du controleur
            url : 'gestionForm.php',
            //passage des données au controler
            data : {dataJson: test123},
            dataType: "json",
            success: function(response) {
                console.log(response); 
            }
        })
    });

Hi, here is my ajax request where I send a json structure : "test123" to the page : gestionForm, There is gestionForm :

$data = $_POST["dataJson"];
if(isset($_POST['submitForm'])){
    $obj = new Form($_POST['nomForm'], $data);
    $obj->getIDPage($_POST['nomPage']);
    $obj->addForm();
}

And I get the error : Undefined array key "dataJson"

Can someone help me pls?

  • 1
    try `var_dump($_POST)` before your `if` statement and show us what you get. – ciekals11 Jun 28 '21 at 12:44
  • test123 is a json structure – Louis Jarrier Jun 28 '21 at 12:44
  • Does this answer your question? [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) – CBroe Jun 28 '21 at 12:45
  • with var_dump($_post) i get : array(3) { ["nomForm"]=> string(7) "azeadza" ["nomPage"]=> string(16) "Bloc Opératoire" ["submitForm"]=> string(7) "envoyer" } so I don't see dataJson – Louis Jarrier Jun 28 '21 at 12:46
  • 1
    `$('.addFormForm').submit(function(){ ..ajax ..` is that *all* that's in `.addFormFrom` submit? Looks like your FORM is POSTing because you've not cancelled the submit. Add `return false;` as the last line, after (and outside) `$.ajax` – freedomn-m Jun 28 '21 at 12:48
  • 1
    try `$('.addFormForm').submit(function(event){ event.preventDefault(); //... rest }` – ciekals11 Jun 28 '21 at 12:49
  • `test123 is a json structure` ... you mean it's a string in JSON format? Or a JavaScript object? It's unclear. If it's JSON, then send the whole request as JSON...don't wrap JSON inside form-url-encoded data (which is what jQuery will transform your data object to), that's just nonsensical. But if it's actually still an object, them stop referring to it as JSON, because it isn't JSON. – ADyson Jun 28 '21 at 13:40

1 Answers1

-1

You'll need to pass the values in gestionForm.php as Array

$output = array
    (
     //your values
    );

echo json_encode($output);

And echo it as output... Note: In AJAX, use the following parameters...

    dataType:"JSON",
    success:function(data)

Hope this helps (:

Harsh
  • 100
  • 1
  • 10