Link to home
Start Free TrialLog in
Avatar of dereksheahan
dereksheahan

asked on

Passing variables under a submit button

Hi All,
I want to pass two url variables that a user has selected from two dynamic drop-downs. I’ve achieved this through a link on a page before using the example.php?cat_variable=test syntax but don’t know where to put it under a submit button. The two url variables I have set up are Type_variable (which I need to take from the drop-down named “Brand”) and the cat_variable (to be taken from “Category”) Any help would be much appreciated. Here’s the code to the page so far.
Thanks,
D

<?php require_once('../Connections/GSM_mobile.php'); ?>
<?php
mysql_select_db($database_GSM_mobile, $GSM_mobile);
$query_rsCatMod = "SELECT category_table.cat_category_id, category_table.cat_category_name, type_table.type_type_id, type_table.type_type_name, type_table.type_category_id FROM category_table, type_table WHERE category_table.cat_category_id = type_table.type_category_id";
$rsCatMod = mysql_query($query_rsCatMod, $GSM_mobile) or die(mysql_error());
$row_rsCatMod = mysql_fetch_assoc($rsCatMod);
$totalRows_rsCatMod = mysql_num_rows($rsCatMod);
?><!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="type_cat_post" name="type_cat_post" method="post" action="">
  <p>Select Your Model Category and Type:</p>
  <p>
    <select name="Category" id="Category" onchange="TCN_reload(this)">
      <option selected="selected">Category</option>
    </select>
    <select name="Brand" id="Brand" onchange="TCN_reload(this)">
      <option selected="selected">Brand</option>
    </select>
    <script language="JavaScript" type="text/javascript">
TCN_contents=new Array();
TCN_tempArray=new Array();
TCN_counter=0;
function TCN_addContent(str){
      TCN_contents[TCN_counter]=str;
      TCN_counter++;
}
function TCN_split(){
      TCN_arrayValues = new Array();
      for(i=0;i<TCN_contents.length;i++){
            TCN_arrayValues[i]=TCN_contents[i].split(separator);
            TCN_tempArray[0]=TCN_arrayValues;
      }
}
function TCN_makeSelValueGroup(){
      TCN_selValueGroup=new Array();
      var args=TCN_makeSelValueGroup.arguments;
      for(i=0;i<args.length;i++){
            TCN_selValueGroup[i]=args[i];
            TCN_tempArray[i]=new Array();
      }
}
function TCN_makeComboGroup(){
      TCN_comboGroup=new Array();
      var args=TCN_makeComboGroup.arguments;
      for(i=0;i<args.length;i++) TCN_comboGroup[i]=findObj(args[i]);
}
function TCN_setDefault(){
      for (i=TCN_selValueGroup.length-1;i>=0;i--){
            if(TCN_selValueGroup[i]!=""){
                  for(j=0;j<TCN_contents.length;j++){
                        if(TCN_arrayValues[j][(i*2)+1]==TCN_selValueGroup[i]){
                              for(k=i;k>=0;k--){
                                    if(TCN_selValueGroup[k]=="") TCN_selValueGroup[k]=TCN_arrayValues[j][(k*2)+1];
                              }
                        }
                  }
            }
      }
}
function TCN_loadMenu(daIndex){
      var selectionMade=false;
      daArray=TCN_tempArray[daIndex];
      TCN_comboGroup[daIndex].options.length=0;
      for(i=0;i<daArray.length;i++){
            existe=false;
            for(j=0;j<TCN_comboGroup[daIndex].options.length;j++){
                  if(daArray[i][(daIndex*2)+1]==TCN_comboGroup[daIndex].options[j].value) existe=true;
            }
            if(existe==false){
                  lastValue=TCN_comboGroup[daIndex].options.length;
                  TCN_comboGroup[daIndex].options[TCN_comboGroup[daIndex].options.length]=new Option(daArray[i][daIndex*2],daArray[i][(daIndex*2)+1]);
                  if(TCN_selValueGroup[daIndex]==TCN_comboGroup[daIndex].options[lastValue].value){
                        TCN_comboGroup[daIndex].options[lastValue].selected=true;
                        selectionMade=true;
                  }
            }
      }
      if(selectionMade==false) TCN_comboGroup[daIndex].options[0].selected=true;
}      
function TCN_reload(from){
      if(!from){
            TCN_split();
            TCN_setDefault();
            TCN_loadMenu(0);
            TCN_reload(TCN_comboGroup[0]);
      }else{
            for(j=0; j<TCN_comboGroup.length; j++){
                  if(TCN_comboGroup[j]==from) index=j+1;
            }
            if(index<TCN_comboGroup.length){
                  TCN_tempArray[index].length=0;
                  for(i=0;i<TCN_comboGroup[index-1].options.length;i++){
                        if(TCN_comboGroup[index-1].options[i].selected==true){
                              for(j=0;j<TCN_tempArray[index-1].length;j++){
                                    if(TCN_comboGroup[index-1].options[i].value==TCN_tempArray[index-1][j][(index*2)-1]) TCN_tempArray[index][TCN_tempArray[index].length]=TCN_tempArray[index-1][j];
                              }
                        }
                  }
            TCN_loadMenu(index);
            TCN_reload(TCN_comboGroup[index]);
            }
      }
}
function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
TCN_makeSelValueGroup("","");
TCN_makeComboGroup("Category","Brand");
 var separator="+#+";
<?php do{?>
TCN_addContent("<?php echo $row_rsCatMod['cat_category_name']; ?>+#+<?php echo $row_rsCatMod['cat_category_id']; ?>+#+<?php echo $row_rsCatMod['type_type_name']; ?>+#+<?php echo $row_rsCatMod['type_type_id']; ?>");
<?php } while ($row_rsCatMod = mysql_fetch_assoc($rsCatMod)); ?>
TCN_reload();

    </script>
</p>
  <p>
    <label>
    <input type="submit" name="Submit" value="Select" />
    </label>
  </p>
</form>


</body>
</html>
<?php
mysql_free_result($rsCatMod);
?>
SOLUTION
Avatar of siva350
siva350

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want those two variables to appear in the query string on another page, change the form to:

<form id="type_cat_post" name="type_cat_post" method="GET action="resultpage.php">

The URL for resultpage.php would then be:

http://www.derektestsite.com/resultpage.php?Brand=selection1&Category=selection2
Avatar of dereksheahan
dereksheahan

ASKER

Hi Jason,
Great that works. One thing is confusing me however. Why I am using the "GET" action to post to another page?? Also, Is it possible to hide the results of passing url variables in the address bar?
Thanks,
D
GET and POST work a little differently.  GET sends the form variables through the URL and redisplays them there as the query string, POST reads them into an array on the server.  In your orginial question, you mentioned passing the variables through the URL, so I showed you how to do that.

If you just want the variables available on the next page, change the form method to POST and call the variables out the the array per my original post.
Thanks for clearing that up. Is it possible to hide the variables being passed using the GET method though, or is that a silly question ? :)
Post is the way I would do it, Jason seems to have it covered.
Thanks siva...your first answer was right too, so Derek should give you an assist (hint hint, Derek.)

You can't hide the query string completely but if you want to protect information and still use GET, you can encrypt it.  Let's say you want to send an email to all of your customers and refer them back to your site to get a special or something.  Let us further say that you want to customize the web page for each customer.  The way you do this is to put the customer_id in the query string of the url:

www.example.com/offer.php?customer_id=1

But you shouldn't do that.  Why?  Because the user can sit there and alter the query string to pull up another customer's record and divulge private information.  So instead, you run some kind of encoding function like crypt() or md5() to produce a random string like:

www.example.com/offer.php?cistomer_id=c4ca4238a0b923820dcc

For transactions that begin and end on a web site, just do as siva says and use POST.  Life is much easier.
thanks, that encoding function looks like fun. Might try to use that somewhere in the future. Cheers guys