Looking to implement a condition that prevents the echoing of HTML code when a certain template ID is selected?

Struggling with inherited PHP MVC code for a website, I encounter an issue on a specific page where unwanted code is echoed out. To prevent this, I aim to skip the echoing when a specific templateID (which I set as default) is selected. However, I'm unsure of how to construct the necessary conditional statement. As a novice developer, understanding the structure of an elseif statement proves challenging.

In my attempted solution, I created an else if statement using this query:

SELECT * FROM `template` WHERE `templateID` = 112 ORDER BY `templateID` ASC 
if($_GET['e']) {
    $query2 = "SELECT DISTINCT templateID, name
    FROM template
    INNER JOIN exam ON examID = ".$_GET['e']."
    WHERE active = 'Yes'
    AND FIND_IN_SET(exam.type,template.templateType) > 0
    ORDER BY rank ASC, name ASC;";
}

else {
    $query2 = "SELECT * FROM template WHERE active = 'Yes' AND templateType LIKE '%General%' ORDER BY rank ASC, name ASC;";
}

$rows2 = $db->fetchAll($query2);

foreach($rows2 as $row2)
{
    $templates .= "<option value=".$row2['templateID'].">".$row2['name']."</option>\n";
}


echo 
"
<form method=POST name=Record>
<input type=hidden name=tempContents id=tempContents>
</form>

<form method=POST name=Comm id=CommForm>
<input type=hidden name=e value=\"".(int)$_GET['e']."\">
<div id=\"sendIds\"></div>
";

echo '
<table style="width:100%">
<tr>
    <td style="padding-bottom: 10px;"><b>Template:&nbsp;&nbsp;&nbsp;</b></td>
    <td style="padding-bottom: 10px;"><select name=template onChange="changeTemplate(this)">'.$templates.'</select></td>
</tr>
<tr>
    <td style="padding-bottom: 10px;"><b>Subject:</b></td>
    <td style="padding-bottom: 10px;"><input type=text size=50 name=subject id=subject style="width:400px"></td>
</tr>
<tr>
    <td style="padding-bottom: 10px;" valign=top><b>Attachments:&nbsp;&nbsp;&nbsp;</b><br><br><input type=button class=button value=Add onClick="newAttachment()"></td>
    <td style="padding-bottom: 10px;" valign=top><div id=fileUploads></div></td>
</tr>
<tr>
    <td style="padding-bottom: 10px;"><b>Message:</b></td>
    <td></td>
<tr>
    <td style="padding-bottom: 10px;" colspan=2>
    <textarea id="msg" name="msg" class="tinymce">
    </textarea>
    </td>
</tr>
</table>
';

To achieve my goal, it seems I need another conditional statement. Do I require two separate conditionals, one for the query and one for the echo? If so, how should I proceed in writing them?

Answer №1

If you only want to display the contact form when the chosen template is not the default one, then surround your entire form with a conditional statement.

Important Points:

  • Be cautious of SQL injection vulnerabilities. It's recommended to use parameterized queries
  • Make sure to properly quote all HTML attributes
  • The second form is lacking a closing tag
  • Consider the purpose of the first form if it contains only hidden fields and lacks a submit button

Here's an illustration:

<?php
if ($templateId <> '112') {

    if($_GET['e']) {
        $query = 'SELECT ...';
    } else {
        $query = 'SELECT ...';
    }

    foreach ($db->... as $row) {
        ...
    }

    echo '<form method="post" name="Comm" id="CommForm">...';
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to randomly change the colors of divs for a variable amount of time?

I have a unique idea for creating a dynamic four-square box that changes colors at random every time a button is clicked. The twist is, I want the colors to cycle randomly for up to 5 seconds before 3 out of 4 squares turn black and one square stops on a r ...

When aligning to the right, input box does not wrap

Is there a way to prevent an input box from displaying on a lower line than the menu when using CSS to create a one line menu floated to the right? This is the CSS code being used: <style type="text/css"> #nav { margin:0; padding:0; lis ...

Refine the pandas Dataframe with a filter on a JavaScript-enabled website

I recently inherited a large software project using Python/Flask on the backend and HTML/Javascript on the frontend. I'm now looking to add some interactivity to one of the websites. I have successfully passed a dataframe to the webpage and can displa ...

On smaller screens in portrait mode, CSS automatically incorporates margin adjustments

Here is the code I am working with: <html> <head> <style> body { margin: auto; width: 720px; } </style> </head> <body> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ip ...

Tips for maintaining checkbox checked status through page reloads

Hello there! In my Laravel e-commerce store, I have a list of currency values displayed as checkboxes. When a checkbox is selected, the page reloads to update the currency value. However, the checkbox selection is lost after the page reloads. Any suggest ...

Pictures failing to load on both Chrome and Safari browsers

For some reason, the images on my app are not appearing in Chrome and Safari but show up fine in Firefox. If you try to open the image URL in a browser, it works perfectly. However, when embedded in the HTML document, the image appears broken. I also test ...

What is the best way to maximize your gaming time when one real-life day equals six in-game days?

Here is my issue: In my game, one game day is meant to represent 1/6 of a real world day. Within this game day, there are 12 game hours, making each game hour equivalent to 20 minutes of real time. However, upon observation, I have noticed that instead o ...

Disabling Bootstrap Js, JQuery, and CSS in Yii2

Can anyone suggest an alternative to using bootstrap.css and bootstrap.js? I've attempted the following: 'assetManager' => [ 'bundles' => [ 'yii\bootstrap\BootstrapAsset' => [ & ...

Tips for concentrating on the initial input field produced by an ng-repeat in AngularJS

Currently, I am dynamically generating input fields using ng-repeat and everything is working smoothly. However, my requirement is to set focus on the first input that is generated by this ng-repeat based on their sequenceId values. So far, I have attempte ...

Retrieving Table Cell Values Using Jquery: A Step-by-Step Guide

<table class="table" align="center" id="tblMain"> <tr> <td>Status</td> <td><b><span name="current" value="Dispatch">Dispatch</span></b></td> </tr> </table> JS $(&apos ...

Utilizing jQuery to pinpoint the exact position within a Flexbox container

I have a unique setup with multiple boxes arranged using Flexbox as the container and list tags as individual boxes inside. These boxes are responsive and change position as the width is resized. My goal is to use jQuery to detect which boxes are touching ...

Encountering [Object Object] within an angular2 app

https://i.stack.imgur.com/iceKH.pngI recently created an angular2 application using ngrx/effects for handling http calls. My reference point was an application on GitHub. However, I am facing an issue where the response from the HTTP call is not displaying ...

Positioning JQuery tooltips

I've been developing a simple tooltip tool (check out the fiddle link below), but I'm encountering some issues with positioning. My goal is to have the tooltip appear centered and above the clicked link, however right now it appears at the top le ...

Working with Express for Database Queries

Seeking assistance to retrieve a specific row from the database table when id = 1 (or another value). Attempting localhost:3000/product/1 in a browser, but encountering issues. The code snippet is as follows: const { query } = require('../db/db&apos ...

Having trouble getting autocomplete to work with JQuery UI?

Currently facing issues in implementing the Amazon and Wikipedia Autocomplete API. It seems that a different autocomplete service needs to be used based on the search parameter. Unfortunately, neither of the services work when adding "?search=5" for Wikipe ...

What steps can I take to protect my website from malicious shells and malware uploads?

I run a website for sharing images and videos, but I'm concerned about its security. I've noticed that anyone can upload harmful shells or malware instead of legitimate content. I'm curious how YouTube and Facebook are able to prevent such v ...

Mastering the art of calculating total time in Laravel

I am working on a project related to timesheets, where I have tables for tasks and timers. Each task can have multiple timers associated with it. In my implementation, I have a method called getTotalAttribute in the Timer model which calculates the time d ...

trailblazer navigation

Seeking assistance to set up breadcrumb navigation. My PHP skills are still developing, so any guidance would be appreciated. The following is the structure of my cats in MySQL: cats_id cats_position cats_parentid 1 1> ...

Encountered an error while attempting to load resource in Node.js

I'm currently working on a project utilizing Node js (Express...). Let me do my best to explain the issue I am encountering. My work is being done on localhost, and the main page of my index.html has buttons that lead to other pages when clicked. I c ...

Height of border not being displayed accurately

I have been searching for a solution to my unique issue with inserting borders in HTML and CSS. When I try to add a border to three images, the height does not display correctly. This is all part of my learning process to improve my skills in coding. Belo ...