4 Replies - 920 Views - Last Post: 19 April 2013 - 07:32 AM Rate Topic: -----

#1 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

Checkbox Form with Submit Button doesn't echo values from database

Posted 18 April 2013 - 12:19 PM

I'm new to Php, html, and mysql. I have a database with a username, product, and price they want to sell the product for. The code is for a checkbox form with a submit button that works like a price filter, that when submitted it's supposed to echo the username, product, and price that matches the corresponding checkbox. . If a user selects a checkbox labeled, "0-25" or "100 & above", and clicks the submit button it supposed to echo all users that have items in that price range. I've looked online an tried to manipulate somewhat close to what I have. Could someone please help me out or give me insight on how to do this? Any help is appreciated.

Here is the code to my checkbox form.

<form action="pricefilter.php" method="post">
    <br><b>Filter By Price:</b><br><br> 
    <input type="checkbox" name="$0-$25[]" id="Price" value="0-25"/>&nbsp;$0-$25<br><br>
    <input type="checkbox" name="$25-$50[]" id="Price" value="25-50"/>&nbsp;$25-$50<br><br>
    <input type="checkbox" name="$50-$100[]" id="Price" value="50-100"/>&nbsp;$50-$100<br><br>
    <input type="submit" name="submit" value="Submit" />
</form>



Here my php code that I have so far:


<?php
mysql_connect ("localhost", "root","root")  or die (mysql_error());
mysql_select_db ("xuswapuser");
 
$term = $_POST['price'];
 
$sql = mysql_query("select * from Books where Price like '%$price%'");

        if(IsChecked('$0-$25','0-25'))
        {
            echo 'Username: '.$row['id'];
            echo '<br/> Product: '.$row['Product'];
           echo '<br/> Condition: '.$row['Condition'];
           echo '<br/> Price: '.$row['Price'];
           echo '<br/><br/>';
        }
        if(IsChecked('$25-$50','25-50'))
        {
            echo 'Username: '.$row['id'];
            echo '<br/> Product: '.$row['Product'];
            echo '<br/> Condition: '.$row['Condition'];
           echo '<br/> Price: '.$row['Price'];
           echo '<br/><br/>';
        }
		if(IsChecked('$50-$75', '50-75'))
		{
			echo 'Username: '.$row['id'];
                        echo '<br/> Product: '.$row['Product'];
                        echo '<br/> Condition: '.$row['Condition'];
                       echo '<br/> Price: '.$row['Price'];
                       echo '<br/><br/>';
			
		}
		
		 if(IsChecked('$75-$100','0-25'))
        {
            echo 'Username: '.$row['id'];
            echo '<br/> Product: '.$row['Product'];
            echo '<br/> Condition: '.$row['Condition'];
            echo '<br/> Price: '.$row['Price'];
            echo '<br/><br/>';
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Checkbox Form with Submit Button doesn't echo values from database

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,354
  • Joined: 12-December 12

Re: Checkbox Form with Submit Button doesn't echo values from database

Posted 18 April 2013 - 01:00 PM

I would definitely not use those combination of symbols as names?! I would probably name the checkboxes "price0", "price1", or "price0", "price25".

If you want to use PHP to process the checkboxes as an array then you could use "price[]" for all of them, and this indicates part of the problem(s) you may be encountering.

The mysql library is deprecated and should not be used for new code - particularly if you are just starting out! And using BR tags to separate content is a poor practice: look into css.. soon-ish.

MORE FUNDAMENTAL is that ids must be unique on the page. I am very surprised that you have not discovered this from the tutorials that you are studying.

And your variable is $term, not $price.

This post has been edited by andrewsw: 18 April 2013 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

Re: Checkbox Form with Submit Button doesn't echo values from database

Posted 18 April 2013 - 01:28 PM

Ok thank you I was able to fix some of it but and I am getting some output. However instead of echoing the value that matches the checkbox selected, instead it keeps echo everything in the table instead of what matches the specific price value. Could you please show me where I went wrong.


<?php
mysql_connect ("localhost", "root","root")  or die (mysql_error());
mysql_select_db ("xuswapuser");
 
$term = $_POST['term'];
 
$sql = mysql_query("select * from Books where Price like '%$term%'");


function IsChecked($chkname,$value)
    {
        if(!empty($_POST[$chkname]))
        {
            foreach($_POST[$chkname] as $chkval)
            {
                if($chkval == $value)
                {
                    return true;
                }
            }
        }
        return false;
    }
while ($row = mysql_fetch_array($sql)){
        if(IsChecked('price025','<=25'))
        {
    echo 'Username: '.$row['id'];
    echo '<br/> Product: '.$row['Product'];
    echo '<br/> Condition: '.$row['Condition'];
    echo '<br/> Price: '.$row['Price'];
    echo '<br/><br/>';
        }
        if(IsChecked('price2550','25-50'))
        {
	echo 'Username: '.$row['id'];
    echo '<br/> Product: '.$row['Product'];
    echo '<br/> Condition: '.$row['Condition'];
    echo '<br/> Price: '.$row['Price'];
    echo '<br/><br/>';
        }
		
		if(IsChecked('price5075', '50-75'))
		{
	echo 'Username: '.$row['id'];
    echo '<br/> Product: '.$row['Product'];
    echo '<br/> Condition: '.$row['Condition'];
    echo '<br/> Price: '.$row['Price'];
    echo '<br/><br/>';
			
		}
		
		 if(IsChecked('price75100','75-100'))
        {
    echo 'Username: '.$row['id'];
    echo '<br/> Product: '.$row['Product'];
    echo '<br/> Condition: '.$row['Condition'];
    echo '<br/> Price: '.$row['Price'];
    echo '<br/><br/>';
        }
        

}

 
?> 


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,354
  • Joined: 12-December 12

Re: Checkbox Form with Submit Button doesn't echo values from database

Posted 18 April 2013 - 02:21 PM

Use print_r($_POST); to check what appears in your post array, and post this output here. Use View/Source in your browser as this output will be formatted and easier to read. You could post your revised HTML as well.

Also, why are you using LIKE in your sql statement? You know exactly what values to expect (particularly as your post-data hasn't been checked or sanitised).

This post has been edited by andrewsw: 18 April 2013 - 02:21 PM

Was This Post Helpful? 0
  • +
  • -

#5 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

Re: Checkbox Form with Submit Button doesn't echo values from database

Posted 19 April 2013 - 07:32 AM

View Postandrewsw, on 18 April 2013 - 09:21 PM, said:

Use print_r($_POST); to check what appears in your post array, and post this output here. Use View/Source in your browser as this output will be formatted and easier to read. You could post your revised HTML as well.

Also, why are you using LIKE in your sql statement? You know exactly what values to expect (particularly as your post-data hasn't been checked or sanitised).


Sorry I'm new to this. Here's my updated code but I still can't get the values to echo into a dynamic table.


<?php
mysql_connect ("localhost", "root","root")  or die (mysql_error());
mysql_select_db ("xuswapuser");
 
$priceFilter = $_GET['priceFilter'];
 
 $filteredResponse = array ();
foreach($priceFilter as $range)
{
if($range == 025)
    {
        $query = "select * from Books where Price <= 25";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
		
		
		 
    }

    if($range == 2550)
    {
        $query = "select * from Books where Price >= 25 AND Price <=50";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }
     if($range == 5075)
    {
        $query = "select * from Books where Price >= 50 AND Price <=75";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }
	
	 if($range == 75100)
    {
        $query = "select * from Books where Price >= 75 AND Price <=100";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }
	 if($range == 100)
    {
        $query = "select * from Books where Price >= 100";
        $sql = mysql_query($query);

        array_push($filteredResponse, $sql);
    }
	
	foreach($filteredResponse as $range)
{
	echo $range['id'];
	}
    // do this for all the conditions and you can save the results in an array of arrays
}
 
?>
<html>
<head>
<title> 
XUSWAP 
</title>
<script type="text/javascript" src="sorttable.js"></script>
</head>
<body style="margin: 0; padding: 0;">
       <div id="header" style="background-color:#339900;height:157px;width:100%;position:relative;">                                      <!--header area-->
            <form id="headerForm" name="headerForm" method="Post" action="" align="right">
                  <input type="image" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSN3XIAe50cjq5Cf91GbAywPkChmI5HOAqYxgmRN8F5OhW7I_RT5Q" alt="Placeholder" align="left" width="150" height="150">
                  <br><br><br>
                  <input type="text" name="searchInput"id="search" style="width:500px;"/>
                  <input type="submit" name="searchButton" id="searchButton" value="Search" />
                  <input type="submit" name="logoutButton" id="logoutButton" value="Logout"/>
                  <br><br><br>
                  
            </form>
       </div> 
       <div id="background" style="background-color:#FFD700;height:100%; width:100%;"> 
 <div id = "pageContent"></div>
 <div id="background" style="background-color:#FFD700;height:100%; width:100%;"> 
<blockquote><blockquote><p>Books </p></blockquote><blockquote>
                          
                                    <table class="sortable" width="940" height="52" border="0">
                                    <tr>
                                      <td width="200" id="sortable">Username</td>
                                      <td width="253"> Product</td>
                                      <td width="220">Condition</td>
                                      <td width="249">Price</td>
                                    </tr>
                                    <?php do { ?>
                                      <tr>
                                        <td><?php echo $range['id']; ?></td>
                                        <td><?php echo $range['Product']; ?></td>
                                        <td><?php echo $range['Condition']; ?></td>
                                        <td><?php echo $range['Price']; ?></td>
                                      </tr>
                                      <?php } while ($range = mysql_fetch_assoc($sql)); ?>
                                  </table>
                               




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1