Hi,I am new to jquery n using below code for submitting data in database.
Its working fine for small data but not working for large data.
Need suggestions
Php code:

<?php
include('db.php');
if($_POST)
{
$name=$_POST['name'];
$desc=$_POST['desc'];
$price=$_POST['price1'];
//$type=$_POST['type'];
mysql_query("INSERT INTO `pgdetail` (`name`, `desc`, `price`) VALUES ('$name', '$desc', '$price')");
}

?>
$(function() {
$(".submit").click(function() {

    var name = $("#name").val();
	var desc = $("#desc").val();
	var price1 = $("#price1").val();
    var dataString = 'name='+ name + '&desc=' + desc + '&price1=' + price1;
    //document.write(name);
    
	if(name=='' || desc=='' || price1=='' )
	{
	$('.success').fadeOut(200).hide();

    $('.error').fadeOut(200).show();	
	}
		
	else
	{
	//alert('hi');
	$.ajax({
	type:"POST",
    url:"addpg.php",
    data:dataString,
    success: function(){
	$('.success').fadeIn(200).show();
    $('.error').fadeOut(200).hide();
    $('#name').val('');
    $('#desc').val('');
    $('#price').val('');
 

		
   }
});

	}
		

    return false;
	});

});
</script>

Recommended Answers

All 9 Replies

what is your definition of large data?

what is your definition of large data?

large data: more then 10 lines of character
small data :single line

Try to encode the string. JavaScript doesn't like quotes, ampersand or others alpha numeric characters that are use to suggest the closing of a variable or a function in JavaScript.

escape($('#desc').val());

Then when you retrieve the strings in the addpg.php make sure to use

stripslashes($_POST['desc']);

if you want to echo the information back, but you can leave it like that to send it to the database, if you use addslashes() will add double slashes. I least by my experience with the same problem.

Hope this help!

Everything seems fine with your jqeury scripts and all the information passes perfectly.

If you still haven't solved this, I would suggest to check your server script and in particular your database insert statement. Most likely you are trying to pass a value that the database doesn't accept and therefore doesn't insert anything.

Add this to your success function:
success: function(msg){
alert(msg);

And add this to your query and try again:

mysql_query("INSERT INTO `pgdetail` (`name`, `desc`, `price`) VALUES ('$name', '$desc', '$price')") or die(mysql_error());

it should tell you if there is a mistake with the insert statement

Thanks !! raphie

I have one query also.I have a registration form where i am checking whether the given(by user) email is exist or not.
after googling found some code:
This code is working fine.but i have other validations also.like email format,password same,& empty fields.
want to work my form in proper sequence
i.e.
It checks empty condition,email format,then email availability.
Need help/suggestions

$(document).ready(function()
{ 

	$("#username").blur(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		//check the username exists or not from ajax
		$.post("user_availability.php",{ username:$(this).val() } ,function(data)
        {
		  if(data=='no') //if username not avaiable
		  {
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('This email Already exists').addClass('messageboxerror').fadeTo(900,1);
			  $("#password").attr("disabled", "disabled");
			  $("#rpassword").attr("disabled", "disabled");
			  $("#mobile").attr("disabled", "disabled");
			  $("#city").attr("disabled", "disabled");
			  $("#name").attr("disabled", "disabled");
			  $("#type").attr("disabled", "disabled");
			});		
          }
		  else
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Email available to register').addClass('messageboxok').fadeTo(1000,1);
			});
		  }
				
        });
 
	});
});
$(".submit").click(function() {

    var name = $("#name").val();
	var username = $("#username").val();
	var password = $("#password").val();
	var type = $("#type").val();
	var mobile=	$("#mobile").val();
	var city=$("#city").val();
	var rpassword=$("#rpassword").val();
    var dataString = 'name='+ name + '&username=' + username + '&password=' + password + '&rpassword=' + rpassword+ '&type=' + type + '&mobile=' + mobile + '&city=' + city;
	if(name=='' || username=='' || password=='' || rpassword=='' || type=='' || city=='' || mobile=='')
	{
	$('.success').fadeOut(200).hide();
    $('.error').fadeOut(200).show();
    $('.pwd_error').fadeOut(200).hide();
    $('.error1').fadeOut(200).hide();
    $('#msgbox').fadeOut(200).hide();
    $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
	{ 
	  //add message and change the class of the box and start fading
	  $(this).html('Email available to register').addClass('messageboxok').fadeTo(1000,1).hide();
	});
	}
	
	else if( !isValidEmailAddress(username) ) 
	  { 
	   $('.success').fadeOut(200).hide();
       $('.error').fadeOut(200).hide();
       $('.error1').fadeOut(200).show();
       $('.pwd_error').fadeOut(200).hide();
       $('#msgbox').fadeOut(200).hide();
       $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
	{ 
	  //add message and change the class of the box and start fading
	  $(this).html('Email available to register').addClass('messageboxok').fadeTo(1000,1).hide();
	});
	  }
	  
	else if(!(password==rpassword))
	{
	 $('.success').fadeOut(200).hide();
     $('.error').fadeOut(200).hide();
     $('.pwd_error').fadeOut(200).show();
     $('.error1').fadeOut(200).hide();
     $('#msgbox').fadeOut(200).hide();
  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
	{ 
	  //add message and change the class of the box and start fading
	  $(this).html('Email available to register').addClass('messageboxok').fadeTo(1000,1).hide();
	});

	}
	else
	{
	$.ajax({
	type:"POST",
    url:"join.php",
    data:dataString,
    success: function(){
	$('.success').fadeIn(200).show();
    $('.error').fadeOut(200).hide();
    $('.pwd_error').fadeOut(200).hide();
    $('.error1').fadeOut(200).hide();
    $('#msgbox').fadeOut(200).hide();

    $('#name').val('');
    $('#type').val('');
    $('#password').val('');
    $('#rpassword').val('');
    $('#username').val('');
    $('#mobile').val('');
    $('#city').val('');
		
   }
});

	}
		
    return false;
	});

});

Try to encode the string. JavaScript doesn't like quotes, ampersand or others alpha numeric characters that are use to suggest the closing of a variable or a function in JavaScript.

escape($('#desc').val());

Then when you retrieve the strings in the addpg.php make sure to use

stripslashes($_POST['desc']);

if you want to echo the information back, but you can leave it like that to send it to the database, if you use addslashes() will add double slashes. I least by my experience with the same problem.

Hope this help!

To validate empty values in jQuery you can use:

emailVal = $('#email').val();

if(emailVal.length == 0){
    alert('Email value is empty');
    error = 1;
}

This validation has to be done before any other process so you need to close your code among:

if(error == 0){
   //The code to execute if validation is true;
}

Also you can create a function to validate the form if the function return true then execute the jQuery function. Example:

function validateFields(formName){
    //get the elements of the form
    form = document.form[formName].elements;
    lenght = form.length;
    for(i=0;i<length;i++){
       fieldname = form[i].name;
       value = form[i].value;
       if(fieldname.indexOf('email') > 0 && value == ''){
           alert(fieldName+' is empty!');
           return false;
           break;
       }
    }
    return true;
}

Then you can make a list of fields to validate in the function.

Hope this helps!

I want to check whether the username(here email)exists or not.
it checks from the database.
My problem is i want to check email(whether in database or not) after the empty & email format validations
http://www.servicesonclick.com/pghostels11/default.php

<?php
include('db.php');
$user_name=$_POST['username'];
$result = mysql_query("SELECT * FROM pg_reg where email='$user_name'");

if(mysql_num_rows($result)>0)
{
	//user name is not availble
	echo "no";
} 
else
{
	//user name is available
	echo "yes";
}
?>

To validate empty values in jQuery you can use:

emailVal = $('#email').val();

if(emailVal.length == 0){
    alert('Email value is empty');
    error = 1;
}

This validation has to be done before any other process so you need to close your code among:

if(error == 0){
   //The code to execute if validation is true;
}

Also you can create a function to validate the form if the function return true then execute the jQuery function. Example:

function validateFields(formName){
    //get the elements of the form
    form = document.form[formName].elements;
    lenght = form.length;
    for(i=0;i<length;i++){
       fieldname = form[i].name;
       value = form[i].value;
       if(fieldname.indexOf('email') > 0 && value == ''){
           alert(fieldName+' is empty!');
           return false;
           break;
       }
    }
    return true;
}

Then you can make a list of fields to validate in the function.

Hope this helps!

I want to check whether the username(here email)exists or not.

I kind of lost here, you want to process the information, if the username is in the database then you want to return the result to the jQuery?

if so then you need to add a line to the last part of the code in the success function:

$.get("db.php", { username: $('#username').val();},
   function(data){
     alert("Username: " + data);
   });

in other worlds you need to do an additional ajax request as far as jQuery that I know of. But the php sample you gave is right.

You can find more information in here:

jQuery.get();
jQuery.load();

Thanks !! raphie

I have one query also.I have a registration form where i am checking whether the given(by user) email is exist or not.
after googling found some code:
This code is working fine.but i have other validations also.like email format,password same,& empty fields.
want to work my form in proper sequence
i.e.
It checks empty condition,email format,then email availability.
Need help/suggestions

Don't do final validation on JS, it can be easily bypassed. You can do empty variable checks and maybe other but make sure you do final validations at Server side! That is a security note :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.