SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    not taking items in database

    hi all,
    i am doing a shopping cart example.after the shopping is complete it is mandatory for the user
    to fill his details.so that it will go in to database.
    i have written my both php and javascript code but the "billing.php" is not taking in the database
    PHP Code:
    <?php
    include("db.php");
    include(
    "functions.php");
    if(
    $_REQUEST['command']=='update')
    {
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $phone=$_REQUEST['phone'];
    $result=mysql_query("insert into customers values('','$name','$email','$phone')");
    $customerid=mysql_insert_id();
    $date=date('Y-m-d');
    $result=mysql_query("insert into orders values('','$date','$customerid')");
    $orderid=mysql_insert_id();
        
    $max=count($_SESSION['cart']);
    for(
    $i=0;$i<$max;$i++)
    {
      
    $pid=$_SESSION['cart'][$i]['productid'];
      
    $q=$_SESSION['cart'][$i]['qty'];
      
    $price=get_price($pid);
      
    mysql_query("insert into order_detail values($orderid,$pid,$q,$price)");
    }
     die(
    'Thank You! Your Order Has Been Placed!');
    }
    ?>
    below is my javascript code.....
    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>
    <title>Billing Information</title>
    <script type="text/javascript">
    function checkName(form)
     {
      var eobj=document.getElementById('realnameerror');
      var sRealName = form.realname.value;
      var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
      var error=false;
      eobj.innerHTML='';
      if (sRealName == '') 
      {
       error='Error: Username cannot be blank!';
       form.realname.focus();
      }
      else if (sRealName.length < 4) 
      {
        error="UserName should be atleast 4 characters long";
      }
      else if (!oRE.test(sRealName))
      {
       error="Incorrect format.";
      }
      if (error)
      {
       form.realname.focus();
       eobj.innerHTML=error;
       return false;
      }
      return true;
     }
    
    function checkEmail(form)          /* for email validation */
    {                               
     var eobj=document.getElementById('emailerror');
     eobj.innerHTML='';
     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value))
     {
      return true;
     }
     eobj.innerHTML='Invalid E-mail Address! Please re-enter.';
     return false;
    }
    
    function validPhone(form)          /* phone no validation */
     {
     var eobj=document.getElementById('phonenoerror');
     var valid = '0123456789';
     var phone = form.phoneno.value;
     var error=false;
     var i=0;
     var temp;
     eobj.innerHTML='';
     if (phone == '')
     {
      error='This field is required. Please enter phone number';
     }
     else if (!phone.length > 1 || phone.length < 10)
     {
      error='Invalid phone number length! Please try again.';
     }
     else 
     {
      for (i=0; i < phone.length; i++)
      {
       temp = '' + phone.substring(i, i + 1);
       if (valid.indexOf(temp) == -1)
       {
        error='Invalid characters in your phone. Please try again.';
       }
      }
     }
     if (error)
     {
      form.phoneno.focus();
      eobj.innerHTML=error;
      return false;
     }
     return true;
    }
    
    function validate() 
    {
     var form = document.forms['form'];
     var ary=[checkName,checkEmail,validPhone];
     var rtn=true;
     var z0=0;
     for (var z0=0;z0<ary.length;z0++)
     {
      if (!ary[z0](form))
      {
        rtn=false;
      }
     }
     return rtn;
     form.command.value='update';
     form.submit(); 
    }
    </script>
    </head>
    <body>
    <form  name="form" method="post" onsubmit="return validate()">
     <div align="left">
       <h1 align="left">Billing Information</h1>
        Order Total:<b><?php echo "$" . get_order_total() ?></b><br>
        Cust Name:<input type="text" name="realname"> <span id="realnameerror" ></span><br>
        Email:<input type="text" name="email"> <span id="emailerror" ></span><br>
        Phone:<input type="text" name="phone" maxlength="10"> <span id="phonenoerror" ></span><br>
           <input type="submit" value="submit" />
     </div>
    </form>
    </body>
    </html>
    can u help me to identify where i went wrong....

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by ravi951 View Post
    i have written my both php and javascript code but the "billing.php" is not taking in the database
    Your PHP code is expecting a form value to be 'update', but you have no form value that performs such a task.
    What is typically done in this case is to give the submit button a name, and to check for that name instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay below is my updated javascript code....
    i have added the line new line with input type="hidden" and name ="command"
    in the form
    Code:
    <form  name="form" method="post" onsubmit="return validate()">
     <input type="hidden" name="command" />
     <div align="left">
       <h1 align="left">Billing Information</h1>
        Order Total:<b><?php echo "$" . get_order_total() ?></b><br>
        Cust Name:<input type="text" name="realname"> <span id="realnameerror" ></span><br>
        Email:<input type="text" name="email"> <span id="emailerror" ></span><br>
        Phone:<input type="text" name="phone" maxlength="10"> <span id="phonenoerror" ></span><br>
        <input type="submit" value="submit" />
     </div>
    </form>
    added line is in bold..
    will now it works

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    will now it works
    Did you try it?

  5. #5
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reply

    yes sir i have tried.after clicking the submit button before entering any values
    in the fields it is displaying like blink message.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    yes sir i have tried.
    Good. If you tried it, you should already know the answer. Does it work?
    after clicking the submit button before entering any values
    in the fields it is displaying like blink message.
    Is that what it should do?


    I think it shouldn't work.
    Code:
    <input type="hidden" name="command" />
    
    if($_REQUEST['command']=='update')
    Because you added a field named 'command', but it doesn't have a value. So the IF will never be true.

  7. #7
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually this is the last part of the program.before this "billing.php"
    i have "product.php" and "shopping.php" there i have been using "command"
    field.it was working there

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    actually this is the last part of the program.before this "billing.php"
    i have "product.php" and "shopping.php" there i have been using "command"
    field.it was working there
    Very good. Then all you have to do is find the difference between those scripts and this one. You did something differently, or it would work here as well.

  9. #9
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think it is right in my php code.
    i have doubt in javascript code because after submitting the button
    it is displaying the messages as in javascript like "user name should not be less than 4 characters"
    but only for a fraction of seconds and so on.but it is not standing for long time.....

  10. #10
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Ok, never mind what I said about the command field. I see you're setting the value in your javascript (completely missed that the first time I looked at it).

    So you send the form without filling the fields.
    The javascript adds the error messages.
    And then they disappear again, and the form is shown without error messages?

    Then it looks like for some reason the form is sent anyway, even though there are errors.
    Try adding some alerts to your javascript code to see what is happening. Or use the firebug plugin in FF (for example) to debug the javascript.

    Another question: are the php code and the javascript/form in the same page (billing.php) ?

  11. #11
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reply

    Code:
    So you send the form without filling the fields.
    The javascript adds the error messages.
    And then they disappear again, and the form is shown without error messages?
    yes
    Code:
    are the php code and the javascript/form in the same page (billing.php) ?
    yes both are in same page saved as "billing.php"

    i am trying for inline validation using javascript

  12. #12
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    Code:
    are the php code and the javascript/form in the same page (billing.php) ?
    yes both are in same page saved as "billing.php"
    ok
    i am trying for inline validation using javascript
    Yes I figured that

    Then it looks like for some reason the form is sent anyway, even though there are errors.
    Try adding some alerts to your javascript code to see what is happening. Or use the firebug plugin in FF (for example) to debug the javascript.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •