Form submission code is not executing in proper order

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shalini Bhalla
    New Member
    • Dec 2007
    • 190

    Form submission code is not executing in proper order

    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 ?
  • altonator
    New Member
    • Jan 2008
    • 15

    #2
    Hi, please explain your problem a little more.

    Comment

    • Shalini Bhalla
      New Member
      • Dec 2007
      • 190

      #3
      PHP is executing before html

      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 ?

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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.)

        Comment

        • Shalini Bhalla
          New Member
          • Dec 2007
          • 190

          #5
          See The Code an tell me whats wrong

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


          Code:
          <!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>Untitled Document</title>
          <script language="javascript">
          function IsEmail(strEmail)
          {
              var status = false;
              var strEmailAddress = new String(strEmail);    
              var strEmailPattern = (/^([a-z0-9_\.\-])+\@(([a-z0-9_\.\-])+\.)+([a-z0-9]{2,4})+$/i);
              if (strEmailAddress.search(strEmailPattern)==0)
              {
                  status = true;
              }
              return status;
          }
          
          function validate()
          {
          if(!(IsEmail(document.form1.email.value)))
          {
          alert("Please enter an exact email address.\n" );
                  return false;
          }
          else if(document.form1.username.value == '')
              {
                  alert('Username can not be blank');
                  return false;
          }
          else if(document.form1.pass.value == '')
              {
              alert('Passward can not be blank');
              return false;
              }
          else if(document.form1.membership.value == '')
              {
              alert('Passward can not be blank');
              return false;
              }
          
          else if(document.form1.email.value == '')
              {
              alert('E-mail ID can not be blank');
              return false;
              }
          else if(document.form1.tele.value == '')
              {
              alert('Teleno ID can not be blank');
              return false;
              }
          else
          {
          document.form1.method = 'GET';
          document.form1.action = 'c_user.php?action=add';
          document.form1.submit(); 
          }
          }
          </script>
          </head>
          
          <body>
          <form id="form1" name="form1" method="GET" action="c_user.php" enctype="multipart/form-data">
            <table width="48%" border="0" cellspacing="5" cellpadding="5">
             <? if($_REQUEST["action"] == "dup");{ ?> 
             <tr>
                <td width="100%" colspan="3">Username already exist</td>
                
              </tr><? }?>
              <tr>
                <td width="34%">Username</td>
                <td width="10%"><span class="style2">*</span></td>
                <td colspan="2"><label>
                  <input type="text" name="username"  />
                </label></td>
              </tr>
              <tr>
                <td>Passward</td>
                <td><span class="style2">*</span></td>
                <td colspan="2"><input type="passward" name="pass" /></td>
              </tr>
              <tr>
                <td>First Name </td>
                <td>&nbsp;</td>
                <td colspan="2"><input type="text" name="fname" /></td>
              </tr>
              <tr>
                <td>Last Name </td>
                <td>&nbsp;</td>
                <td colspan="2"><input type="text" name="lname" /></td>
              </tr>
              <tr>
                <td>Tele No </td>
                <td><span class="style2">*</span></td>
                <td colspan="2"><input type="text" name="tele" /></td>
              </tr>
              <tr>
                <td>Address</td>
                <td>&nbsp;</td>
                <td colspan="2"><input type="text" name="add" /></td>
              </tr>
              <td>Country</td>
                <td>&nbsp;</td>
                <td width="25%"><label>
                  <select name="country" id="country">
                    <option value="India">India</option>
                    <option value="Other">Other</option>
                  </select>
                </label></td>
                <td width="31%"><label>
                  <input name="Ocountry" type="text" id="Ocountry" />
                </label></td>
              <tr>
                <td>City</td>
                <td>&nbsp;</td>
                <td colspan="2"><input type="text" name="city" /></td>
              </tr>
              <tr>    </tr>
              <tr>
                <td>Occupation</td>
                <td>&nbsp;</td>
                <td colspan="2"><input name="occu" type="text" id="occu" /></td>
              </tr>
              <tr>
                <td>Company</td>
                <td>&nbsp;</td>
                <td colspan="2"><input type="text" name="company" /></td>
              </tr>
              <tr>
                <td>Membership Plan </td>
                <td><span class="style2">*</span></td>
                <td colspan="2"><label>
                  <select name="membership" id="membership">
                    <option value="Free" selected="selected">Free</option>
                    <option value="ProI">Pro Indvidual</option>
                    <option value="ProA">Pro Agent</option>
                    <option value="ProS">Super Pro</option>
                  </select>
                </label></td>
              </tr>
              <tr>
                <td>Email</td>
                <td><span class="style2">*</span></td>
                <td colspan="2"><input name="email" type="text" id="email" /></td>
              </tr>
             
              <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td colspan="2"><label>
                  <div align="right">
                    <input type="button" name="Submit" value="Submit" onclick="validate();" />
                  </div>
                </label></td>
              </tr>
            </table>
            
          </form>
          </body>
          </html>
          Code:
          <?php
          include("conn.php");
          if($_REQUEST["action"] == "add"); 
          {
          $username = $_GET["username"];
          $passward = $_REQUEST["pass"];
          $fname = $_REQUEST["fname"];
          $lname = $_REQUEST["lname"];
          $teleno = $_REQUEST["tele"];
          $email = $_REQUEST["email"];
          $company = $_REQUEST["company"];
          $add = $_REQUEST["add"];
          $country = $_REQUEST["add"];
          $city = $_REQUEST["add"];
          $occu = $_REQUEST["occu"];
          $membership = $_REQUEST["membership"];
          $q = "select u_name from my_user where u_name ='".$username."'";
          
          if(!mysql_query($q))
          {
          die("error".mysql_error());
          }
          $r = mysql_query($q);
          $rows = mysql_numrows($r);
          if($rows <= 0)
          {
          $query = "insert into my_user values('','$username' , '$passward', '$fname' , '$lname' , '$tele' ,'$add','$city','$country','$occu','$company','$membership',CURDATE(),'$email')";
          if(!mysql_query($query))
          {
          
          die("error".mysql_error());
          }
          $to = $email;
          $subject = "Your Registration At  Classifieds Guide India";
          $message = "Dear ".$fname;
          $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 .";
          $from = "xyz@yahoo.com";
          $headers = "From: $from";
          mail($to,$subject,$message,$headers);
          header("Location:create_user.php");
          }
          if($rows > 0 )
          {
          
          header("Location:create_user.php?action=dup");
          }
          }
          ?>
          Last edited by Shalini Bhalla; Jan 9 '09, 12:32 AM.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            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 :)

            Comment

            Working...