Jump to content

print selections from checkbox array populated with DB query *help*


DarkJamie

Recommended Posts

Hello, I have a bit of a problem. I am pretty new to PHP and have had a website thrust upon me to fix. I've been searching far and wide for a solution for my checkbox issue, however, most of the things I've found only explain how to display predefined values of said checkboxes.

 

Here's what I'm trying to do. I have a list of reports in my MySQL database that the users will be allowed to select from. I've assigned checkboxes to each report description with it's price. What I need now is when the user hit submit, the next page displays only the report descriptions and price checked in the list, along with a total price. I haven't got to the sum of price values yet, as I can't even get the items to display.

 

Here is the list page:

 

http://www.fantiniresearch.com/report_list.php

 

Here is my report list code:

	echo "<center> Sorry! there were no Files that matched your search. </center>";
    
	}
  		


	echo "<h2>Select the reports you would like to purchase</h2>";
	echo "<br>";


	echo "<form action='file_select.php' method='POST'>";



	while ($row=mysql_fetch_array($sql_result)) {
			$id = $row["id"];
    	    $file_number=$row["file_number"];
      		$filename=$row["filename"];
      		$description=$row["description"];
			$price=$row["price"];
			$active=$row["active"];
			        

		echo "<table border=0>";
		echo "<tr>";
		echo "<td width=10px><input type='checkbox' name='item[]' value=$id</td>";
		echo "<td width=300px>$description</td>";
		echo "<td><b>$price</b></td>";
		echo "</tr>";
		echo "</table>";

	}

	echo "<br><input type='submit' class='button' value='Continue' tabindex='2'>"; 


//		
	echo "</form>";

 

Here is the file_select.php code:

Echo "<h2>Individual Reports</h2>";

  $aReport = $_POST['item'];
  if(empty($aReport)) 
  {
    echo("You didn't select any reports.");
  } 
  else 
  {
    $N = count($aReport);

    echo("You selected $N report(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo($aReport[$i] . " ");
    }
  }

 

Any help would be highly appreciated.

 

Link to comment
Share on other sites

I will have to get back to you on apache logs. For now, here is the complete code for the report_list.php:

<?php
$path = $_SERVER['DOCUMENT_ROOT'];

include ($path.'/incs/dbsetup.php');

ini_set('display_errors',1);
error_reporting(E_ALL);


$connect=mysql_connect ($server,$user,$pword) or die ("Could Not connect to MySQL Server");

    $db=mysql_select_db ($dbname,$connect) or die ("Could not select MySQL Database");

  		$sql="select * from downloads order by file_number";

	$sql_result=mysql_query($sql,$connect) or die ("Could Not Query Database");

	$num=mysql_numrows($sql_result);

		if($num == 0) {

	echo "<center> Sorry! there were no Files that matched your search. </center>";
    
	}
  		


	echo "<h2>Select the reports you would like to purchase</h2>";
	echo "<br>";


	echo "<form action='file_select.php' method='POST'>";



	while ($row=mysql_fetch_array($sql_result)) {
			$id = $row["id"];
    	    $file_number=$row["file_number"];
      		$filename=$row["filename"];
      		$description=$row["description"];
			$price=$row["price"];
			$active=$row["active"];
			        

		echo "<table border=0>";
		echo "<tr>";
		echo "<td width=10px><input type='checkbox' name='item[]' value=$id</td>";
		echo "<td width=300px>$description</td>";
		echo "<td><b>$price</b></td>";
		echo "</tr>";
		echo "</table>";

	}

	echo "<br><input type='submit' class='button' value='Continue' tabindex='2'>"; 


//		
	echo "</form>";
	echo "<br>";
?>

 

The dbsetup.php is fine, as the site has been up for a couple of years with all other functionality working ok.

 

Link to comment
Share on other sites

Here is the code for file_select.php

 

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
include ($path.'/incs/dbsetup.php');

ini_set('display_errors',1);
error_reporting(E_ALL);


Echo "<h2>Individual Reports</h2>";

  $aReport = $_POST['item'];
  if(empty($aReport)) 
  {
    echo("You didn't select any reports.");
  } 
  else 
  {
    $N = count($aReport);

    echo("You selected $N report(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo($aReport[$i] . " ");
    }
  }


?>

 

Link to comment
Share on other sites

I am going to put my money on the incorrect HTML

echo "<td width=10px><input type='checkbox' name='item[]' value=$id</td>";

should be

echo "<td width=10px><input type='checkbox' name='item[]' value='$id' /></td>";

 

Also on your file_select.php I would do this.

$path = $_SERVER['DOCUMENT_ROOT'];
include ($path.'/incs/dbsetup.php');

ini_set('display_errors',1);
error_reporting(E_ALL);

echo "<h2>Individual Reports</h2>";
if (isset($_POST['item'])) {
echo 'You selected '.count($_POST['item']).' items<br/>';
foreach ($_POST['item'] as $value) {
	echo $value . ' ';
}
} else {
echo 'Nothing Selected';
}

Link to comment
Share on other sites

Thanks Buddski,

 

I was just reading up on isset when your post came through. I changed the code as you suggested, but still get no results. Now instead of a T_VARIABLE error, I get a T_STRING error. Here is the error log:

 

parse error, unexpected T_STRING in /html/file_select.php on line 11, referer: /html/report_list.php

 

As with both parsing errors, I've checked semicolons and bracets and such, but can't seem to find what's wrong.

Link to comment
Share on other sites

I've added the pre print code as suggested and that line now errors before it gets to the previous error:

 

arse error, unexpected T_CONSTANT_ENCAPSED_STRING in /html/file_select.php on line 8, referer: /report_list.php

 

Just to be certain, here is the code from the report_list.php file:

 

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
include ($path.'/incs/dbsetup.php');

ini_set('display_errors',1);
error_reporting(E_ALL);

echo '<pre>'.print_r($_POST,true).'</pre>';

Echo "<h2>Individual Reports</h2>";

if (isset($_POST['item'])) {
echo 'You selected '.count($_POST['item']).' items<br/>';
foreach ($_POST['item'] as $value) {
	echo $value . ' ';
}
} else {
echo 'Nothing Selected';
}


?>

 

Link to comment
Share on other sites

Here's the report_list.php code:

 

<?php
$path = $_SERVER['DOCUMENT_ROOT'];

include ($path.'/incs/dbsetup.php');

ini_set('display_errors',1);
error_reporting(E_ALL);


$connect=mysql_connect ($server,$user,$pword) or die ("Could Not connect to MySQL Server");

    $db=mysql_select_db ($dbname,$connect) or die ("Could not select MySQL Database");

  		$sql="select * from downloads order by file_number";

	$sql_result=mysql_query($sql,$connect) or die ("Could Not Query Database");

	$num=mysql_numrows($sql_result);

		if($num == 0) {

	echo "<center> Sorry! there were no Files that matched your search. </center>";
    
	}
  		


	echo "<h2>Select the reports you would like to purchase</h2>";
	echo "<br>";


	echo "<form action='file_select.php' method='POST'>";



	while ($row=mysql_fetch_array($sql_result)) {
			$id = $row["id"];
    	    $file_number=$row["file_number"];
      		$filename=$row["filename"];
      		$description=$row["description"];
			$price=$row["price"];
			$active=$row["active"];
			        

		echo "<table border=0>";
		echo "<tr>";
		echo "<td width=20px><input type='checkbox' name='item[]' value='$id'</td>";
		echo "<td width=300px >$description</td>";
		echo "<td><b>$price</b></td>";
		echo "</tr>";
		echo "</table>";

	}

	echo "<br><input type='submit' name='submit' class='button' value='Continue' tabindex='2'>"; 


//		
	echo "</form>";
	echo "<br>";
?>

 

Link to comment
Share on other sites

I've simplified the actual checkbox line in my table and fixed the syntax error of the missing >.

 

report_list.php code:

 

<?php
$path = $_SERVER['DOCUMENT_ROOT'];

include ($path.'/incs/dbsetup.php');

ini_set('display_errors',1);
error_reporting(E_ALL);


$connect=mysql_connect ($server,$user,$pword) or die ("Could Not connect to MySQL Server");

    $db=mysql_select_db ($dbname,$connect) or die ("Could not select MySQL Database");

  		$sql="select * from downloads order by file_number";

	$sql_result=mysql_query($sql,$connect) or die ("Could Not Query Database");

	$num=mysql_numrows($sql_result);

		if($num == 0) {

	echo "<center> Sorry! there were no Files that matched your search. </center>";
    
	}
  		


	echo "<h2>Select the reports you would like to purchase</h2>";
	echo "<br>";


	echo "<form action='file_select.php' method='POST'>";



	while ($row=mysql_fetch_array($sql_result)) {
			$id = $row["id"];
    	    $file_number=$row["file_number"];
      		$filename=$row["filename"];
      		$description=$row["description"];
			$price=$row["price"];
			$active=$row["active"];
			        

		echo "<table border=0>";
		echo "<tr>";
		echo "<td width=320px><input type='checkbox' name='item[]' value='$id'> $description</td>";
		echo "<td><b>$price</b></td>";
		echo "</tr>";
		echo "</table>";

	}

	echo "<br><input type='submit' name='submit' class='button' value='Continue' tabindex='2'>"; 


//		
	echo "</form>";
	echo "<br>";
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.