943,169 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 95
  • PHP RSS
Sep 2nd, 2010
0

Validation of data

Expand Post »
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 Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3.  
  4. include("conpb.php");
  5.  
  6. $sql="SELECT * FROM code_numbers WHERE active=1 AND code_value ='".$_POST['user_name']."'";
  7.  
  8. //echo $sql;
  9.  
  10. $result=mysql_query($sql,$conn);
  11.  
  12. if (!$result) {
  13. echo($query);
  14. die(' Invalid query: ' . mysql_error());
  15. }
  16. $idProm=mysql_fetch_array($result);
  17.  
  18. //echo $idProm;
  19.  
  20. $user_name=$_POST['otro'];
  21.  
  22. if (in_array($user_name, $idProm))
  23. {
  24. //user name is not availble
  25. echo "no";
  26. }
  27. else
  28. {
  29. //user name is available
  30. echo "yes";
  31.  
  32. }
  33.  
  34.  
  35. ?>

Form:
PHP Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3. include("conpb.php");
  4. ?>
  5.  
  6.  
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  8. <html xmlns="http://www.w3.org/1999/xhtml">
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  11. <title>Code system</title>
  12. <script src="js/jquery.js" type="text/javascript" language="javascript"></script>
  13. <script language="javascript">
  14. //<!---------------------------------+
  15. // Developed by Roshan Bhattarai
  16. // Visit http://roshanbh.com.np for this script and more.
  17. // This notice MUST stay intact for legal use
  18. // --------------------------------->
  19. $(document).ready(function()
  20. {
  21. $("#username").blur(function()
  22. {
  23. //remove all the class add the messagebox classes and start fading
  24. $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
  25. //check the username exists or not from ajax
  26. $.post("user_availability.php",{ user_name:$(this).val() } ,function(data)
  27. {
  28. if(data=='no') //if code is not avaiable
  29. {
  30. $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
  31. {
  32. //add message and change the class of the box and start fading
  33. $(this).html('<img src="images/no.png" />').addClass('messageboxerror').fadeTo(900,1);
  34. });
  35. }
  36. else
  37. {
  38. $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
  39. {
  40. //add message and change the class of the box and start fading
  41. $(this).html('<img src="images/yes.png" />').addClass('messageboxok').fadeTo(900,1);
  42. });
  43. }
  44.  
  45. });
  46.  
  47. });
  48. });
  49. </script>
  50.  
  51.  
  52. </head>
  53.  
  54. <body>
  55.  
  56. <div align="center" style="padding:20px;">
  57.  
  58. <div style="border:1px solid #CCC; padding:20px; width:500px;">
  59.  
  60. <form id="codsys_01" class="appnitro" name="codsys_01" method="post" action="index2.php">
  61.  
  62. <?php
  63.  
  64. // data comes by default via post
  65.  
  66. if(!isset($_POST['code']))
  67.  
  68. {
  69.  
  70. $_POST['username']="";
  71.  
  72. $_POST['client_name']="";
  73.  
  74. $_POST['client_email']="";
  75.  
  76.  
  77. }
  78.  
  79. ?>
  80.  
  81. <div style="float:left; width:50%;">Code</div>
  82. <div style="float:left; width:50%;"><input id='username' name='username' type="text" value="<?php echo $_POST['username']; ?>" />
  83.  
  84. <span id="msgbox" style="display:none"></span>
  85.  
  86. </div>
  87.  
  88.  
  89. <div style="float:left; width:50%;">Name:</div>
  90. <div style="float:left; width:50%;"><input id='client_name' name='client_name' type="text" value="<?php echo $_POST['client_name']; ?>" /></div>
  91.  
  92.  
  93. <div style="float:left; width:50%;">e-Mail</div>
  94. <div style="float:left; width:50%;"><input id='client_email' name='client_email' type="text" value="<?php echo $_POST['client_email']; ?>" /></div>
  95.  
  96.  
  97. <div style=" clear:both;"></div>
  98. <div><input id="saveForm" class="button_text" type="submit" value='Send'></div>
  99.  
  100. </form>
  101.  
  102. </div>
  103.  
  104. </div>
  105.  
  106. </body>
  107. </html>

Answer + store data in the DB

PHP Syntax (Toggle Plain Text)
  1.  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  6. <title>Documento sin t&iacute;tulo</title>
  7. </head>
  8.  
  9. <body>
  10.  
  11. <div>Hello world!</div>
  12.  
  13. <div>
  14.  
  15. <?php
  16.  
  17. include("conpb.php");
  18.  
  19. if(isset($_POST['client_name']))
  20. {
  21. include("conpb.php");
  22.  
  23. $clientIP = getClientIP();
  24.  
  25. $userAgent = getUserAgent();
  26.  
  27.  
  28. $sql="INSERT INTO client_data (fk_id_cd, client_name, client_email, ip, agent)
  29. VALUES ('".$_POST['fk_id_cd']."','".$_POST['client_name']."','".$_POST['client_email']."','".$clientIP."','".$userAgent."')";
  30.  
  31.  
  32. $result=mysql_query($sql,$conn);
  33. //echo $sql;
  34. if(!$result)
  35. {
  36. echo "Error".mysql_error();
  37. }
  38. else
  39. {
  40. echo "Well Done";
  41. }
  42.  
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. function getClientIP() {
  51.  
  52. if ($_SERVER) {
  53.  
  54. if ($_SERVER[HTTP_X_FORWARDED_FOR]) {
  55.  
  56. $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  57.  
  58. }
  59.  
  60. else {
  61.  
  62. if ( $_SERVER["HTTP_CLIENT_IP"] ) {
  63.  
  64. $ip = $_SERVER["HTTP_CLIENT_IP"];
  65.  
  66. }
  67.  
  68. else {
  69.  
  70. $ip = $_SERVER["REMOTE_ADDR"];
  71.  
  72. }
  73.  
  74. }
  75.  
  76. }
  77.  
  78. else {
  79.  
  80. if ( getenv( "HTTP_X_FORWARDED_FOR" ) ) {
  81.  
  82. $ip = getenv( "HTTP_X_FORWARDED_FOR");
  83.  
  84. }
  85.  
  86. else {
  87.  
  88. if ( getenv( "HTTP_CLIENT_IP" ) ) {
  89.  
  90. $ip = getenv( "HTTP_CLIENT_IP");
  91.  
  92. }
  93.  
  94. else {
  95.  
  96. $ip = getenv( "REMOTE_ADDR");
  97.  
  98. }
  99.  
  100. }
  101.  
  102. }
  103.  
  104.  
  105.  
  106. return $ip;
  107.  
  108. }
  109.  
  110.  
  111.  
  112. function getUserAgent() {
  113.  
  114. $userAgent = $_SERVER["HTTP_USER_AGENT"];
  115.  
  116.  
  117.  
  118. return $userAgent;
  119.  
  120. }
  121.  
  122.  
  123. ?>
  124.  
  125. </div>
  126.  
  127. </body>
  128. </html>


Thanks for any help!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wendeus is offline Offline
1 posts
since Sep 2007
Sep 2nd, 2010
0
Re: Validation of data
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
Reputation Points: 10
Solved Threads: 2
Newbie Poster
CyberSpatium is offline Offline
9 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: sending variables from a form with an array
Next Thread in PHP Forum Timeline: user permission layout





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC