Reputation: 444
So yesterday I was bored and created a flash card web application. I basically used an HTML landing page that has 10 different text areas that push the content entered from the text area into a PHP variable. Each text area had a name "questionone" "questiontwo" and so on. However, is there a cleaner and easier way to do this?
<?php
$questionone = $_POST['questionone'];
$questiontwo = $_POST['questiontwo'];
$questionthree = $_POST['questionthree'];
$questionfour = $_POST['questionfour'];
$questionfive = $_POST['questionfive'];
$questionsix = $_POST['questionsix'];
$questionseven = $_POST['questionseven'];
$questioneight = $_POST['questioneight'];
$questionnine = $_POST['questionnine'];
$questionten = $_POST['questionten'];
$answerone = $_POST['answerone'];
$answertwo = $_POST['answertwo'];
$answerthree = $_POST['answerthree'];
$answerfour = $_POST['answerfour'];
$answerfive = $_POST['answerfive'];
$answersix = $_POST['answersix'];
$answerseven = $_POST['answerseven'];
$answereight = $_POST['answereight'];
$answernine = $_POST['answernine'];
$answerten = $_POST['answerten'];
?>
<div class="two"><p><?php echo "$questionone"; ?></p></div>
<div class="three"><p><?php echo "$questiontwo"; ?></p></div>
<div class="two"><p><?php echo "$questionthree"; ?></p></div>
<div class="three"><p><?php echo "$questionfour"; ?></p></div>
<div class="two"><p><?php echo "$questionfive"; ?></p></div>
<div class="three"><p><?php echo "$questionsix"; ?></p></div>
<div class="two"><p><?php echo "$questionseven"; ?></p></div>
<div class="three"><p><?php echo "$questioneight"; ?></p></div>
<div class="two"><p><?php echo "$questionnine"; ?></p></div>
<div class="three"><p><?php echo "$questionten"; ?></p></div>
</div>
Upvotes: 0
Views: 216
Reputation: 10548
I will suggest you to give all questions with one name i.e. question[]
and, all answers with one name i.e. answer[]
<form method='POST' action='submitPage.php'>
<textarea name='question[]'></textarea> <br>
<textarea name='question[]'></textarea> <br>
<textarea name='question[]'></textarea> <br>
.
.
<textarea name='answer[]'></textarea> <br>
<textarea name='answer[]'></textarea> <br>
<textarea name='answer[]'></textarea> <br>
.
.
.
</form>
submitPage.php
<?
$question=$_POST['question'];
$answer=$_POST['answer'];
$TotalQuestion=sizeof($question);
for($i=0;$i<$TotalQuestion;$i++)
{
$Question=$question[$i];
$Answer=$answer[$i];
?>
<div class="Question"><p><?php echo $Question;?></p></div>
<div class="Answer"><p><?php echo $Answer;?></p></div>
<?}?>
Upvotes: 0
Reputation: 1135
If you already know what the questions are (and there's only a few) and don't need to rely on posted values, you could use an array.
$questions = array('question1', 'question2', 'question3');
$i = 1;
foreach($questions as $question) {
echo "Question " . htmlspecialchars($i) . ":" . " " . htmlspecialchars($question) . '<br>';
$i++;
}
Or if in future you need to pull from a big set of quesitons, use a DB:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT id, questiontext FROM tbleQuestions");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
Upvotes: 0
Reputation: 491
<?php
foreach ($_POST as $key => $post) {
${$key} = $post;
}
?>
<div class="two"><p><?php echo "$questionone"; ?></p></div>
<div class="three"><p><?php echo "$questiontwo"; ?></p></div>
<div class="two"><p><?php echo "$questionthree"; ?></p></div>
<div class="three"><p><?php echo "$questionfour"; ?></p></div>
<div class="two"><p><?php echo "$questionfive"; ?></p></div>
<div class="three"><p><?php echo "$questionsix"; ?></p></div>
<div class="two"><p><?php echo "$questionseven"; ?></p></div>
<div class="three"><p><?php echo "$questioneight"; ?></p></div>
<div class="two"><p><?php echo "$questionnine"; ?></p></div>
<div class="three"><p><?php echo "$questionten"; ?></p></div>
</div>
OR
<?php
$i = 0;
foreach ($_POST as $post) {
if ($i%2) {
echo '<div class="two"><p>' . $post . '</p></div>';
} else {
echo '<div class="three"><p>' . $post . '</p></div>';
}
$i++;
}
?>
Upvotes: 0
Reputation: 3627
While the other answers appear to be correct, most of them are not utilizing htmlspecialchars or issets to prevent warnings. Try something like this:
<?php $trusted_post = array( 'questionone', 'questiontwo', 'questionthree', 'questionfour', 'questionfive', 'questionsix', 'questionseven', 'questioneight', 'questionnine', 'questionten', 'answerone', 'answertwo', 'answerthree', 'answerfour', 'answerfive', 'answersix', 'answerseven', 'answereight', 'answernine', 'answerten'); ?>
<?php foreach($trusted_post as $loopKey => $postKey): ?>
<?php if(isset($_POST[$postKey])): ?>
<div class="<?php echo ($loopKey%2===0)?'two':'three'; ?>"><p><?php echo htmlspecialchars( $_POST[ $postKey ] ); ?></p></div>
<?php endif; ?>
<?php endforeach; ?>
Upvotes: 2
Reputation: 8560
You can use arrays in html like this:
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
and process this data in php like this:
$name = $_POST['name'];
foreach( $name as $v ) {
print $v . '<br/>';
}
Upvotes: 2