Jump to content

Weird HTML Form to PHP Problem


cavey5

Recommended Posts

I have a form that allows users to select one of five options via a radio button. All of the radio buttons have the same name, and different values. These are passed to a script via POST and inserted in a database. The problem is it is randomly selecting one of the five values, not always what the user selected. My code is below, what am I missing? It seems so basic, am I crazy?

 

User Input Form

 

<form name="cm_subscribe" action="addtocart.php" method="post" autocomplete="off" onSubmit="return checkit(this)">
<table width="448" cellpadding="0" cellspacing="0" align="center"  valign="top"  border="0">
  <tr>
    <td width="28" height="30" bgcolor="#dcddd8" align="right"><input type="radio" name="cm_subtype" value="usa1"></input></td>
    <td width="105" height="30" bgcolor="#dcddd8" align="center">18</td>
    <td width="85" height="30" bgcolor="#dcddd8" align="left">3 Years</td>
    <td width="52" height="30" bgcolor="#dcddd8" align="left">$49.97</td>
    <td width="40" height="30" bgcolor="#dcddd8" align="left"><img src="images/cart/red_arrow.jpg" alt="Best Deal!" /></td>
    <td width="138" height="30" bgcolor="#dcddd8" align="left"><B>Only $2.77 an issue!</B></td>
  </tr>
  <tr>
    <td width="448" height="1" colspan="6" bgcolor="#333333"></td>
  </tr>
  <tr>
    <td width="28" height="30" bgcolor="#f5f5f5" align="right"><input type="radio" name="cm_subtype" value="usa2"></input></td>
    <td width="105" height="30" bgcolor="#f5f5f5" align="center">12</td>
    <td width="85" height="30" bgcolor="#f5f5f5" align="left">2 Years</td>
    <td width="52" height="30" bgcolor="#f5f5f5" align="left">$37.97</td>
    <td width="40" height="30" bgcolor="#f5f5f5" align="center"></td>
    <td width="138" height="30" bgcolor="#f5f5f5" align="center"></td>
  </tr>
  <tr>
    <td width="28" height="30" bgcolor="#f5f5f5" align="right"><input type="radio" name="cm_subtype" value="usa3"></input></td>
    <td width="105" height="30" bgcolor="#f5f5f5" align="center">6</td>
    <td width="85" height="30" bgcolor="#f5f5f5" align="left">1 Year</td>
    <td width="52" height="30" bgcolor="#f5f5f5" align="left">$23.97</td>
    <td width="40" height="30" bgcolor="#f5f5f5" align="center"></td>
    <td width="138" height="30" bgcolor="#f5f5f5" align="center"></td>
  </tr>
  <tr>
    <td width="448" height="1" colspan="6" bgcolor="#333333"></td>
  </tr>
  <tr height="10"> </td>
  </tr>
    <tr>
    <td width="28" height="30" bgcolor="#f5f5f5" align="right"><input type="radio" name="cm_subtype" value="can1"></input></td>
    <td width="420" height="30" bgcolor="#f5f5f5" align="left" colspan="5">        Canadian Order / $34.00 USD Per Year</td>
  </tr>
  <tr>
    <td width="28" height="30" bgcolor="#f5f5f5" align="right"><input type="radio" name="cm_subtype" value="int1"></input></td>
    <td width="420" height="30" bgcolor="#f5f5f5" align="left" colspan="5">        Foreign Order / $45.00 USD Per Year</td>
  </tr>
</table>

 

 

 

Add To Cart Script Dumps It In a Database Table

 

<?php
if (!$PHPSESSID) {
        header("Location: index.php");
        exit;
}
session_start();
?>

<?

// Makes initial connection to database
@include ('sql_connect.php');

$cm_subtype = mysql_real_escape_string($_POST['cm_subtype']);
$cm_sfirstname = mysql_real_escape_string($_POST['cm_sfirstname']);
$cm_slastname = mysql_real_escape_string($_POST['cm_slastname']);
$cm_saddress1 = mysql_real_escape_string($_POST['cm_saddress1']);
$cm_saddress2 = mysql_real_escape_string($_POST['cm_saddress2']);
$cm_scity = mysql_real_escape_string($_POST['cm_scity']);
$cm_sstate = mysql_real_escape_string($_POST['cm_sstate']);
$cm_szipcode = mysql_real_escape_string($_POST['cm_szipcode']);
$cm_scountry = mysql_real_escape_string($_POST['cm_scountry']);
$cm_sprovince = mysql_real_escape_string($_POST['cm_sprovince']);
$cm_bfirstname = mysql_real_escape_string($_POST['cm_bfirstname']);
$cm_blastname = mysql_real_escape_string($_POST['cm_blastname']);
$cm_baddress1 = mysql_real_escape_string($_POST['cm_baddress1']);
$cm_baddress2 = mysql_real_escape_string($_POST['cm_baddress2']);
$cm_bcity = mysql_real_escape_string($_POST['cm_bcity']);
$cm_bstate = mysql_real_escape_string($_POST['cm_bstate']);
$cm_bzipcode = mysql_real_escape_string($_POST['cm_bzipcode']);
$cm_bcountry = mysql_real_escape_string($_POST['cm_bcountry']);
$cm_bprovince = mysql_real_escape_string($_POST['cm_bprovince']);
$cm_email = mysql_real_escape_string($_POST['cm_email']);
$cm_phone = mysql_real_escape_string($_POST['cm_phone']);
$cm_cardtype = mysql_real_escape_string($_POST['cm_cardtype']);
$cm_cardnumber = mysql_real_escape_string($_POST['cm_cardnumber']);
$cm_expmon = mysql_real_escape_string($_POST['cm_expmon']);
$cm_expyear = mysql_real_escape_string($_POST['cm_expyear']);
$cm_promocode = mysql_real_escape_string($_POST['cm_promocode']);

if ($cm_subtype == "usa1")
{
  $cm_price = number_format (23.97,2);
  $cm_description = "USA Subscription - 1 Year";
}
elseif ($cm_subtype == "usa2")
    {
      $cm_price = number_format (37.97,2);
      $cm_description = "USA Subscription - 2 Years";
    }
elseif (cm_subtype == "usa3")
    {
      $cm_price = number_format (49.97,2);
      $cm_description = "USA Subscription - 3 Years";
    }
elseif ($cm_subtype = "can1")
    {
      $cm_price = number_format (35.00,2);
      $cm_description = "CAN Subscription - 1 Year";
    }
elseif ($cm_subtype = "int1")
    {
      $cm_price = number_format (45.00,2);
      $cm_description = "Foreign Subscription - 1 Year";
    }
else 
    {
      $cm_price = number_format (25.00,2);
      $cm_description = "USA Subscription - 1 Year";
    }
    
      
    $insert_sub = "INSERT INTO cart (sessid, cm_subtype, cm_sfirstname, cm_slastname, cm_saddress1, cm_saddress2, cm_scity, cm_sstate, cm_szipcode, 
    cm_scountry, cm_sprovince, cm_bfirstname, cm_blastname, cm_baddress1, cm_baddress2, cm_bcity, cm_bstate, cm_bzipcode, cm_bcountry, cm_bprovince,
    cm_email, cm_phone, cm_cardtype, cm_cardnumber, cm_expmon, cm_expyear, cm_promocode, cm_description, cm_price) 
    VALUES ('$PHPSESSID', '$cm_subtype', '$cm_sfirstname', '$cm_slastname', '$cm_saddress1', '$cm_saddress2', '$cm_scity', '$cm_sstate', '$cm_szipcode',
    '$cm_scountry', '$cm_sprovince', '$cm_bfirstname', '$cm_blastname', '$cm_baddress1', '$cm_baddress2', '$cm_bcity', '$cm_bstate', '$cm_bzipcode',
    '$cm_bcountry', '$cm_bprovince', '$cm_email', '$cm_phone', '$cm_cardtype', '$cm_cardnumber', '$cm_expmon', '$cm_expyear', '$cm_promocode', '$cm_description', '$cm_price')";  

  $insert_sub_result= mysql_query($insert_sub) 
   OR die(mysql_error());  


header("Location: cart.php");

exit;
?>

 

 

Then the cart displays the results in a table... the problem is when I select any radio button, it comes out wrong, most of the time it reads 1 year Canadian Sub $35.00... I did this on Mac Safari and Firefox with the same result, and the database is receiving the same data, it isn't a cache thing. Like I select the radio button for cm_subtype="usa1" and the price should be $23.97 and cm_description should be USA 1 year Subscription or whatever, but it is displaying $35.00 and 1 year Canadian Subscriotion

Link to comment
https://forums.phpfreaks.com/topic/52281-weird-html-form-to-php-problem/
Share on other sites

Alright I changed my if statement around a little but still have a problem...

 

I have five radio buttons, all with the same name but with five different values assigned to them, as follows:

 

usa1

usa2

usa3

can

for

 

can is a Canadian subscription and it is $36.00

for is a foreign subscription and it is $45.00

 

No matter what i do though, when I select the foreign option, it echos the Canadian price and description to the database. The other four work fine, what gives? Is there something wrong with my if statement? I echod the values that the form is putting out to a blank page and it is perfect, if you select Foreign, echo $_POST[cm_subtype]; displays "for"

 

Ideas?

 

if ($_POST['cm_subtype'] == "usa1")
{
  $cm_price = number_format (23.97,2);
  $cm_description = "USA Subscription - 1 Year";
}
elseif ($_POST['cm_subtype'] == "usa2")
    {
      $cm_price = number_format (37.97,2);
      $cm_description = "USA Subscription - 2 Years";
    }
elseif ($_POST['cm_subtype'] == "usa3")
    {
      $cm_price = number_format (49.97,2);
      $cm_description = "USA Subscription - 3 Years";
    }
elseif ($_POST['cm_subtype'] = "can")
    {
      $cm_price = number_format (36.00,2);
      $cm_description = "CAN Subscription - 1 Year";
    }
elseif ($_POST['cm_subtype'] = "for")
    {
      $cm_price = number_format (45.00,2);
      $cm_description = "Foreign Subscription - 1 Year";
    }
else 
    {
      $cm_price = number_format (25.00,2);
      $cm_description = "USA Subscription - 1 Year";
    }

<?php
if (!$PHPSESSID) {
        header("Location: index.php");
        exit;
}
session_start();
?>

 

I may be wrong but shouldn't the session_start(); be before the if statement like this?

<?php
session_start();

if ($PHPSESSID) {
       header("Location: index.php");
       exit;
}

No, that would defeat the purpose of the if statement. The session is initiated on a previous page, that code is just checking to see if a session already exists. If it doesn't, it routes you back to the index and starts a session. This keeps people from typing in www.domain.com/cart.php and seeing an empty cart.

 

I still cannot get this to work. For some reason the international selection is echoing the canadian data. Maybe my server is a smart ass and trying to tell me that Canada is international haha..

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.