passing variables via a html form, no results showing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dragon_agility
Forum Newbie
Posts: 3
Joined: Wed Mar 18, 2009 12:02 pm

passing variables via a html form, no results showing

Post by dragon_agility »

Hi!

I've got a php project connected to a local database where I've created a form where users can search for a book based on it's subject. When the user presses search, the information should be posted to another php page within my project and the filtered information shown within a table.

However, although there are no syntax errors, the variables are not being passed. The page is blank.

I was wondering if anyone could look over my code and help me with any suggestions? I'm fairly new to php (6 months) and it's frustrating my greatly.

thanks.


index 1st page.php

Code: Select all

<?php
$db = new mysqli ('localhost','root','','library');
$results = $db->query('select distinct subject from catalogue order by subject');
?>
<form action="view.php?library" method="POST">
<select name="library">
<option>All Subjects</option>
<?php while ($row = $results->fetch_assoc()){?>
<option><?php echo $row['subject']?></option>
<?php }?>
<input name="library" input type="submit" value="search" />
</select>
</form>
<?php
$db->close();
?>


view page.php

Code: Select all

<?php
$library = $_POST['library'];
$db = new mysqli ('localhost','root','','library');
$sql = "select author, title, description from catalogue where subject = '{$_POST['library']}' order by title";
$results = $db->query($sql);
?>
<table border="3" cellpadding="8">
<?php while($rows = $results->fetch_array(MYSQLI_ASSOC)){ ?>
<tr>
<td width="300px" border="1"><?php echo $row['author'] ?></td>
<td width="300px" border="1"><?php echo $row['title'] ?></td>
</tr>
<tr><td width="500px" border="1"><?php echo $row['description'] ?></td></tr>
<?php } ?>
</table>
 
<?php $db->close(); ?>

Thanks again!
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Re: passing variables via a html form, no results showing

Post by William »

dragon_agility wrote:Hi!

I've got a php project connected to a local database where I've created a form where users can search for a book based on it's subject. When the user presses search, the information should be posted to another php page within my project and the filtered information shown within a table.

However, although there are no syntax errors, the variables are not being passed. The page is blank.

I was wondering if anyone could look over my code and help me with any suggestions? I'm fairly new to php (6 months) and it's frustrating my greatly.

thanks.


index 1st page.php

Code: Select all

<?php
$db = new mysqli ('localhost','root','','library');
$results = $db->query('select distinct subject from catalogue order by subject');
?>
<form action="view.php?library" method="POST">
<select name="library">
<option>All Subjects</option>
<?php while ($row = $results->fetch_assoc()){?>
<option><?php echo $row['subject']?></option>
<?php }?>
<input name="library" input type="submit" value="search" />
</select>
</form>
<?php
$db->close();
?>


view page.php

Code: Select all

<?php
$library = $_POST['library'];
$db = new mysqli ('localhost','root','','library');
$sql = "select author, title, description from catalogue where subject = '{$_POST['library']}' order by title";
$results = $db->query($sql);
?>
<table border="3" cellpadding="8">
<?php while($rows = $results->fetch_array(MYSQLI_ASSOC)){ ?>
<tr>
<td width="300px" border="1"><?php echo $row['author'] ?></td>
<td width="300px" border="1"><?php echo $row['title'] ?></td>
</tr>
<tr><td width="500px" border="1"><?php echo $row['description'] ?></td></tr>
<?php } ?>
</table>
 
<?php $db->close(); ?>

Thanks again!
I didn't read your entire post but you're setting both your HTML select list and submit button to "library". Also the HTML for your submit button has "input" randomly in the middle of the tag.
dragon_agility
Forum Newbie
Posts: 3
Joined: Wed Mar 18, 2009 12:02 pm

Re: passing variables via a html form, no results showing

Post by dragon_agility »

William wrote: I didn't read your entire post but you're setting both your HTML select list and submit button to "library". Also the HTML for your submit button has "input" randomly in the middle of the tag.
OMG thank you, it works :D

I'm such a newbie heh
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Re: passing variables via a html form, no results showing

Post by William »

dragon_agility wrote:
William wrote: I didn't read your entire post but you're setting both your HTML select list and submit button to "library". Also the HTML for your submit button has "input" randomly in the middle of the tag.
OMG thank you, it works :D

I'm such a newbie heh
We all started somewhere. :-) Glad I could help.
Post Reply