• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

PHP how can i have it so so that email form is not sent until the user has finished selecting quantity ..

hi again
I have a PHP email form inputs of name, email, quantity (and price and bulb pulled from database ) .
how do can i have it so so that email is not sent until the user has finished selecting quantity and bulbs.ie the one customer may want to order more than one type of bulb at the one time
Current Form with thanks to  * stefanaichholzer *
<FORM ACTION="Email.php" method=post>
<table border=0>
<tr>
<td width=55>Name
</td>
<td ><input type="text" name="name" size="3" maxlength="3"></td>
</tr>
<td width=75>Email
</td>
<td ><input type="text" name="Email" size="3" maxlength="3"></td>
</tr><tr>
<td width=15>Quantity
</td>
<td ><input type="text" name="qty" size="3" maxlength="3"></td>
</tr>
<tr>
<td>
<p>
      <label for="bname">BULBS:</label></td>
   <td><select multiple name="bname[]" id="bname">
        <option value="">Select Bulb</option>
            <?php
                  while($bname = $imageList->fetch_assoc())
                   {
                         $selected = ($bname['bname'] == $bname) ? 'selected="selected"' : '';
                              echo '<option value="'.($bname['bname']*$bname['price']).'" '.$selected.'>'.$bname['bname']. ' '.$bname['price'].'</option>';
                         }
                  ?>
      </select>  
    </p>
</td>
</tr>
<tr>
<td colspan="2" align="center"><br>
<input type="submit" value="Enter Order"></td>
</tr>
</table>
</form>
0
LeighJor
Asked:
LeighJor
  • 6
  • 5
1 Solution
 
siva_sivaCommented:
Use the below code to check the validation

<script language="JavaScript">
function checkForm()
{
   var name, email, qty;
   with(document.bulbform)
   {
      name            = name;
      email            = Email;
      qty            = qty;
        bname            = ['bname[]'];
   }

   if(trim(name.value) == '')
   {
      alert('Please enter your name');
      name.focus();
      return false;
   }
   else if(trim(email.value) == '')
   {
      alert('Please enter your email');
      email.focus();
      return false;
   }
   else if(!isEmail(trim(email.value)))
   {
      alert('Email address is not valid');
      email.focus();
      return false;
   }
   else if(trim(qty.value) == '')
   {
      alert('Please enter quantity');
      qty.focus();
      return false;
   }
   else if(bname.value == '')
   {
      alert('Please select any bulb');
      bname.focus();
      return false;
   }
   else
   {
      name.value            = trim(name.value);
      email.value            = trim(email.value);
      qty.value                  = trim(qty.value);
        bname.value            = bname.value;
      return true;
   }
}

function trim(str) {
   return str.replace(/^\s+|\s+$/g,'');
}

function isEmail(str) {
   var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
   return regex.test(str);
}
</script>

<FORM ACTION="Email.php" method=post name="bulbform">
      <table border=0>
            <tr>
                  <td width=55>Name</td>
                  <td><input type="text" name="name" size="3" maxlength="3"></td>
            </tr>
                  <td width=75>Email</td>
                  <td><input type="text" name="Email" size="3" maxlength="3"></td>
            </tr>
            <tr>
                  <td width=15>Quantity</td>
                  <td><input type="text" name="qty" size="3" maxlength="3"></td>
            </tr>
            <tr>
                  <td><p><label for="bname">BULBS:</label></td>
                  <td><select multiple name="bname[]" id="bname"><option value="">Select Bulb</option>
                  <?php
                        while($bname = $imageList->fetch_assoc()) {
                              $selected = ($bname['bname'] == $bname) ? 'selected="selected"' : '';
                              echo '<option value="'.($bname['bname']*$bname['price']).'" '.$selected.'>'.$bname['bname']. ' '.$bname['price'].'</option>';
                        }
                  ?>
                  </select></p></td>
            </tr>
            <tr>
                  <td colspan="2" align="center"><br><input type="submit" value="Enter Order" onClick="return checkForm();"></td>
            </tr>
      </table>
</form>
0
 
LeighJorAuthor Commented:
Ok I'm getting the idea but what happens is that each selection of bulb still needs to be submitted and the on return the previous entries are still in form. Some how I need the name and email to remain while the customer is  given the option to choose more
Thanks so far  
0
 
siva_sivaCommented:
In this you will replace your email code in the email part. You name the below code as Email.php.

Email.php
########
<script language="JavaScript">
function checkForm()
{
   var name, email, qty;
   with(document.bulbform)
   {
      name            = name;
      email            = Email;
      qty            = qty;
        bname            = ['bname[]'];
   }

   if(trim(name.value) == '')
   {
      alert('Please enter your name');
      name.focus();
      return false;
   }
   else if(trim(email.value) == '')
   {
      alert('Please enter your email');
      email.focus();
      return false;
   }
   else if(!isEmail(trim(email.value)))
   {
      alert('Email address is not valid');
      email.focus();
      return false;
   }
   else if(trim(qty.value) == '')
   {
      alert('Please enter quantity');
      qty.focus();
      return false;
   }
   else if(bname.value == '')
   {
      alert('Please select any bulb');
      bname.focus();
      return false;
   }
   else
   {
      name.value            = trim(name.value);
      email.value            = trim(email.value);
      qty.value                  = trim(qty.value);
        bname.value            = bname.value;
      return true;
   }
}

function trim(str) {
   return str.replace(/^\s+|\s+$/g,'');
}

function isEmail(str) {
   var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
   return regex.test(str);
}
</script>

<?php
      $name = '';
      $email = '';
      $qty = '';

      if(isset($_POST['submit'])) {
            $name = $_POST['name'];
            $email = $_POST['Email'];
            $qty = $_POST['qty'];
            // Email Part
      }
?>

<FORM ACTION="Email.php" method="post" name="bulbform">
      <table border=0>
            <tr>
                  <td width=55>Name</td>
                  <td><input type="text" name="name" size="3" maxlength="3" value="<?php echo $name; ?>"></td>
            </tr>
                  <td width=75>Email</td>
                  <td><input type="text" name="Email" size="3" maxlength="3" value="<?php echo $email; ?>"></td>
            </tr>
            <tr>
                  <td width=15>Quantity</td>
                  <td><input type="text" name="qty" size="3" maxlength="3" value="<?php echo $qty; ?>"></td>
            </tr>
            <tr>
                  <td><p><label for="bname">BULBS:</label></td>
                  <td><select multiple name="bname[]" id="bname"><option value="">Select Bulb</option>
                  <?php
                        while($bname = $imageList->fetch_assoc()) {
                              $selected = ($bname['bname'] == $bname) ? 'selected="selected"' : '';
                              echo '<option value="'.($bname['bname']*$bname['price']).'" '.$selected.'>'.$bname['bname']. ' '.$bname['price'].'</option>';
                        }
                  ?>
                  </select></p></td>
            </tr>
            <tr>
                  <td colspan="2" align="center"><br><input type="submit" name="submit" value="Enter Order" onClick="return checkForm();"></td>
            </tr>
      </table>
</form>
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
LeighJorAuthor Commented:
Sorry Siva Siva
I cant get it to work
I think i need 2 buttons one to send email and one to allow multiple selections until email button is sent
0
 
siva_sivaCommented:
I can't understand your requirement...

Actually why you need 2 buttons?
1. One button is to submit email.
2. You select multiple selection before when you click the submit button.. Why you need that second button?
0
 
LeighJorAuthor Commented:
Sorry Siva Siva .I am new to this
I want the form to look like this
  ***from database***          *Entered into Box*    
                 v
BULB NAME     PRICE                 Qty                           Total
apple                 20                      7                                Qty*Pice
pear                  40                        9                                    "

I think i can get the lists  but if qty is from input box how do iI reference in the Total column and
how do I get the email to look this
Thanks for your patience
Leigh Jor
0
 
siva_sivaCommented:
For this you have to replace the multiple select list box to the following code, then only customer will give different type of quantity to different bulbs

<tr>
      <td>Bulb Name</td>
      <td>Price</td>
      <td>Quantity</td>
</tr>
<tr>
      <?php
            $i = 0
            while($bname = $imageList->fetch_assoc()) {
      ?>
            <td><input type="text" name="bname_$i" value="<? echo $bname['bname']; ?>" disabled></td>
            <td><input type="text" name="price_$i" value="<? echo $bname['price']; ?>" disabled></td>
            <td><input type="text" name="qty_$i" value="<? echo $qty; ?>"></td>
      <?php
            $i++;
            }
      ?>
</tr>
0
 
LeighJorAuthor Commented:
Hi Siva Siva
I cant get the total column to work.
Can i call on the value of any input form before it is submitted? Thanks again

<td>Total</td>
</tr>
<tr>
     <?php
         $i = 0;
         while($bname = $imageList->fetch_assoc()) {
     ?>
<tr>
     <td><input name="bname_$i" type="text" value="<? echo $bname['bname']; ?>" size="50" disabled></td>
       <td><input name="price_$i" type="text" value="<? echo $bname['price']; ?>" size="10" disabled></td>
            <td><input name="qty_$i" type="text" size="10" value="<? echo $qty; ?>" ></td>
                <?php
               $p='bname_$i';
                               $q='$qty';
               $total= $q*$p ;
                 ?>      
         <td><input type="text" name="total" value="<? echo $total; ?>"></td>
 </tr>
    <?php
            $i++;
             }
      ?>
</tr>
0
 
siva_sivaCommented:
In this you will write a javascript function to find total value.

<input name="qty_$i" type="text" size="10" value="<? echo $qty; ? onchange="find_total();">
0
 
LeighJorAuthor Commented:
Thanks Siva Siva
0
 
siva_sivaCommented:
Most welcome LeighJor
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now