Duncan
Duncan

Reputation: 125

jQuery.post() breaks when more than 10 variables are specified in the data string

Hey guys, I'm having an issue which I hope is easy to resolve. Currently I'm using this code to send variables from a form to PHP. It works as long as I have less than 10 variables, but as soon as I have 10+, I get an error in my PHP script. Here is my .post() code:

$.ajax({
            type: "POST",
            url: "ajax.php",
            data: "&email="+ email + "&fname="+ fname + "&lname="+ lname + "&password="+ password + "&groupname="+ groupname + "&productkey="+ productkey + "&company="+ company + "&position="+ position + "&sector="+ sector, 

                success: function(){
                    $('form#submit').hide();
                    //$('form#submit :input').val("");
                     $('div.success').fadeIn();
                }
    });

It seems somewhat inefficient to me, which could be why it's breaking as soon as I add the tenth variable. I'm at a bit of a loss. Any thoughts? If needed, I can provide a zip of the form with all of the files.

Cheers.

Here is the main content of the ajax.php file:

  $fname        = htmlspecialchars(trim($_POST['fname']));  
$lname        = htmlspecialchars(trim($_POST['lname']));  
$email        = htmlspecialchars(trim($_POST['email']));  
$password     = md5(trim($_POST['password']));  
$groupname    = htmlspecialchars(trim($_POST['groupname']));  
$productkey   = htmlspecialchars(trim($_POST['productkey']));   
$company      = htmlspecialchars(trim($_POST['company']));   
$position     = htmlspecialchars(trim($_POST['position']));   
$sector       = htmlspecialchars(trim($_POST['sector']));  


$addClient  = "INSERT INTO tbl_user (fname,lname,email,password,groupname,productkey,company,position,sector) VALUES ('$fname','$lname','$email','$password','$groupname','$productkey','$company','$position','$sector')";  
mysql_query($addClient) or die(mysql_error());  

Upon further troubleshooting, I'm still not 100% confident where the problem was, however I do believe that the way of passing data in the accepted solution is better than the way I was trying to do it before. There may have also been an issue in my mySQL database where I needed to delete a column and re-create it before it operated correctly. Thanks everyone - hope I didn't waste your time

Upvotes: 1

Views: 219

Answers (1)

muirbot
muirbot

Reputation: 2081

try just passing your data as a simple object:

var data = {"email": email, "fname": fname, ....}
$.ajax({
  type: "POST",
  url:  "ajax.php",
  data: data,
  ...
})

maybe that will help :/

Upvotes: 1

Related Questions