converting html/php form to variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oatesj77
Forum Newbie
Posts: 3
Joined: Tue Apr 05, 2005 2:13 pm
Location: Portland, OR

converting html/php form to variable

Post by oatesj77 »

new to php, but slowly building up my understanding. I've been working on this for a few days and slowly going crazy, so any help would be great.

Here is what i'm doing. I have an add to cart form that i wanted to convert to a variable so it would only be displayed if the item price was not equal to "SOLD OUT", I've managed to work my way through this, but now the problem comes in getting my add to cart form to actually work when it is displayed.
here are the pages and code I'm working on. http://www.oatesj77.com/oldsite/unklbra ... ynamic.php and the code is: http://www.oatesj77.com/oldsite/unklbra ... ynamic.txt
this page has items linking to a details page where the form is displayed/not displayed.
http://www.oatesj77.com/oldsite/unklbra ... etails.php and the code is http://www.oatesj77.com/oldsite/unklbra ... etails.txt

when you get to the detail page, you can see that i have two add to cart forms, one that is pulled from the variable and doesn't work, and the other is just the form, but it works.

basically what i'm trying to do is convert

Code: Select all

<form name=&quote;unklbrandcart_1_ATC_<?php echo $row_rsToysї'ProdID']; ?>&quote; method=&quote;POST&quote; action=&quote;<?php echo $_SERVERї&quote;PHP_SELF&quote;]; ?><?php echo (isset($_SERVERї&quote;QUERY_STRING&quote;]) && $_SERVERї&quote;QUERY_STRING&quote;] != &quote;&quote;)?&quote;?&quote;.$_SERVERї&quote;QUERY_STRING&quote;]:&quote;&quote;; ?>&quote;>
      <input type=&quote;hidden&quote; name=&quote;unklbrandcart_1_ID_Add&quote; value=&quote;<?php echo $row_rsToysї'ProdID']; ?>&quote; >
      <input type=&quote;image&quote; src=&quote;images/add-to-cart.gif&quote; border=&quote;0&quote; value=&quote;Add To Cart&quote; name=&quote;unklbrandcart_1_ATC&quote; alt=&quote;add to cart&quote;>
    </form>
into

Code: Select all

<?php
	$cartform = '<form name="unklbrandcart_1_ATC_'.$row_rsToys["ProdID"].'" method="post" action="'.$_SERVER["PHP_SELF"].' '.$_SERVER["QUERY_STRING"].'">
 	<input type="hidden" name="unklbrandcart_1_ID_Add" value="'.$row_rsToys["ProdID"].'">
  	<input type="image" src="images/add-to-cart.gif" border="0" value="Add To Cart" name="unklbrandcart_1_ATC" alt="add to cart">
	</form>';
	?>
then using this to display variable $cartform

Code: Select all

<?php
$prodPrice=$row_rsToys['ProdPrice']; 
$nfs="Not For Sale"; 
if ($prodPrice!="SOLD OUT") {echo $cartform;} 
else {echo $nfs;}
	?>
like i said, i've been working on this a long time and could use some good advice. thanks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

When you say "isn't working" ... are you getting errors or anything? Could you be more specific please?

BTW: Is there supposed to be a space between $_SERVER['PHP_SELF'] and $_SERVER['QUERY_STRING']? That could cause probs since it outputs "/yourfile.php%20?queries=values"

Code: Select all

$_SERVER["PHP_SELF"].' '.$_SERVER["QUERY_STRING"].
//Should be
$_SERVER["PHP_SELF"].$_SERVER["QUERY_STRING"].
oatesj77
Forum Newbie
Posts: 3
Joined: Tue Apr 05, 2005 2:13 pm
Location: Portland, OR

Post by oatesj77 »

thanks for taking a look at this.

made this change, but no fix

Code: Select all

$_SERVER["PHP_SELF"].' '.$_SERVER["QUERY_STRING"].
//Should be
$_SERVER["PHP_SELF"].$_SERVER["QUERY_STRING"].
going from http://www.oatesj77.com/oldsite/unklbra ... ynamic.php to http://www.oatesj77.com/oldsite/unklbra ... etails.php

what doesn't work is when you click on the add to cart button that is displayed via the variable $cartform, it just refreshes the page, when the actual form is used at the bottom of the page it will add the item to the shopping-cart.php page.

see above post for link to my code.
thanks
oatesj77
Forum Newbie
Posts: 3
Joined: Tue Apr 05, 2005 2:13 pm
Location: Portland, OR

solved

Post by oatesj77 »

i ended up solving by wrapping the form

Code: Select all

<?php
   $nfs="Not For Sale";
   if ($row_rsToys['ProdPrice'] != 0)    {
   ?>
     <form name="unklbrandcart_1_ATC_<?php echo $row_rsToys['ProdID']; ?>"      
     method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?><?php echo 
     (isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !=
     "")?"?".$_SERVER["QUERY_STRING"]:""; ?>">
     <input type="hidden" name="unklbrandcart_1_ID_Add" value="
       <?php echo $row_rsToys['ProdID']; ?>" >
       <input type="image" src="images/add-to-cart.gif" border="0" value="Add To Cart"    
       name="unklbrandcart_1_ATC" alt="add to cart">
    </form>
  <?php
	}
	else {echo $nfs;}
?>
Post Reply