Hi everybody,

I wanna make a simple leads form in php, this form has three fields.
the first field is "code", the second one is Name and third is e-mail.
the issue is that i have two tables in my database and i want in one hand store all this information in one table---that's pretty easy...but first I wanna validate the code against another table with some pre-charged codes....the idea is try to make a pre-validation of this codes before the client send the form. I think the best way to do that is with an ajax validation but i can acces the data base through ajax....somebody can help me?

this is my code:

Validation query:

<?php

include("conpb.php");

		$sql="SELECT * FROM code_numbers WHERE active=1 AND code_value ='".$_POST['user_name']."'";
		
		//echo $sql;
		
		$result=mysql_query($sql,$conn);
		
		if (!$result) {
			echo($query);
			die(' Invalid query: ' . mysql_error());
		}
		$idProm=mysql_fetch_array($result);
		
		//echo $idProm;
		
		$user_name=$_POST['otro'];
		
		if (in_array($user_name, $idProm))
			{
			//user name is not availble
				echo "no";
			} 
			else
			{
				//user name is available
				echo "yes";

			}
			
			
?>

Form:

<?php 
include("conpb.php");
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Code system</title>
<script src="js/jquery.js" type="text/javascript" language="javascript"></script>
<script language="javascript">
//<!---------------------------------+
//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use
// --------------------------------->
$(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",{ user_name:$(this).val() } ,function(data)
        {
		  if(data=='no') //if code is 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('<img src="images/no.png" />').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
		  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('<img src="images/yes.png" />').addClass('messageboxok').fadeTo(900,1);	
			});
		  }
				
        });
 
	});
});
</script>


</head>

<body>

<div align="center" style="padding:20px;">

	<div style="border:1px solid #CCC; padding:20px; width:500px;">
    	
		<form id="codsys_01" class="appnitro" name="codsys_01" method="post" action="index2.php">
		
		<?php 

		// data comes by default via post

		if(!isset($_POST['code']))

			{

				$_POST['username']="";

				$_POST['client_name']="";

				$_POST['client_email']="";


			}

		?>
		
    		<div style="float:left; width:50%;">Code</div> 
        	<div style="float:left; width:50%;"><input id='username' name='username' type="text" value="<?php echo $_POST['username']; ?>" />
			
			<span id="msgbox" style="display:none"></span>
			
			</div>
        
        
        	<div style="float:left; width:50%;">Name:</div> 
        	<div style="float:left; width:50%;"><input id='client_name' name='client_name' type="text" value="<?php echo $_POST['client_name']; ?>" /></div>
        
        
        	<div style="float:left; width:50%;">e-Mail</div> 
        	<div style="float:left; width:50%;"><input id='client_email' name='client_email' type="text" value="<?php echo $_POST['client_email']; ?>" /></div>
        
		
        	<div style=" clear:both;"></div>
        	<div><input id="saveForm" class="button_text" type="submit" value='Send'></div>
			
        </form>
		
    </div>

</div>

</body>
</html>

Answer + store data in the DB

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
</head>

<body>

	<div>Hello world!</div>

	<div>
	
		<?php
		
		include("conpb.php");
		
		if(isset($_POST['client_name']))
			{
				include("conpb.php");
		
				$clientIP = getClientIP();

				$userAgent = getUserAgent();
				
				
				$sql="INSERT INTO client_data (fk_id_cd, client_name, client_email, ip, agent)
				VALUES ('".$_POST['fk_id_cd']."','".$_POST['client_name']."','".$_POST['client_email']."','".$clientIP."','".$userAgent."')";
				
				
				$result=mysql_query($sql,$conn);
				//echo $sql;
					if(!$result)
						{
							echo "Error".mysql_error();
						}
							else
						{
							echo "Well Done";
						}
				
				}
				
				




function getClientIP() {

	if ($_SERVER) {

		if ($_SERVER[HTTP_X_FORWARDED_FOR]) {

				$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];

		}

		else {	

			if ( $_SERVER["HTTP_CLIENT_IP"] ) {	

				$ip = $_SERVER["HTTP_CLIENT_IP"];

			}

   			else {

	   			$ip = $_SERVER["REMOTE_ADDR"];

	   		}		

		}

   }

   else {

	   if ( getenv( "HTTP_X_FORWARDED_FOR" ) ) {

		   $ip = getenv( "HTTP_X_FORWARDED_FOR");

		}

		else {

			if ( getenv( "HTTP_CLIENT_IP" ) ) {

				$ip = getenv( "HTTP_CLIENT_IP");	

			}

 			else {

	 			 $ip = getenv( "REMOTE_ADDR");

	 		}

 		}

	}

  

    return $ip;

}



function getUserAgent() { 

	$userAgent = $_SERVER["HTTP_USER_AGENT"];



	return $userAgent;

}


		?>
		
	</div>

</body>
</html>

Thanks for any help!

i suggest you use the Zend Framework Valuation Class. This class is heavly documented at the Zend website so even a newbie will be able to figure you how to use the class. This will also save you tons of coding time since you do not have to develop your own validation framework for your script.

Validation Class

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.