0

Good morning everyone,

I am a kind of newbie concerning Ajax. I have already a php script which works. I know that my php needs to evolve with PDOs but it is another problem. But i want to implement ajax to avoid the reloading and because i want to learn it.

Php script:

<?php 

session_start();

require "connect.php";

$firstName  = $_POST['regFirstName']; 
$lastName  = $_POST['regLastName']; 
$companyName = $_POST['regCompanyName']; 
$email = $_POST['regEmail'];
$password = $_POST['regPassword'];
$confirmPwd = $_POST['regConfirmPwd'];    

$add = mysqli_query($con,"INSERT INTO user (firstName, lastName, companyName, email, password) VALUES ('$firstName' , '$lastName', '$companyName', '$email', '$password') ") or die("Can't  Insert! ");
mail($email, "Your registration has been successful, please note your registration details\n\n ID: $email\n\n Password: $password\n\n" ); 
$success = "Successful! <a href='../login.php'> Click Here </a> to log In.";
echo($success); 

And below the ajax script:

$(document).ready(function(){
    $("#regForm").submit(registration);

            function registration(){

                var regData = $("#regForm").serialize();

                $.ajax({
                    url: "regScript.php",
                    type: "POST",
                    data: regData,
                    success: function(){
                        console.log("ok");
                    },

                    error: function(){
                        console.log("regData");
                    }
                });      
    });

And the form:

<form id="regForm" name="regForm">
    <div>
        <input type="text" id="regFirstName" name="regFirstName">
    </div>

    <div>
        <input type="text" id="regLastName" name="regLastName">
    </div>

    <div>
        <input type="text" id="regCompanyName" name="regCompanyName">
    </div>

    <div>
        <input type="email" id="regEmail" name="regEmail">
    </div>

    <div>
        <input type="password" id="regPassword" name="regPassword">
    </div>

    <div>
        <input type="password" id="regConfirmPwd" name="regConfirmPwd">
    </div>
    <br>
    <div id="errorWindowRegister">
    </div>

    <br>

    <button type="reset" name="reset" id="rstRegister" href="#">Reset</button>
    <button type="submit" name="submit" id="regButton">Register</button>
</form>

I don't know how to retrieve the data from Ajax to Php. The Jquery code seems to work but the data is not well sent to Php.

How to retrieve the data with Ajax and send it to Php?

Regards, E

Emile
  • 345
  • 2
  • 11
  • 1
    what is the error message. Please post the error message. – Altaf Hussain Mar 20 '14 at 09:57
  • 1
    Submiting a FORM redirect user, you need to prevent this behaviour. Search for it and you will find thousand of available examples – A. Wolff Mar 20 '14 at 10:00
  • What do you get when you do console.log(regData)? – lshettyl Mar 20 '14 at 10:28
  • it displayed this :[object DeadObject] – Emile Mar 20 '14 at 10:33
  • can you expand the object in your console by clicking on the arrow on the left and see what you got? – lshettyl Mar 20 '14 at 10:35
  • [Object { name="regFirstName", value=""}, Object { name="regLastName", value=""}, Object { name="regCompanyName", value=""}, Object { name="regEmail", value=""}, Object { name="regPassword", value=""}, Object { name="regConfirmPwd", value=""}] Although it is the same if i insert values. – Emile Mar 20 '14 at 10:39
  • So, it's not ajax that's not sending the data. Try .serialize() instead of .serializeArray() and see if that makes a difference. – lshettyl Mar 20 '14 at 10:48
  • A test: http://jsfiddle.net/lshettyl/2shvm/ i tried works fine though. – lshettyl Mar 20 '14 at 11:06
  • Now the error is "regFirstName=&regLastName=&regCompanyName=&regEmail=&regPassword=&regConfirmPwd=" – Emile Mar 20 '14 at 11:48
  • I'm agree with the use of serialize. Anyway, can you edit and show your last code with the place where you put console.log() please. – Debflav Mar 20 '14 at 12:24
  • I updated the code, still an error =( – Emile Mar 20 '14 at 14:28
  • I've read again and I don't see where you print : "regFirstName=&regLastName=...". And you say that it's fill when submit the form so it's normal. The "ok" is not print in the console ? – Debflav Mar 20 '14 at 14:54
  • Yes, only error is displayed with a console.log of my data – Emile Mar 20 '14 at 15:01
  • I am sorry Debflav, actually no error is displayed anymore in my console. But it seems that Ajax does not send data to php. I checked the paths in my php script and all is ok. I think it is due to Ajax because if i don't use it, the php script works (in using the form action) – Emile Mar 20 '14 at 15:44
  • You haven't updated your current code... In any case, I've updated my answer. My last answer because I've no more idea. Hope it will be helpful. – Debflav Mar 20 '14 at 18:45

4 Answers4

1

You forgot to add a semicolon in your script. Anyway you don't stop the event when sending the form. I think you would have another problem.

$(document).ready(function(){
    $("#regForm").submit(registration);
    function registration(){
        var regData = $("#regForm").serializeArray();
        $.ajax({
           //...
        })//; Semi-colon is missing here
    }) // Edit: And here extra ')' ?
});

Add your console output if you have any errors, please.


Edit

If the Ajax request is the problem, the following should solve your problem. I can't see the modification you already made. But I'm sur at 99% that sample will be helpful.

$(document).ready(function(){
    $("#regForm").submit( function(e) {

        e.preventDefault(); // Avoid page reload
        var regData = $("#regForm").serialize();

        $.ajax({
            url: "regScript.php",
            type: "POST",
            data: regData,
            dataType: "html", // You want retrieve html (I guess it's the default but it's better to be explicit)
            success: function(myReturnedHtml){
               console.log(myReturnedHtml); // Put the result where you want
            }
            //... 
        });
    }); 
});
Debflav
  • 1,131
  • 7
  • 17
  • Thanks man but I corrected it and i have still not data sent. – Emile Mar 20 '14 at 10:27
  • As I said we need your output error (see in developer tool) and I guess you need to stop form submission with [preventDefault](http://api.jquery.com/event.preventdefault/) – Debflav Mar 20 '14 at 10:28
  • The console error which is displayed is [object DeadObject] – Emile Mar 20 '14 at 10:36
0

What's that url is regScript in $.ajax call. It has to be regScript.php right.?

arthankamal
  • 6,341
  • 4
  • 36
  • 51
0

Your code should be this in the Ajax block

url: "regScript.php",
type: "POST",
data: "regData="+regData
BlackPearl
  • 2,532
  • 3
  • 33
  • 49
0
    $(document).ready(function(){
        $("#regButton").click(function()
    {
             var regData = $("#regForm").serializeArray();
            $.ajax({
                url: "regScript.php",
                type: "POST",
                data: regData,
                success: function(){
                    console.log("OK");
                },
                error: function(){
                    console.log("NOT OK");
                }
            })  ;    
    });
   });

HTML

 <input type="button" name="submit" id="regButton" value="Register">
Chetan Gawai
  • 2,361
  • 1
  • 25
  • 36