This is an old revision of the document!
In this activity you will create a form and use it to pass data from one page to another.
form.php
<?php
include('header.php');
include('menu.php');
?>
<main>
<p>Choose your hero.</p>
<ol>
<li><a href="process_form.php?name=Loki">Loki</a></li>
<li><a href="process_form.php?name=Hulk">Hulk</a></li>
</ol>
</main>
<?php
include('footer.php');
?>
process_form.php
<?php
include('header.php');
include('menu.php');
?>
<main>
<p>You have chosen <?php echo $_GET['name']; ?></p>
</main>
<?php
include('footer.php');
?>
<main> tag.<form> tag has a parameter called action. This is where you want to send your data.action to 'process_form.php'.<form action="process_form.php"></form>
<form> tags. <button type="submit" name="name" value="Loki"></button>
<button type="submit" name="name" value="Hulk"></button>
type to 'submit' means that when we click the button it will submit the form and send the data. Both buttons will do this.name is the parameter name to be sent. Submit buttons will often be named 'submit' and not contain data, but sometimes there will be two forms on a page (or sent to a page) and you need to know which one to process. We use that here to differentiate the selected heroes.value is the text that will appear on the button.<form> tag and add the following code. <ul>
<li>
</li>
</ul>
<li>
<label>Hero Alias:</label>
<input type="text" name="alias" size="30" maxlength="50" hint="Iron Man">
</li>
<li>
<label>Identity:</label>
<input type="text" name="identity" size="30" maxlength="50" hint="Tony Stark">
</li>
<label> tag.type="button" or type="submit", it is type="text" which is used for a simple text field, usually up to 100 characters or so.name that will inform 'process_form.php' what the value refers to.size sets the size of the input box to 30 characters and maxlength limits the length of characters that can be entered.hint. <li>
<label>Side:</label>
<input type="radio" name="side" value="Hero" checked="checked"> Hero<br>
<input type="radio" name="side" value="Villain"> Villain
</li>
<input> tag and, like buttons, they have a name and a value.value isn't displayed as it is on a button, because there's no room on a radio button. Instead, the value is the text we want to send to 'process_form.php'.<input depending on how we want the form to appear.checked attribute in the first input. This sets the default item if you want one. <li>
<label>First Appearance:</label>
<input type="number" name="year">
</li>
size and maxlength, but we'll look at better ways to manage this later.<select> field. <li>
<label>Source of Power:</label>
<select name="power">
<option value="skill">Skill</option>
<option value="bio">Biological</option>
<option value="magic">Magic</option>
<option value="tech">Technology</option>
<option value="mutant">Mutant</option>
</select>
</li>
name for each option individually, we set it once in the <select> tag.value attribute, while the user sees the text between the <option> tags.<input type="submit" name="submit" value="Add Hero">
value associated with that option. include('database.php');
<?php
// Set up database connection
define ('DBCONNECT', "../../pdo.php"); // database connection
include DBCONNECT;
$db = 'webdev';
$dsn = "mysql:host=$db_host;dbname=$db;charset=utf8mb4";
try
{ // connect
$pdo = new MyPDO($dsn, $db_user, $db_pass, $db_options);
} catch (\PDOException $e) {
throw new \PDOException ($e->getMessage(), (int)$e->getCode());
}
?>
include commands are together) add some code to get the hero names. // get list of heroes
$query = "SELECT * FROM heroes";
$args = array();
$rslt = $pdo->prepare($query);
$rslt->execute($args);
<li>
<label>Hero</label>
<select name="hero">
<?php
// display list of heroes
while($row = $rslt->fetch()) {
?>
<option value="<?php echo $row['alias']; ?>"><?php echo $row['alias']; ?></option>
<?php
}
?>
</select>
</li>
// get list of movies
$query = "SELECT * FROM appearances";
$args = array();
$mrslt = $pdo->prepare($query);
$mrslt->execute($args);
<?php
// display list of movies
while($row = $mrslt->fetch()) {
?>
<li></li>
<?php
}
?>
$hrslt is now $mrslt because we're looking at the movie query result rather than the hero query result.<li><input type="checkbox" name="movie[]" value="<?php echo $row['movie']; ?>" id=""> <?php echo $row['movie']; ?></li>
[] because we want to send a list of appearances as an array.SELECT * FROM `appearances` GROUP BY movie
SELECT movie FROM `appearances` GROUP BY movie
// get list of movies
$query = "SELECT movie FROM appearances GROUP BY movie";
$args = array();
$mrslt = $pdo->prepare($query);
$mrslt->execute($args);
</ul> tag).<li><input type="checkbox" name="movie[]" value="new" id=""> <input type="text" name="new_movie"></li>
</form> tag.<input type="submit" name="submit" value="Add Appearance">
& . + symbol. This is why we started with just 'Hulk' and 'Thor' in the first activity.