If then syntax, conditional link in form submit button

I have a site with some code which checks the value of a field in a database table, and controls which page a user goes to when they hit the submit button on a form.

It is part of a shopping cart, where a few optional extras are offered along the way.

I have added a new option for a different size of cushions, but can’t get the syntax right, or am going wrong somewhere.

I have this code in my header:


$row = mysql_fetch_assoc($result);
if (!@empty($_POST)) {
  $_SESSION["basket"]["items"][$_POST["id"]]["paintwork"] = $_POST["colour"];
  if (@empty($row["cushions"])) {
    $url = "accessories.php?id=" . $_REQUEST["id"];} 
  elseif (@empty($row["cushions2"])) {
    $url = "accessories2.php?id=" . $_REQUEST["id"];
  } elseif (!@empty($row["glasstop"])) {
    $url = "glasstop.php?id=" . $_REQUEST["id"];
  } else {
    $url = "shopping_cart.php";
  }

And this code in my form button:

<a href="<?php echo (!@empty($row["cushions"])) ? "shopping_cart.php" : "accessories.php?id=" . $_REQUEST["id"]?>" style="float: left; color: #251717; background-color: #DBC87B">NO THANKYOU, PROCEED WITH ORDER</a>

How can amend that so that it will go to the accessories.php page or the accessories2.php page?

I’m not sure what you’re expecting to get back for $row[‘cushions’], $row[‘cushions2’] and whatnot. You don’t need @ before calling empty as it won’t return an error ever … only a true/false. Even then, you should always handle errors instead of simply hiding them.

I also can’t tell if your if clause is looking for an empty value or not. Check your !'s to make sure you have them where they need to be.

Empty() will evaluate a variable as empty in the following circumstances per php manual:

The following things are considered to be empty:

“” (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
“0” (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)

Thank for the reply Keith.

I adopted this site, and other than giving it a bit of a lick of paint ages ago, I don’t have much to do with it. I must admit to being a little out of my depth with some of the PHP code here.

A quick summary - basically its set up with a products table that includes a field ‘Cushions’ which is set to 0 (i.e. empty) by default, but can be set to -1.

Empty (0) indicates that cushions are included in with the product, so there is no need to display the page that offers them (accessories.php).

Not Empty (-1) indicates that cushions are not included with the product, so the accessories.php page needs to display.

The change that brought this about is that there are now 2 sizes of cushions, depending on the product. So I’m trying to get it to display accessories.php if the original cushion field (cushions) is empty / 0. Or display the accessories2.php if the new cushion field (cushions2) is empty / 0.

So from the code above on that page, I’m trying top get it to do the following in plain English:

If cushions and cushions2 are both empty / 0, go to shopping cart.php

If cushions = -1 go to accessories.php

If cushions2 = -1 go to accessories2.php

If you know the value you’re looking for is -1, I’d check it against that instead of using empty(). Since empty can trigger based on a few different values, this prevents bugs in the future if you’re getting unexpected results. I personally don’t like using empty. In my head, empty should only be “not set”, or an empty array. Everything else, including an empty string and the value null are in fact values. But that’s just my opinion.

Try this instead:


$row = mysql_fetch_assoc($result);
if (!empty($_POST)) {
    
    $_SESSION["basket"]["items"][$_POST["id"]]["paintwork"] = $_POST["colour"];
    
    if ($row["cushions"] == -1) {
        $url = "accessories.php?id=" . $_REQUEST["id"];
    } else if ($row["cushions2"] == -1) {
        $url = "accessories2.php?id=" . $_REQUEST["id"];
    } else if (!empty($row["glasstop"])) {
        $url = "glasstop.php?id=" . $_REQUEST["id"];
    } else {
        $url = "shopping_cart.php";
    } 

And:


<a href="<?= $url ?>" style="float: left; color: #251717; background-color: #DBC87B">NO THANKYOU, PROCEED WITH ORDER</a>

Thanks Keith - that;s definitely made some progress.

The value of the fields is working the opposite way - is that correct? So go to the accessories.php page if cushions = -1? That does seem less confusing, as I think there were too many double negatives before.

That seems to work now, but only if the user opts to have the item painted.

The whole form for the input is:


<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>" onsubmit="return validate(this)">
      <label for="quantity" style="float: left; width: 75px; margin: 0">COLOUR</label>
	  <textarea name="colour" id="colour" style="float: left; height: 80px; width: 250px"></textarea>
      <img src="images/spacer.gif" class="clear" style="height: 10px" />
	  <input type="submit" value="ADD COLOUR TO ORDER" id="submit" onmouseover="this.style.color = '#DBC87B'" onmouseout="this.style.color = '#FFFFFF'" style="float: left; margin-left: 75px; margin-right: 10px; width: 255px; padding-bottom: 3px; height: 23px" />
      <a href="<?= $url ?>" style="float: left; color: #251717; background-color: #DBC87B">NO THANKYOU, PROCEED WITH ORDER</a>
      <input type="hidden" name="id" value="<?php echo $_REQUEST["id"]?>" />
	</form>

Which displays an option text box to enter a color, and the ‘No Thank you, proceed with order’ option.

So currently, if the text box is filled in, and the user clicks on ‘Add Colour to Order’ then it correctly goes to either accessories.php or accessories2.php.

But if you click on ‘No thank you, proceed with order’ nothing happens, it just stays on the same page.

If you load the page, and click view source, what does the html for the form look like?


<form method="post" action="/paintwork.php" onsubmit="return validate(this)">
      <label for="quantity" style="float: left; width: 75px; margin: 0">COLOUR</label>
      <textarea name="colour" id="colour" style="float: left; height: 80px; width: 250px"></textarea>
      <img src="images/spacer.gif" class="clear" style="height: 10px" />
      <input type="submit" value="ADD COLOUR TO ORDER" id="submit" onmouseover="this.style.color = '#DBC87B'" onmouseout="this.style.color = '#FFFFFF'" style="float: left; margin-left: 75px; margin-right: 10px; width: 255px; padding-bottom: 3px; height: 23px" />
      <a href="" style="float: left; color: #251717; background-color: #DBC87B">NO THANKYOU, PROCEED WITH ORDER</a>
      <input type="hidden" name="id" value="0" />
</form>

Yeah, it’s either not seeing the value of $url, or your server has PHP short openers disabled. Try changing:


<?= $url ?>

To:


<?php echo $url ?>

Try it again, and if it’s still acting up, paste the full PHP code of the entire page so we can see if it’s a variable scope issue.

Still no joy.

PHP at the top of the page:


&lt;?php
session_start();
$page = "paintwork";
include("../includes/header.php");
include("../includes/db_open.php");
$sql =
  "SELECT p.*, c.`name` AS `categoryname` " .
  "FROM `products` p, `categories` c " .
  "WHERE p.`category` = c.`id` " .
  "AND p.`id` = '" . $_SESSION["basket"]["items"][$_REQUEST["id"]]["product"] . "'";
$result = mysql_query($sql) or die("Query failed : $sql at line " . __line__);
$row = mysql_fetch_assoc($result);
if (!empty($_POST)) {
    
    $_SESSION["basket"]["items"][$_POST["id"]]["paintwork"] = $_POST["colour"];
    
    if ($row["cushions"] == -1) {
        $url = "accessories.php?id=" . $_REQUEST["id"];
    } else if ($row["cushions2"] == -1) {
        $url = "accessories2.php?id=" . $_REQUEST["id"];
    } else if (!empty($row["glasstop"])) {
        $url = "glasstop.php?id=" . $_REQUEST["id"];
    } else {
        $url = "shopping_cart.php";
    }
?&gt;

The header.php has this:


&lt;?php
$root = ($_SERVER["HTTP_HOST"] == "server_ip") ? "/~username" : "";
$preload = array(
  "{$root}/images/1_on.jpg",
  "{$root}/images/2_on.jpg",
  "{$root}/images/3_on.jpg",
  "{$root}/images/4_on.jpg",
  "{$root}/images/5_on.jpg",
  "{$root}/images/6_on.jpg",
  "{$root}/images/7_on.jpg");
if (@empty($keywords)) $keywords = array("Lloyd Loom","garden furniture","wicker furniture","conservatory furniture","outdoor furniture","Lloyd Loom chair","patio garden furniture","rattan furniture");
function keyword() {
  global $keywords;
  static $count = 0;
  $count++;
  if ($count &gt; count($keywords)) $count = 1;
  return $keywords[$count - 1];
}
include("vat.php");
?&gt;

And


&lt;?php
if ($page == "paintwork") {
?&gt;
&lt;script type="text/javascript"&gt;
function validate(form) {
  if (form.elements["colour"].value == "") {
    alert("Please enter a colour")
	form.elements["colour"].focus()
	return false
  }
  return true
}
&lt;/script&gt;
&lt;?php
}
?&gt;

db_open.php has:


&lt;?php
mysql_connect("localhost", "username", "password") or die("Could not connect : " . mysql_error());
mysql_query("SET NAMES 'utf8'");
mysql_select_db("database") or die("Could not select database");
?&gt;

Rest of the page:


&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="refresh" content="0;url=&lt;?php echo $url?&gt;" /&gt;
&lt;/head&gt;
&lt;/html&gt;
&lt;?php
  exit;
}
?&gt;
&lt;div id="trail"&gt;&lt;a href="shop.php"&gt;Shop&lt;/a&gt; &gt; &lt;a href="products.php?category=&lt;?php echo $row["category"]?&gt;"&gt;&lt;?php echo $row["categoryname"]?&gt;&lt;/a&gt; &gt; &lt;a href="product.php?id=&lt;?php echo $row["id"]?&gt;"&gt;&lt;?php echo $row["code"]?&gt;&lt;/a&gt; &gt; &lt;a href="colour.php?id=&lt;?php echo $_REQUEST["id"]?&gt;"&gt;Paintwork&lt;/a&gt;&lt;/div&gt;
  &lt;div class="clear" id="divider"&gt;&lt;img src="images/spacer.gif" alt="&lt;?php echo keyword()?&gt;" /&gt;&lt;/div&gt;
  &lt;h1&gt;Paintwork (Optional)&lt;/h1&gt;
  &lt;p style="margin-bottom: 0"&gt;This product can be painted for an extra &pound;&lt;?php echo $row["paintwork"]?&gt; inc VAT.&lt;/p&gt;
  &lt;p style="margin-bottom: 0"&gt;Please note that this will be a special order, and that delivery will take 12-14 weeks.&lt;/p&gt;
  &lt;p style="margin-bottom: 0"&gt;If you wish to use this service, please describe your chosen colour below:-&lt;/p&gt;
  &lt;div class="left"&gt;
	&lt;form method="post" action="&lt;?php echo $_SERVER["PHP_SELF"]?&gt;" onsubmit="return validate(this)"&gt;
      &lt;label for="quantity" style="float: left; width: 75px; margin: 0"&gt;COLOUR&lt;/label&gt;
	  &lt;textarea name="colour" id="colour" style="float: left; height: 80px; width: 250px"&gt;&lt;/textarea&gt;
      &lt;img src="images/spacer.gif" class="clear" style="height: 10px" /&gt;
	  &lt;input type="submit" value="ADD COLOUR TO ORDER" id="submit" onmouseover="this.style.color = '#DBC87B'" onmouseout="this.style.color = '#FFFFFF'" style="float: left; margin-left: 75px; margin-right: 10px; width: 255px; padding-bottom: 3px; height: 23px" /&gt;
      &lt;a href="&lt;?php echo $url ?&gt;" style="float: left; color: #251717; background-color: #DBC87B"&gt;NO THANKYOU, PROCEED WITH ORDER&lt;/a&gt;
      &lt;input type="hidden" name="id" value="&lt;?php echo $_REQUEST["id"]?&gt;" /&gt;
	&lt;/form&gt;
  &lt;/div&gt;
  &lt;img src="images/spacer.gif" alt="&lt;?php echo keyword()?&gt;" class="clear" style="height: 40px" /&gt;
&lt;?php
include("../includes/footer.php");
?&gt;

Well, this looks suspect:


<html>
<head>
<meta http-equiv="refresh" content="0;url=<?php echo $url?>" />
</head>
</html>
<?php
  exit;
}
?>

I’d get rid of that block. It appears to be closing the HTML page and terminating execution before it even gets to your form data.

And, modified PHP code at the top:


&lt;?php
session_start();
$page = "paintwork";
include("../includes/header.php");
include("../includes/db_open.php");
$sql =
  "SELECT p.*, c.`name` AS `categoryname` " .
  "FROM `products` p, `categories` c " .
  "WHERE p.`category` = c.`id` " .
  "AND p.`id` = '" . $_SESSION["basket"]["items"][$_REQUEST["id"]]["product"] . "'";
$result = mysql_query($sql) or die("Query failed : $sql at line " . __line__);
$row = mysql_fetch_assoc($result);

$url = "shopping_cart.php";
if (!empty($_POST)) {
    
    $_SESSION["basket"]["items"][$_POST["id"]]["paintwork"] = $_POST["colour"];
    
    if ($row["cushions"] == -1) {
        $url = "accessories.php?id=" . $_REQUEST["id"];
    } else if ($row["cushions2"] == -1) {
        $url = "accessories2.php?id=" . $_REQUEST["id"];
    } else if (!empty($row["glasstop"])) {
        $url = "glasstop.php?id=" . $_REQUEST["id"];
    }
?&gt;

With those changes, the ‘No thank you, proceed with order’ works if there are no cushion options, i.e. cushions and cushions2 equal 0 it goes to shopping_cart.php.

And with the Add colour to Order option, it reloads the page, but kills it after the navbar.

I don’t know if the issue is with the lines:


<?php
  exit;
}
?>

Taking them out, Dreamweaver through up an error message, but it didn’t seem very helpful, as it was just at the last line, which is the last of:


<?php
include("../includes/footer.php");
?>

Leaving it in produces the results above.

Taking it out, and the paintwork.php doesn’t load at all - just a white page.

Sorry this is becoming a bit of a pain, but I really appreciate you helping out - hopefully its getting close and is something small to fix it from here.

Looking at it again, is the issue that the ‘No thank you’ link is just that - a link. So the submit button reloads the same page, and the code at the top is processed, and the user is sent on to the correct page.

But just clicking the link doesn’t do that, so no URL is passed.

Going back to the original code:


<a href="<?php echo (!@empty($row["cushions"])) ? "shopping_cart.php" : "accessories.php?id=" . $_REQUEST["id"]?>">NO THANK YOU, PROCEED WITH ORDER</a>

Which is saying:

If ‘cushions’ is not empty then go to shopping_cart.php, otherwise go to accessories.php.

Can that be changed to say:

If ‘cushions’ = -1 then go to accessories.php, If 'cushions2 = -1 then go to accessories2.php, otherwise go to shopping_cart.php?

OK - I had a bit of an epiphany, in that I realised I could just use three links, each wrapped in a bit of conditional code:


<?php if ($row['cushions']=="0" AND $row['cushions2']=="0"){ ?>
      <a href="shopping_cart.php" style="float: left; color: #251717; background-color: #DBC87B">NO THANK YOU, PROCEED WITH ORDER</a><br>
      <?php }  ?>
      <?php if ($row['cushions']=="-1"){ ?>
      <a href="<?php echo "accessories.php?id=" . $_REQUEST["id"]?>" style="float: left; color: #251717; background-color: #DBC87B">NO THANK YOU, PROCEED WITH ORDER</a><br>
      <?php } ?>
      <?php  if ($row['cushions2']=="-1"){ ?>
      <a href="<?php echo "accessories2.php?id=" . $_REQUEST["id"]?>" style="float: left; color: #251717; background-color: #DBC87B">NO THANK YOU, PROCEED WITH ORDER</a><br>
<?php } ?>

Its not as elegant as a single link, but it seems to work.