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
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…


<!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…

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.

okay below is my updated javascript code…
i have added the line new line with input type=“hidden” and name =“command”
in the form


<form  name="form" method="post" onsubmit="return validate()">
 [B]<input type="hidden" name="command" />[/B]
 <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

Did you try it?

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

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.


<input type="hidden" name="[B]command[/B]" />

if($_REQUEST['[B]command[/B]']=='[B][COLOR="#FF0000"]update[/COLOR][/B]')

Because you added a field named ‘command’, but it doesn’t have a value. So the IF will never be true.

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.

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…

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) ?


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


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

ok

i am trying for inline validation using javascript

Yes I figured that :slight_smile:

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.