Reputation: 21
I'm having some issues with some functions within PHP/HTML. I'm generally a VBA guy, so this is a bit odd to me. I'm trying to create a form that a user can fill in, and have the submit button either:
So far, this is the code I've managed to put together. I also have no clue if there is code in there that is completely useless.
<!DOCTYPE html>
<html>
<head>
<style>
.error {
color: #FF0000;
}
</style>
</head>
<body>
<?php function test_input($data) { $data=t rim($data); $data=s tripslashes($data); $data=h tmlspecialchars($data); return $data; } ?>
<align left></align>
Name:</br>
<input type="text" select name="Name" placeholder="Name">
<br>
<br>
<input type="checkbox" name="Alergy" value="Alergy">Alergy
<input type="checkbox" name="Gluten" value="Gluten">Gluten Free</br>
</br>
<text>City</text>
</br>
<Align Center>
<select name="City">
<option value=""></option>
<option value="Arvada">Arvada</option>
<option value="Boulder">Boulder</option>
<option value="Ft. Collins">Ft. Collins</option>
<option value="Greeley">Greeley</option>
<option value="Littleton">Littleton</option>
<option value="Longmont">Longmont</option>
<option value="Loveland">Loveland</option>
<option value="Thornton">Thornton</option>
<option value="Out of State">Out of State</option>
</select>
</br>
<Text>Prefered Meal</Text>
</br>
<Align Center>
<select name="Meal">
<option value=""></option>
<option value="Chicken">Chicken</option>
<option value="Beef">Beef</option>
<option value="Fish">Fish</option>
<option value="vegetarian">vegetarian</option>
</select>
<Align Center>
</br>
<text></text>
</br>
<input type="checkbox" name="RSVP" value="RSVP">RSVP
<br>
</form>
<input type="submit" value="Submit">
</br>
</form>
</Align>
</body>
</html>
Upvotes: 1
Views: 209
Reputation: 1822
Stateless web applications are a different animal from desktop apps. Coming from a VBA background means you need to think about this differently.
The submit button itself cannot perform any action on the server. Here's why...
The browser loads and submits data to an HTTP server without knowledge of what happened before - it is stateless. Web developers have bolted on a stateful mechanism using various types of cookies in order for the server side to be able to retain knowledge about the HTTP requests coming in.
Even though PHP allows you to mix server side PHP code beside HTML code in your source file it does not mean that the PHP code is executed when a submit button is pressed.
Submit buttons cause the browser to send a brand new request to the HTTP server with the data that the HTML document has specified - in the case of a form, it sends form data. The method of sending form data is determined by the method
attribute on the form tag. The method
attribute changes where the data is available in PHP ($_GET or $_POST) and also can alter the URL in the browser (if the method
is GET
)
When this new request is processed on the server the entire PHP file is reloaded and re-executed without any knowledge of the previous page load. It is entirely possible for someone to write a bot that sends data directly to your program.
If you need to keep track of a logged in user (out of scope of this question) you would use sessions or cookies. Even still, the PHP file would have no knowledge if the page had been accessed prior to a form submission.
Upvotes: 1
Reputation: 20439
This is rather a broad question, but the structure could be like so:
<?php
// Init database here
if ($_POST)
{
// Save to database here
// Redirect to self if successful, and then exit
// Put validation/errors in an array if not
// (i.e. let the page render in a POST op)
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
.error {
color: #FF0000;
}
</style>
</head>
<!-- remainder of your HTML here -->
I notice that you are missing your opening <form>
tag too. You'll want something like this:
<form method="post">
You don't need an action
if you are posting to the same page.
Upvotes: 0