0

I use a form to get the email adress that will be recorded for the newsletter of my site. this form is managed by AJAX send and get the response :

$.ajax({
        url: "./control/newsletter/ct_newsletter_subscribe.php",
        type: "POST",
        data: $this.serialize(),
        success: function(html) {
            alert(html); // html contient l'id du dernier inscrit
            if(html == 'erreur'){
                var message = "<p>Erreur d'enregistrement en BDD</p>";
            }else if(html != 'erreur'){
                var message = "Adresse email enregistrée en BDD : id = "+html;
            }

I want the response (that get the lastInsertId) to be used in a second function to get the informations recorded.

In the file ct_newsletter_subscribe.php I get the lastInsertId but I cannot re-use it.

        $LastInsertId = add_email_newsletter($newsletterNom, $newsletterPrenom, $newsletterEmail, $newsletterGsm, $registered, $regdate, $codeConfirm);
        echo '<script>alert("ct newsletter subscribe L36 : '.$LastInsertId.'")</script>';// DEBUG
        $reponse = $LastInsertId;
        echo '<script>alert("ct newsletter subscribe L38 : '.$reponse.'")</script>';// DEBUG

In this part, $reponse and $LastInsertId contain the last id, but I cannot re-use it for another function. the 2 "echo ..." doesn't give the variable I want them to show I don't understand why.

tckmn
  • 57,719
  • 27
  • 114
  • 156
kobano
  • 489
  • 7
  • 12

1 Answers1

0

the form is managed by a .js with AJAX. AJAX send the $_POST to ct_newsletter_subscribe.php. In ct_newsletter_subscribe.php I call a function "add_email_newsletter" and his response $LastInsertId is correct.

Then I want to reuse $LastInsertId into another function :

    // First function works correctly :
    $LastInsertId = add_email_newsletter($newsletterNom, $newsletterPrenom, $newsletterEmail, $newsletterGsm, $registered, $regdate, $codeConfirm);
    echo '<script>alert("ct newsletter subscribe L36 : '.$LastInsertId.'")</script>';// DEBUG   -> $LastInsertId is empty
    $reponse = $LastInsertId; // this result is correct
    echo '<script>alert("ct newsletter subscribe L38 : '.$reponse.'")</script>';// DEBUG -> $reponse is empty

    if($reponse != 'erreur'){ // if no error appear in the first function
        echo '<script>alert("ct newsletter subscribe L42 : '.$reponse.'")</script>';// DEBUG
        //Récupération des données enregistrées :
        // Fonction container :
        include_once('../../model/newsletter/mo_newsletter_email_infos.php');
        //récupération des infos selon l'id retournée par $reponse :
        $retourNewsletterEmailInfos = get_newletter_email_infos($LastInsertId);

        $reponse .= ' & Fonction get_infos : '.$retourNewsletterEmailInfos[0]['email'];
     }
     echo $reponse;

Then in AJAX the "html" result is correctly the last id inserted.

But in PHP, $LastInsertId is empty for the function :

$retourNewsletterEmailInfos = get_newletter_email_infos($LastInsertId);

the debug line doesn't show any content in $reponse

echo '<script>alert("ct newsletter subscribe L42 : '.$reponse.'")</script>';// DEBUG
kobano
  • 489
  • 7
  • 12
  • The problem was in the first function add_email_newsletter . At the end I used "echo" that caused an unwanted answer in AJAX. I use now "return" that I can deal with properly. Thanksall for help ! – kobano Sep 03 '14 at 10:30