johngordon Posted August 4, 2014 Share Posted August 4, 2014 Hope someone can help put with this - I have inherited a site and am trying to modify some PHP that I am unfamiliar with. I have gotten so far with it, but still have a problem with it. Its a page on an online store offering optional extras depending on the product. The products are furniture, so and there was an option to buy cushions for some. The change I am trying to make is to have an option for two different sizes of cushion depending on the product. So there was a field 'cushions' in the product table, and a page 'accessories.php' that offered the option to add cushions. I have added a new field 'cushions2' and a new page 'accessories2.php' to cover the new size of cushions. The site is here, with this example product: http://www.lloydloomonline.com/product.php?id=32 If you click 'Add to cart' the next page offers you the option to add custom paintwork. If you enter something there, and click on 'Add colour to order' it works - i.e. the next page is the one offering the cushions. But if you just click on 'No thank you, proceed with order' it just reloads the page - basically the URL isn't being puled through as it should be. On that page the PHP at the top of the page looks like this: <?phpsession_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"; }?> And the form with the input / submit looks like this: <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="<?php echo $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> Hope that makes sense - I think it must nearly be there, as all the conditional code seems to work, but the URL isn't being pulled through correctly in the form. If anyone could have a look and hopefully get megabuck on track, I'd really appreciate it. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/290268-if-else-syntax-conditional-link-in-form-submit-button/ Share on other sites More sharing options...
CroNiX Posted August 5, 2014 Share Posted August 5, 2014 I take it you fixed this? When I click on the "NO THANKYOU, PROCEED WITH ORDER" it goes to the shopping_cart.php page. Note that "THANKYOU" really should be 2 words. Quote Link to comment https://forums.phpfreaks.com/topic/290268-if-else-syntax-conditional-link-in-form-submit-button/#findComment-1486825 Share on other sites More sharing options...
johngordon Posted August 5, 2014 Author Share Posted August 5, 2014 No - not fixed, as the 'No thank you' link should only go to shopping_cart.php if both 'cushions' and 'cushions2' are empty / 0. Looking at it again, I have it working with the form submit button which happens if someone chooses to have the paintwork added. That reloads the page, processes the code below, and the user goes to the correct page: $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 { $url = "shopping_cart.php"; } So the trick is to apply that same logic to the link in the form. The original looks like: <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? Quote Link to comment https://forums.phpfreaks.com/topic/290268-if-else-syntax-conditional-link-in-form-submit-button/#findComment-1486855 Share on other sites More sharing options...
johngordon Posted August 5, 2014 Author Share Posted August 5, 2014 OK, I think I have this working now. I had a bit of an epiphany in that I could wrap three links in a bit of conditional code which would work: <?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 it might be, but it seems to work! Quote Link to comment https://forums.phpfreaks.com/topic/290268-if-else-syntax-conditional-link-in-form-submit-button/#findComment-1486859 Share on other sites More sharing options...
Ch0cu3r Posted August 5, 2014 Share Posted August 5, 2014 No need to state your link three times for each condition, Just define the url to be used for the link in a variable. <?php // default url $url = 'shopping_cart.php'; // override default url if cusions or cushions2 availabled if ($row['cushions'] == "-1") { $url = "accessories.php?id=" . $_REQUEST["id"]; } elseif ($row['cushions2']=="-1") { $url = "accessories2.php?id=" . $_REQUEST["id"]; } // output the link ?> <a href="<?php echo $url ?>" style="float: left; color: #251717; background-color: #DBC87B">NO THANK YOU, PROCEED WITH ORDER</a><br> Quote Link to comment https://forums.phpfreaks.com/topic/290268-if-else-syntax-conditional-link-in-form-submit-button/#findComment-1486862 Share on other sites More sharing options...
johngordon Posted August 5, 2014 Author Share Posted August 5, 2014 I get you - that works too. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/290268-if-else-syntax-conditional-link-in-form-submit-button/#findComment-1486865 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.