0

I was working with jQuery in the front end and php in the backend. I had some cross origin problem which I have fixed. but now I am filling up forms (sign up) but db is getting updated, not even empty row. Before I was getting some empty row in db ( I am sure about that, my memory is little hazy about that day! sorry!! ) I am unable to find any problem as errors or success message are not getting hit in jQuery file. I tried http://localhost/signup.php and its showing success message upon connecting to db. So I am sure that its connected to the proper db. I also tried to manual input using that php and i was able to import data as well.

I am adding the jQuery and php code below. I am using all this in XAMPP. Thanks in advance. :)

jQuery:

$("#userReg_btn").click(function(e) 
{
    //user registration in


    var array = [];
    var flag = false;

    var fullName = $("#uFn").val();
    var email = $("#uEa").val();
    var mobile = $("#uMn").val();
    var nID = $("#uNm").val();
    var zip = $("#uZc").val();
    var prof = $("#uPc").val();



    array.push(fullName);
        array.push(email);
        array.push(mobile);
        array.push(nID);
        array.push(zip);
        array.push(prof);

        alert(array);
        console.log(array);
    $.ajax({
            url:"http://localhost/signup.php",
            data:
            {
                fullName: array[0],
                email: array[1],
                mobile: array[2],
                nID: array[3],
                zip: array[4],
                prof: array[5],
            },
            type:"POST",
            dataType:"json",
            success:function(e){
                alert("in success");
                alert(e);
                console.log("success");
            },
            error:function(e)
            {
                alert("error is hit");
                alert(e);
                console.log(e);
            }
        });
     });

and php file:

<?php

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 2016 00:00:00 GMT');

header('Content-type: application/json');
     $con=mysql_connect("localhost","root", "");
     mysql_select_db("tester",$con);


$name = $_GET['fullName'];
$email= $_GET['email'];
$mobile= $_GET['mobile'];
$nID = $_GET['nID'];
$zip = $_GET['zip'];
$prof= $_GET['prof'];
$query="INSERT INTO user_reg(fullName,email,mobile,nID,zip,prof) VALUES('$name','$email', '$mobile','$nID','$zip','$prof');";

echo $name;

   if(mysql_query($query))
   {
       echo "success";
   }else{
   echo  mysql_error();
   }

//}

?>

Thanks in advance again :)

Ibtehaz
  • 49
  • 10
  • Do you see something in your Network profiler ? Does the AJAX request is sent ? – Ulti Apr 24 '15 at 18:09
  • How to do that? @Ulti – Ibtehaz Apr 24 '15 at 18:11
  • Which browser are you using ? Ctrl+Shift+J on Chrome for example. – Ulti Apr 24 '15 at 18:11
  • [You need to prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 24 '15 at 18:18
  • @JAyBlanchard, I dont work with db. Sorry :( I will pass it on. – Ibtehaz Apr 24 '15 at 18:20
  • You need to make the changes in *your* PHP code @Ibtehaz – Jay Blanchard Apr 24 '15 at 18:20

2 Answers2

2

I found a problem in your php script. In your AJAX call you are declaring the HTTP request as post. But in your php script you are using the $_GET method to look for the variable. Use $_POST.

In your PHP script:

$name = $_GET['fullName']

turns into

$name = $_POST['fullName'];

Do this for all your variables.

The reason why you were getting a blank row is because the variables were blank. By changing the method to $_POST your script will be able to find the variables

Andy
  • 91
  • 4
  • I have added the mentioned $_GET to $_POST changes in the php file and as well as inside the JS file. but I am still not getting any result. My db is not even updating empty row!!! – Ibtehaz Apr 24 '15 at 18:27
1

Your data object contains an extra ,, that shouldn't be there. Also, if you're sending json (note, I'm not sure you want to, but the headers suggest it):

data: JSON.stringify({
        fullName: array[0],
        email: array[1],
        mobile: array[2],
        nID: array[3],
        zip: array[4],
        prof: array[5]
}),

You will also need to set the contentType of the ajax post:

contentType: 'application/json',
Mackan
  • 6,200
  • 2
  • 25
  • 45
  • i have added datatype: json inside the AJAX code. do i need to add the contentType as well? – Ibtehaz Apr 24 '15 at 18:13
  • 1
    datatype is for the data you receive and contentType for the data you send – Mackan Apr 24 '15 at 18:19
  • I have changed the necessary changes u have mentioned and the changes of $_GET to $_POST in php as well. But still nothing is coming up. not even blank row. – Ibtehaz Apr 24 '15 at 18:26
  • Well, I'm not sure you WANT to send it using json to be honest. I don't know php, but I guess a $_POST will have trouble fetching a json object. Remove whatever I suggested (except the extra `,`, that should not be there), and try again :) – Mackan Apr 24 '15 at 18:27
  • Thanks did that. It worked :) data is uploading in db. – Ibtehaz Apr 24 '15 at 18:29
  • Glad it worked for you. Good catch by user4875837455 :) – Mackan Apr 24 '15 at 18:30