Jump to content

Help passing checkbox selection through


stepo

Recommended Posts

I need to create a new final page. this is basically a confirmation page of whatever the user has inputed into the form.

 

I need to carry through whatever boxes are checked on a check box and have them presented on a confirmation page and also how much the total will cost will need to be carried through.

 

Here is the code I have, hopefully you can make some sense of it. it does work for everything I need it to do.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
  <HEAD>
    <TITLE>Stock Market</TITLE>
    <SCRIPT LANGUAGE="JavaScript">

    <!--
    function showPrices(n) {
      if (n == 1) {
        if (document.priceForm.AviationField.value == "") {
          document.priceForm.AviationField.value = 50 ; }
        else {
          document.priceForm.AviationField.value = "" ; }
      }
      if (n == 2) {
        if (document.priceForm.BanksField.value == "") {
          document.priceForm.BanksField.value = 25 ; }
        else {
          document.priceForm.BanksField.value = "" ; }
      }
     
if (n == 3) {
        if (document.priceForm.ConstructionField.value == "") {
          document.priceForm.ConstructionField.value = 30 ; }
        else {
          document.priceForm.ConstructionField.value = "" ; }
      }
  if (n == 4) {
        if (document.priceForm.ElectronicsField.value == "") {
          document.priceForm.ElectronicsField.value = 60 ; }
        else {
          document.priceForm.ElectronicsField.value = "" ; }
      }
  if (n == 5) {
        if (document.priceForm.OilandGasField.value == "") {
          document.priceForm.OilandGasField.value = 80 ; }
        else {
          document.priceForm.OilandGasField.value = "" ; }
      }
  if (n == 6) {
        if (document.priceForm.PharmaceuticalsField.value == "") {
          document.priceForm.PharmaceuticalsField.value = 25 ; }
        else {
          document.priceForm.PharmaceuticalsField.value = "" ; }
      }
   document.priceForm.TotalField.value = totalPrices() ;
    }

    function totalPrices() {
      var total = 0;
      if (document.priceForm.AviationField.value != "") {
        total += parseInt(document.priceForm.AviationField.value) ; }
      if (document.priceForm.BanksField.value != "") {
        total += parseInt(document.priceForm.BanksField.value) ; }
  if (document.priceForm.ConstructionField.value != "") {
        total += parseInt(document.priceForm.ConstructionField.value) ; }
  if (document.priceForm.ElectronicsField.value != "") {
        total += parseInt(document.priceForm.ElectronicsField.value) ; }
  if (document.priceForm.OilandGasField.value != "") {
        total += parseInt(document.priceForm.OilandGasField.value) ; }
  if (document.priceForm.PharmaceuticalsField.value != "") {
        total += parseInt(document.priceForm.PharmaceuticalsField.value) ; }
      return total ;
    }
    -->
    </SCRIPT>
  </HEAD>
  <BODY>
    <H1>Stock Market Hot Tips</H1><HR>
    
    <P/>
  
    <?php
	$name = $_POST['CodeField'];
    ?>
   
    <?php
	echo "Hello $name, and welcome<br>\n" ;
	?>
  
Check the boxes to select the area(s) you are interested in.<BR>
    The price for access to our page of useful and valuable information on that area will be shown.
    
    <script type="text/javascript">
  function CheckForm(){
    if(document.getElementById('Markets1').checked==false && 
       document.getElementById('Markets2').checked==false && 
       document.getElementById('Markets3').checked==false &&
   document.getElementById('Markets4').checked==false && 
       document.getElementById('Markets5').checked==false && 
       document.getElementById('Markets6').checked==false 
    ){
      alert('Please choose a category');
      return false; 
    }
    return true;
  }
</script>

    
    <FORM NAME="priceForm" method="post" action="goodbye.php" onSubmit="return CheckForm()">
      <P/>
      <TABLE>
      <TR>
      <TD></TD>
      	<TD></TD>
      <TD>Price(£)</TD>
      </TR>
      <TR>
      <TD><INPUT TYPE="checkbox" NAME="Markets" ID="Markets1" VALUE="Aviation and Aerospace"
      	      onClick="showPrices(1)" ></INPUT></TD>
      <TD>Aviation and Aerospace</TD>
      	<TD><INPUT TYPE="text" NAME="AviationField" SIZE="5" VALUE="" ></INPUT></TD>
      </TR>
      <TR>
      <TD><INPUT TYPE="checkbox" NAME="Markets" ID="Markets2" VALUE="Banks"
      	      onClick="showPrices(2)"></INPUT></TD>
      <TD>Banks</TD>
      	<TD><INPUT TYPE="text" NAME="BanksField" SIZE="5" VALUE=""></INPUT></TD>
      </TR>
       <TR>
      <TD><INPUT TYPE="checkbox" NAME="Markets" ID="Markets3" VALUE="Construction"
      	      onClick="showPrices(3)"></INPUT></TD>
      <TD>Construction</TD>
      	<TD><INPUT TYPE="text" NAME="ConstructionField" SIZE="5" VALUE=""></INPUT></TD>
      </TR>
      <TR>
      <TD><INPUT TYPE="checkbox" NAME="Markets" ID="Markets4" VALUE="Electronics"
      	      onClick="showPrices(4)"></INPUT></TD>
      <TD>Electronics</TD>
      	<TD><INPUT TYPE="text" NAME="ElectronicsField" SIZE="5" VALUE=""></INPUT></TD>
      </TR>
      <TR>
      <TD><INPUT TYPE="checkbox" NAME="Markets" ID="Markets5" VALUE="Oil and Gas"
      	      onClick="showPrices(5)"></INPUT></TD>
      <TD>Oil and Gas</TD>
      	<TD><INPUT TYPE="text" NAME="OilandGasField" SIZE="5" VALUE=""></INPUT></TD>
      </TR>
      <TR>
      <TD><INPUT TYPE="checkbox" NAME="Markets" ID="Markets6" VALUE="Pharmaceuticals"
      	      onClick="showPrices(6)"></INPUT></TD>
      <TD>Pharmaceuticals</TD>
      	<TD><INPUT TYPE="text" NAME="PharmaceuticalsField" SIZE="5" VALUE=""></INPUT></TD>
      </TR>
      
<TR>
      <TD></TD>
      	<TD>Total:</TD>
      <TD><INPUT TYPE="text" NAME="TotalField" SIZE="5" VALUE="0"></INPUT></TD>
      </TR>
      </TABLE>
      <p>
    <input type="hidden" name="CodeField" VALUE="<?php echo $name ?>">    
  <input type="submit" name="submit" value="Subscribe">

  
    </FORM>
  
    <HR>
    <NOSCRIPT>
      Sorry, no scripting support
    </NOSCRIPT>
  </BODY>
</HTML>

 

Link to comment
https://forums.phpfreaks.com/topic/233519-help-passing-checkbox-selection-through/
Share on other sites

Thanks for that, It does come out on goodbye but in a weird way...

 

Array ( [Markets] => Array ( [0] => Aviation and Aerospace [1] => Banks [2] => Construction ) [AviationField] => 50 [banksField] => 25 [ConstructionField] => 30 [ElectronicsField] => [OilandGasField] => [PharmaceuticalsField] => [TotalField] => 105 [CodeField] => Stephen [submit] => Subscribe )

 

 

How do I maniputlate that so I can just use the variables that have been checked and also use the total?

Don't use the total. Calculate it according to the other values. Because if you assume that the total is correct then you open yourself to being conned by every single intelligent user you have.

 

$_POST["Markets"] is an array. An array of the checkboxes that were checked. So...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.