Hi,
I am having trouble submitting checked boxes.
The HTML
<form action="?<?php htmlout($action);?>" method="post">
<input type="hidden" name="search" value="class">
<input type="submit" value="<?php htmlout($button); ?>">
<!-- <?php
/*echo "<pre>";
print_r($row);
echo "</pre>";*/
?> -->
<fieldset>
<legend>Search</legend>
<table>
<tr>
<td>
<table class="inner" id="searchTable">
<th>Exam Type:</th>
<?php foreach($examSearch as $searchExam): ?>
<tr><td class="left"><input type="checkbox" class="year" id="<?php htmlout($searchExam['name']); ?>" name="exams[]" value="<?php htmlout($searchExam['name']); ?>">
<?php htmlout($searchExam['name']); ?></td></tr>
<?php endforeach; ?>
</table>
</td>
<td>
<table class="inner" id="searchYear">
</table>
</td>
</tr>
</table>
</fieldset>
</form>
The controller file,
<?php
if (isset($_GET['search']))
{
$actionStudent = 'student';
$action = 'search';
$button = 'Submit';
include 'search.html.php';
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'search')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try
{
$sql = 'SELECT subject FROM halbform WHERE name = :name';
$s = $pdo->prepare($sql);
$s->bindValue(':name', $_POST['exams']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error fetching search from halbform.';
include 'error.html.php';
exit();
}
$row = $s->fetch();
include 'search.html.php';
exit();
}
What am I doing wrong?
Thanks
I’m not entirely sure, however, if you are relying on the ?whatever
in the action to be a GET
for the POST
, I don’t think that works. Try putting in another hidden field with that value and see if that fixes it.
HTH,
$_POST[‘exams’] is an array. You cant use it this way. Are you trying ot run multiple selections?
I thought that just sends $action as in this case ‘search’ but search has no value because of the ‘?’.
The HTML is,
<form action="?<?php htmlout($action);?>" method="post">
<input type="hidden" name="action" value="search">
<input type="submit" value="<?php htmlout($button); ?>">
<?php
if(isset($row)){
echo("Blah");
/*echo "<pre>";
print_r($row);
echo "</pre>";*/
}
?>
<fieldset>
<legend>Search</legend>
<table>
<tr>
<td>
<table class="inner" id="searchTable">
<th>Exam Type:</th>
<?php foreach($examSearch as $searchExam): ?>
<tr><td class="left"><input type="checkbox" class="year" id="<?php htmlout($searchExam['name']); ?>" name="exams[]" value="<?php htmlout($searchExam['name']); ?>">
<?php htmlout($searchExam['name']); ?></td></tr>
<?php endforeach; ?>
</table>
</td>
<td>
<table class="inner" id="searchYear">
</table>
</td>
</tr>
</table>
</fieldset>
</form>
I see now that i should be iterating through the array. I tried this,
if (isset($_GET['search']))
{
$actionStudent = 'student';
$action = 'search';
$button = 'Submit';
include 'search.html.php';
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'search')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try
{
$sql = 'SELECT subject FROM halbform WHERE exam = :name';
$s = $pdo->prepare($sql);
foreach ($_POST['exams'] as $examination)
{
$s->bindValue(':name', $examination);
$s->execute();
}
}
catch (PDOException $e)
{
$error = 'Error fetching search from halbform.';
include 'error.html.php';
exit();
}
$row = $s->fetch();
include 'search.html.php';
exit();
}
Thanks
Yes i have changed that now to simply
<form action="?" method="post">
The DB looks like this,
The HTML is,
<form action="?" method="post">
<input type="hidden" name="action" value="search">
<input type="submit" value="<?php htmlout($button); ?>">
<?php
if(isset($row)){
/*echo("Blah");*/
echo "<pre>";
print_r($row);
echo "</pre>";
}
?>
<fieldset>
<legend>Search</legend>
<table>
<tr>
<td>
<table class="inner" id="searchTable">
<th>Exam Type:</th>
<?php foreach($examSearch as $searchExam): ?>
<tr><td class="left"><input type="checkbox" class="year" id="<?php htmlout($searchExam['name']); ?>" name="exams[]" value="<?php htmlout($searchExam['name']); ?>">
<?php htmlout($searchExam['name']); ?></td></tr>
<?php endforeach; ?>
</table>
</td>
<td>
<table class="inner" id="searchYear">
</table>
</td>
</tr>
</table>
</fieldset>
</form>
the index.php
if (isset($_GET['search']))
{
$actionStudent = 'student';
$action = 'search';
$button = 'Submit';
include 'search.html.php';
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'search')
{
include $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/db.inc.php';
$button = 'Submit111';
try
{
$sql = 'SELECT subject FROM halbform WHERE exam = :name';
$s = $pdo->prepare($sql);
foreach ($_POST['exams'] as $examination)
{
$s->bindValue(':name', $examination);
$s->execute();
}
}
catch (PDOException $e)
{
$error = 'Error fetching search from halbform.';
include 'error.html.php';
exit();
}
$row = $s->fetch();
include 'search.html.php';
exit();
}
This outputs $row as
Array
(
[subject] => Geography
[0] => Geography
)
which is only one row where the exam is AS and the subject is Geography. But there are three subject for that exam in the DB. How can I get all instances not just the first?
Thanks
ofeyofey:
$row = $s->fetch();
Think carefully about where this line is, and what it’s placement means in terms of execution.
1 Like
Hi,
I haven’t moved the line $row = $s->fetch(); but i have changed it to,
$row = $s->fetchAll(PDO::FETCH_ASSOC);
This does give all the subjects in an array for a particular checked exam type. But if I check two exams it only gives the subjects for the last checked exam type. So I think $row is being overwritten for each exam type. I need to somehow concatenate it.not sure how to do that.
Thanks
The positioning is still the issue. You’ve correctly identified what it’s doing, but now take steps to correct it.
I have tried it in three different positions now,
just outside the foreach as here
if (isset($_POST['action']) and $_POST['action'] == 'search')
{
include $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/db.inc.php';
$button = 'Submit111';
try
{
$sql = 'SELECT subject FROM halbform WHERE exam = :name';
$s = $pdo->prepare($sql);
foreach ($_POST['exams'] as $examination)
{
$s->bindValue(':name', $examination);
$s->execute();
//$row = $s->fetchAll(PDO::FETCH_ASSOC);
}
$row = $s->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e)
{
$error = 'Error fetching search from halbform.';
include 'error.html.php';
exit();
}
//$row = $s->fetch();
//$row = $s->fetchAll(PDO::FETCH_ASSOC);
include 'search.html.php';
exit();
}
also inside the foreach and where it was originally. All three give me the same result.
Inside the foreach is the correct location; however you want to store the information somewhere you wont later overwrite. [Hint: [FPHP]array_merge[/FPHP]]
Aaha! Yes i see what you mean. I will work on this.
Thanks
Ok That is working nicely now.
$tot = array();
try
{
$sql = 'SELECT subject FROM halbform WHERE exam = :name';
$s = $pdo->prepare($sql);
foreach ($_POST['exams'] as $examination)
{
$s->bindValue(':name', $examination);
$s->execute();
$row = $s->fetchAll(PDO::FETCH_ASSOC);
$tot = array_merge($tot, $row);
}
Thanks for your help,
1 Like
Only comment i would make is that you do not need to bindValue inside the foreach - instead bindParam outside the foreach; because bindParam uses a reference instead of the value. It’s a very minor thing.
1 Like
Thanks I can change it to bindValue but surely it won’t work if that line is outside the foreach.
Thanks
bindParam will work outside the loop; bindValue will not. It’s somewhat of a personal-preference thing in this case, so far as i know.
IE:
(bindValue is as you have it)
bindParam would be:
$tot = array();
try
{
$sql = 'SELECT subject FROM halbform WHERE exam = :name';
$s = $pdo->prepare($sql);
$s->bindParam(':name', $examination);
foreach ($_POST['exams'] as $examination)
{
$s->execute();
$row = $s->fetchAll(PDO::FETCH_ASSOC);
$tot = array_merge($tot, $row);
}
system
Closed
November 16, 2015, 10:25pm
16
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.