By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,691 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,691 IT Pros & Developers. It's quick & easy.

Form submission code is not executing in proper order

100+
P: 190
hi ,

A strange error is coming , when i run a php file , all the php and java script executes before submit only which actually should execute after clicking on submit button and performing some checks.what to do ?
Dec 31 '08 #1
Share this Question
Share on Google+
5 Replies


P: 15
Hi, please explain your problem a little more.
Dec 31 '08 #2

100+
P: 190
I have written a file , in which i am creating a form to create new user .On submit i am checking for duplicate username in database and if username already existing i come back to form and if does't exist then accepting user name create a new user.

But the problem is that as soon as i acces url what ever php i have written is getting executed before displaying form objects . And because the form are empty fist time , there is no data fir queries , mysqo_numrows() is throwing warning before only.

how to rectify it ?
Dec 31 '08 #3

Atli
Expert 5K+
P: 5,058
Hi.

We would need to see the part of the code that is causing the problem to be able to say anything for sure.

But I will say this.
PHP is a server side language. IT will ALWAYS be executed before the HTML reaches the browser. You can not execute PHP code using JavaScript events or any other client-side coding. (Except AJAX, but that is an entire different thing.)
Dec 31 '08 #4

100+
P: 190
here "user doesnot exist shoud appear on if i ma getting action=dup but its getting executed before only.....


Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Untitled Document</title>
  6. <script language="javascript">
  7. function IsEmail(strEmail)
  8. {
  9.     var status = false;
  10.     var strEmailAddress = new String(strEmail);    
  11.     var strEmailPattern = (/^([a-z0-9_\.\-])+\@(([a-z0-9_\.\-])+\.)+([a-z0-9]{2,4})+$/i);
  12.     if (strEmailAddress.search(strEmailPattern)==0)
  13.     {
  14.         status = true;
  15.     }
  16.     return status;
  17. }
  18.  
  19. function validate()
  20. {
  21. if(!(IsEmail(document.form1.email.value)))
  22. {
  23. alert("Please enter an exact email address.\n" );
  24.         return false;
  25. }
  26. else if(document.form1.username.value == '')
  27.     {
  28.         alert('Username can not be blank');
  29.         return false;
  30. }
  31. else if(document.form1.pass.value == '')
  32.     {
  33.     alert('Passward can not be blank');
  34.     return false;
  35.     }
  36. else if(document.form1.membership.value == '')
  37.     {
  38.     alert('Passward can not be blank');
  39.     return false;
  40.     }
  41.  
  42. else if(document.form1.email.value == '')
  43.     {
  44.     alert('E-mail ID can not be blank');
  45.     return false;
  46.     }
  47. else if(document.form1.tele.value == '')
  48.     {
  49.     alert('Teleno ID can not be blank');
  50.     return false;
  51.     }
  52. else
  53. {
  54. document.form1.method = 'GET';
  55. document.form1.action = 'c_user.php?action=add';
  56. document.form1.submit(); 
  57. }
  58. }
  59. </script>
  60. </head>
  61.  
  62. <body>
  63. <form id="form1" name="form1" method="GET" action="c_user.php" enctype="multipart/form-data">
  64.   <table width="48%" border="0" cellspacing="5" cellpadding="5">
  65.    <? if($_REQUEST["action"] == "dup");{ ?> 
  66.    <tr>
  67.       <td width="100%" colspan="3">Username already exist</td>
  68.  
  69.     </tr><? }?>
  70.     <tr>
  71.       <td width="34%">Username</td>
  72.       <td width="10%"><span class="style2">*</span></td>
  73.       <td colspan="2"><label>
  74.         <input type="text" name="username"  />
  75.       </label></td>
  76.     </tr>
  77.     <tr>
  78.       <td>Passward</td>
  79.       <td><span class="style2">*</span></td>
  80.       <td colspan="2"><input type="passward" name="pass" /></td>
  81.     </tr>
  82.     <tr>
  83.       <td>First Name </td>
  84.       <td>&nbsp;</td>
  85.       <td colspan="2"><input type="text" name="fname" /></td>
  86.     </tr>
  87.     <tr>
  88.       <td>Last Name </td>
  89.       <td>&nbsp;</td>
  90.       <td colspan="2"><input type="text" name="lname" /></td>
  91.     </tr>
  92.     <tr>
  93.       <td>Tele No </td>
  94.       <td><span class="style2">*</span></td>
  95.       <td colspan="2"><input type="text" name="tele" /></td>
  96.     </tr>
  97.     <tr>
  98.       <td>Address</td>
  99.       <td>&nbsp;</td>
  100.       <td colspan="2"><input type="text" name="add" /></td>
  101.     </tr>
  102.     <td>Country</td>
  103.       <td>&nbsp;</td>
  104.       <td width="25%"><label>
  105.         <select name="country" id="country">
  106.           <option value="India">India</option>
  107.           <option value="Other">Other</option>
  108.         </select>
  109.       </label></td>
  110.       <td width="31%"><label>
  111.         <input name="Ocountry" type="text" id="Ocountry" />
  112.       </label></td>
  113.     <tr>
  114.       <td>City</td>
  115.       <td>&nbsp;</td>
  116.       <td colspan="2"><input type="text" name="city" /></td>
  117.     </tr>
  118.     <tr>    </tr>
  119.     <tr>
  120.       <td>Occupation</td>
  121.       <td>&nbsp;</td>
  122.       <td colspan="2"><input name="occu" type="text" id="occu" /></td>
  123.     </tr>
  124.     <tr>
  125.       <td>Company</td>
  126.       <td>&nbsp;</td>
  127.       <td colspan="2"><input type="text" name="company" /></td>
  128.     </tr>
  129.     <tr>
  130.       <td>Membership Plan </td>
  131.       <td><span class="style2">*</span></td>
  132.       <td colspan="2"><label>
  133.         <select name="membership" id="membership">
  134.           <option value="Free" selected="selected">Free</option>
  135.           <option value="ProI">Pro Indvidual</option>
  136.           <option value="ProA">Pro Agent</option>
  137.           <option value="ProS">Super Pro</option>
  138.         </select>
  139.       </label></td>
  140.     </tr>
  141.     <tr>
  142.       <td>Email</td>
  143.       <td><span class="style2">*</span></td>
  144.       <td colspan="2"><input name="email" type="text" id="email" /></td>
  145.     </tr>
  146.  
  147.     <tr>
  148.       <td>&nbsp;</td>
  149.       <td>&nbsp;</td>
  150.       <td colspan="2"><label>
  151.         <div align="right">
  152.           <input type="button" name="Submit" value="Submit" onclick="validate();" />
  153.         </div>
  154.       </label></td>
  155.     </tr>
  156.   </table>
  157.  
  158. </form>
  159. </body>
  160. </html>
  161.  
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("conn.php");
  3. if($_REQUEST["action"] == "add"); 
  4. {
  5. $username = $_GET["username"];
  6. $passward = $_REQUEST["pass"];
  7. $fname = $_REQUEST["fname"];
  8. $lname = $_REQUEST["lname"];
  9. $teleno = $_REQUEST["tele"];
  10. $email = $_REQUEST["email"];
  11. $company = $_REQUEST["company"];
  12. $add = $_REQUEST["add"];
  13. $country = $_REQUEST["add"];
  14. $city = $_REQUEST["add"];
  15. $occu = $_REQUEST["occu"];
  16. $membership = $_REQUEST["membership"];
  17. $q = "select u_name from my_user where u_name ='".$username."'";
  18.  
  19. if(!mysql_query($q))
  20. {
  21. die("error".mysql_error());
  22. }
  23. $r = mysql_query($q);
  24. $rows = mysql_numrows($r);
  25. if($rows <= 0)
  26. {
  27. $query = "insert into my_user values('','$username' , '$passward', '$fname' , '$lname' , '$tele' ,'$add','$city','$country','$occu','$company','$membership',CURDATE(),'$email')";
  28. if(!mysql_query($query))
  29. {
  30.  
  31. die("error".mysql_error());
  32. }
  33. $to = $email;
  34. $subject = "Your Registration At  Classifieds Guide India";
  35. $message = "Dear ".$fname;
  36. $message .= "\n\tThankyou For Registring In Classifieds .You will shortly receive a confirmation call to get to know about payment details at your give telephone number :".$tele.".Plese be ready with details.\n\t\tThankyou\nClassifieds .";
  37. $from = "xyz@yahoo.com";
  38. $headers = "From: $from";
  39. mail($to,$subject,$message,$headers);
  40. header("Location:create_user.php");
  41. }
  42. if($rows > 0 )
  43. {
  44.  
  45. header("Location:create_user.php?action=dup");
  46. }
  47. }
  48. ?>
  49.  
Jan 9 '09 #5

Atli
Expert 5K+
P: 5,058
Line #65 of the first code. There is an extra semi-colon after the if statement, before the code block, which means the code block will always get executed regardless of how the if statement was executed.

You should also consider changing the form method to POST.
Sending all that data on the GET protocol is just not sanitary :)
Jan 9 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.