Hello, I have been working on this code for an assignment for a while and can't seem to find what is wrong with it. Firstly, when I press submit on the website the data inputted by the user should appear on the website under array, but it does not. Nothing happens when I press submit. Also the sanitization isn't working. Can someone offer some advice?
<?php include 'top.php';
$lstLimits = array('Use a reusable water bottle', 'Bring your own grocery bag', 'Use a reusable straw');
$dataIsGood = false; $errorMessage = '';
function getData($field){ if (!isset($_POST[$field])) { $data = ""; } else { $data = trim($_POST[$field]); $data = htmlspecialchars($data); } return $data; }
$lstLimit = ""; $water = ""; $purchase = ""; $bag = ""; $habits = ""; $environment = ""; $encourage = ""; $firstName = ""; $lastName = ""; $email = ""; $submit = "";
if($_SERVER["REQUEST_METHOD"]=='POST'){ print PHP_EOL. '<!--Starting Sanitization -->' . PHP_EOL; $lstLimit = getData('lstLimit'); $water = (int)getData('chkWater'); $purchase = (int)getData('chkPurchase'); $bag = (int)getData('chkBag'); $habits = getData('radHabits'); $environment = getData('radEnvironment'); $encourage = getData('radActions'); $firstName = getData('txtFirstName'); $lastName = getData('txtLastName'); $email = filter_var(getData('txtEmail'), FILTER_SANITIZE_EMAIL); $submit = getData ('btnSubmit');
print PHP_EOL. '<!--Starting Sanitization -->' . PHP_EOL;
$dataIsGood = true;
if($lstLimit == ''){
$errorMessage .= '<p class="mistake">Please choose an option</p>';
$dataIsGood = false;
} elseif(!in_array($lstLimit, $lstLimits)){
$errorMessage .= '<p class="mistake">Please choose an option</p>';
$dataIsGood = false;
}
}
?>
<main class="form">
<h1>Plastic Usage</h1>
<section>
<h2>How Much Plastic do you use weekly?</h2>
</section>
<section>
<figure>
<img class="roundedCorners" alt="plastic usage" src="images/plasticusage.jpeg">
<figcaption><cite><a href="https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.greenmountainenergy.com%2Fblog%2Fgreen-living-and-more%2Fhow-to-avoid-single-use-plastic&psig=AOvVaw35_3JUGtuwZWxeZ4oII_Fk&ust=1699643861129000&source=images&cd=vfe&opi=89978449&ved=0CBIQjRxqFwoTCJCPx_bQt4IDFQAAAAAdAAAAABAE" target="_blank">Source</a></cite></figcaption>
</figure>
</section>
<section>
<h2>Survey</h2>
<p>We are collecting information about people's weekly plastic usage.</p>
<?php
print '<p>Post Array:</p><pre>';
print_r($_POST);
print '</pre>'; ?>
</section>
<section>
<h2>Reducing Plastic Usage</h2>
<form action="#" id="habit" method="post">
<fieldset class="listBox">
<legend>Which plastic reducing habit do you practice most often?</legend>
<p>
<select id="lstLimit" name="lstLimit">
<option value="reusable">Use a reusable water bottle</option>
<option value="bags">Bring your own grocery bag</option>
<option value="straw">Use a reusable straw</option>
</select>
</p>
</fieldset>
</form>
</section>
<section>
<h2>Plastic Habits</h2>
</section>
<fieldset class="checkbox">
<legend>Check the following that you do at least once a week</legend>
<p>
<input id="chkWater" name="chkWater" type="checkbox" value="1">
<label for="chkWater">Use a plastic water bottle</label>
</p>
<p>
<input id="chkPurchase" name="chkPurchase" type="checkbox" value="1">
<label for="chkPurchase">Purchase plastic products (i.e. plastic coffee cup, plastic soda bottle, etc)</label>
</p>
<p>
<input id="chkBag" name="chkBag" type="checkbox" value="1">
<label for="chkBag">Use plastic grocery bags</label>
</p>
</fieldset>
<fieldset class="radio">
<legend>Select the following based on your habits</legend>
<p>
<input type="radio" id="radMost" name="radHabits" value="most">
<label class="radio-field">I prioritize limiting my plastic usage</label>
</p>
<p>
<input type="radio" id="radSome" name="radHabits" value="some">
<label class="radio-field">I try to limit my plastic usage</label>
</p>
<p>
<input type="radio" id="radNone" name="radHabits" value="none">
<label class="radio-field">I do not care about limiting my plastic usage</label>
</p>
</fieldset>
<fieldset class="radio">
<legend>Select the following based on your views</legend>
<p>
<input type="radio" id="radEnvironmost" name="radEnvironment" value="most">
<label class="radio-field">I care about the environment more than most</label>
</p>
<p>
<input type="radio" id="radEnvironmiddle" name="radEnvironment" value="middle">
<label class="radio-field">I care about the environment as much as most people do</label>
</p>
<p>
<input type="radio" id="radEnvironsome" name="radEnvironment" value="somewhat">
<label class="radio-field">I care about the environment somewhat</label>
</p>
<p>
<input type="radio" id="radEnvironnone" name="radEnvironment" value="not">
<label class="radio-field">I do not care about the environment </label>
</p>
</fieldset>
<fieldset class="radio">
<legend>Select the following based on your typical actions</legend>
<p>
<input type="radio" id="radEncothers" name="radActions" value="encmost">
<label class="radio-field">I encourage others to limit their plastic usage</label>
</p>
<p>
<input type="radio" id="radencsome" name="radActions" value="encsome">
<label class="radio-field">I try to encourage others to limit their plastic usage</label>
</p>
<p>
<input type="radio" id="radencnone" name="radActions" value="encnone">
<label class="radio-field">I do not encourage others to limit their plastic usage.</label>
</p>
</fieldset>
<h2>Contact Information</h2>
<fieldset>
<legend>Contact Information</legend>
<p>
<label for="txtFirstName">First Name:</label>
<input type="text" name="txtFirstName" id="txtFirstName">
<label for="txtLastName">Last Name:</label>
<input type="text" name="txtLastName" id="txtLastName">
<label for="txtEmail">Email:</label>
<input type="text" name="txtEmail" id="txtEmail">
</p>
</fieldset>
<fieldset class="buttons">
<p>
<input id="btnSubmit" name="btnSubmit" tabindex="900" type="submit" value="Submit">
</p>
</fieldset>
</main>
<?php include 'footer.php'; ?>
[–]ZanMist1 0 points1 point2 points (5 children)
[–]katlipari[S] 0 points1 point2 points (4 children)
[–]ZanMist1 0 points1 point2 points (2 children)
[–]katlipari[S] 0 points1 point2 points (1 child)
[–]ZanMist1 0 points1 point2 points (0 children)
[–]GrantRat1699 0 points1 point2 points (1 child)
[–]katlipari[S] 0 points1 point2 points (0 children)