lingo5 Posted March 27, 2012 Share Posted March 27, 2012 Hi, I am trying to pass a variable when posting a form. This is my form with the select: <form id="form1" name="form1" method="post" action="products_2.php?id_subcategoria= WHAT SOULD I PUT HERE?"> <select name="subcats" class="subcatsSelectMenu" id="subcats" onchange="this.form.submit()"> <option value="">Ver placas por tipos</option> <?php do { ?> <option value="<?php echo $row_subcats_RS['id_subcategoria']?>"><?php echo $row_subcats_RS['subcategoria_esp']?></option> <?php } while ($row_subcats_RS = mysql_fetch_assoc($subcats_RS));$rows = mysql_num_rows($subcats_RS); if($rows > 0) { mysql_data_seek($subcats_RS, 0); $row_subcats_RS = mysql_fetch_assoc($subcats_RS); } ?> </select> </form> How can I pass the variable in the URL? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/259822-please-help-passing-variable/ Share on other sites More sharing options...
Drummin Posted March 27, 2012 Share Posted March 27, 2012 Rather than attempting to change the the action section of the form and passing value as get, just pick up posted value at the top of your page and use that variable for calling records etc. <?php if (isset($_POST['subcats'])){ $id_subcategoria=$_POST['subcats']; } ?> <html> <body> <form id="form1" name="form1" method="post" action="products_2.php"> <select name="subcats" class="subcatsSelectMenu" id="subcats" onchange="this.form.submit()"> <option value="">Ver placas por tipos</option> <?php do { ?> <option value="<?php echo $row_subcats_RS['id_subcategoria']?>"><?php echo $row_subcats_RS['subcategoria_esp']?></option> <?php } while ($row_subcats_RS = mysql_fetch_assoc($subcats_RS));$rows = mysql_num_rows($subcats_RS); if($rows > 0) { mysql_data_seek($subcats_RS, 0); $row_subcats_RS = mysql_fetch_assoc($subcats_RS); } ?> </select> </form> Quote Link to comment https://forums.phpfreaks.com/topic/259822-please-help-passing-variable/#findComment-1331617 Share on other sites More sharing options...
lingo5 Posted March 27, 2012 Author Share Posted March 27, 2012 Thanks Drummin, I don't understand what you mean sorry....how would I pass the variable from products.php to products_2.php using your method? Quote Link to comment https://forums.phpfreaks.com/topic/259822-please-help-passing-variable/#findComment-1331618 Share on other sites More sharing options...
Drummin Posted March 27, 2012 Share Posted March 27, 2012 Oh. I didn't realize we were dealing with different pages. Is there any reason the processing needs to be on a different page? You could always use a header("location: ") if you must, but I would just have the posted values process on the page where it will be used. Quote Link to comment https://forums.phpfreaks.com/topic/259822-please-help-passing-variable/#findComment-1331621 Share on other sites More sharing options...
lingo5 Posted March 27, 2012 Author Share Posted March 27, 2012 The reason why I'm going to a different page is because I have a recordset in the curret page that selects items by id_category like so: mysql_select_db($database_MySQLconnect, $MySQLconnect); $query_productos_RS = sprintf("SELECT * FROM t_articulo WHERE id_categoria = %s", GetSQLValueString($colname_productos_RS, "int")); $query_limit_productos_RS = sprintf("%s LIMIT %d, %d", $query_productos_RS, $startRow_productos_RS, $maxRows_productos_RS); $productos_RS = mysql_query($query_limit_productos_RS, $MySQLconnect) or die(mysql_error()); $row_productos_RS = mysql_fetch_assoc($productos_RS); So I dont know how to set the same page up to use your method. Quote Link to comment https://forums.phpfreaks.com/topic/259822-please-help-passing-variable/#findComment-1331631 Share on other sites More sharing options...
lingo5 Posted March 27, 2012 Author Share Posted March 27, 2012 I think the easiest way would be to pass the variable from page A to page B. Could I do something like this? action="productos_2.php?id_subcategoria=<?php echo((isset($_POST["subcats"]))?$_POST["subcats"]:"") ?>"> ...please help!! Quote Link to comment https://forums.phpfreaks.com/topic/259822-please-help-passing-variable/#findComment-1331635 Share on other sites More sharing options...
Drummin Posted March 27, 2012 Share Posted March 27, 2012 Yes, never having used sprintf() or GetSQLValueString(), or understanding the reason for using them I can't help with that directly. However as you seem to need values passed as GET, then just use the header as I mentioned, adding this to the top of your products.php page. Be sure to change your forms to post to this page as well. <?php if (isset($_POST['subcats'])){ $id_subcategoria=$_POST['subcats']; header ("location: productos_2.php?id_subcategoria=$id_subcategoria"); exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259822-please-help-passing-variable/#findComment-1331662 Share on other sites More sharing options...
Drummin Posted March 28, 2012 Share Posted March 28, 2012 Man I feel silly missing this. I guess it's because I never use GET in a form. Why not just change the form to... <form id="form1" name="form1" method="get" action="products_2.php"> <select name="id_subcategoria" class="subcatsSelectMenu" id="subcats" onchange="this.form.submit()"> <option value="">Ver placas por tipos</option> <?php do { ?> <option value="<?php echo $row_subcats_RS['id_subcategoria']?>"><?php echo $row_subcats_RS['subcategoria_esp']?></option> <?php } while ($row_subcats_RS = mysql_fetch_assoc($subcats_RS));$rows = mysql_num_rows($subcats_RS); if($rows > 0) { mysql_data_seek($subcats_RS, 0); $row_subcats_RS = mysql_fetch_assoc($subcats_RS); } ?> </select> </form> Quote Link to comment https://forums.phpfreaks.com/topic/259822-please-help-passing-variable/#findComment-1331786 Share on other sites More sharing options...
lingo5 Posted March 28, 2012 Author Share Posted March 28, 2012 Thanks Drummin!! I don't know why I was trying to make it so complicated.... Quote Link to comment https://forums.phpfreaks.com/topic/259822-please-help-passing-variable/#findComment-1331802 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.