new to php need help

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
Atl Felipe
Forum Newbie
Posts: 2
Joined: Fri Feb 24, 2006 7:17 am
Contact:

new to php need help

Post by Atl Felipe »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have a form that I want verify if an email address is in my database and then if it is download a file.   The part that checks the database works but I can't get the remainder of the code to work.  Please help.  I can get each part to work seperatly but not together.

Code: Select all

<form id="download_form" name="download_form" method="get">
        <table width="395" border="0" cellspacing="1" cellpadding="2">
          <tr>
              <td width="215"><input name="email" type="text" id="email"></td>
              <td width="169"><em>:Email Address</em></td>
          </tr>
		  <tr><td colspan="2"><select name="file_name" id="file_name">
 		  <option value="jca">Java Calendar jar</option>
		  <option value="bmi">Body Mass Index jar</option>
		  <option value="calendar">Calendar jar</option>
		  <option value="mailtest">Mail Test jar</option>
		  <option value="sendmail">Send Mail jar</option>
		  <option value="pwdgen">Password Generator jar</option>
		  <option value="textscroller">Text Scroller jar</option>
		  <option value="viewsource">View WebPage Source jar</option>
		  <option value="htmlcalendar">HTML Calendar Generator Application</option>
		  <option value="passwordassistant">Password Assistant 2.0</option>
		  </select></td></tr>
                  <tr>
		  	<td colspan="2"><input type="Submit" value="Download"></td>
		  </tr>
		 
				<?php
                                // donwload file from database
				function getFile($file)
				{
					echo "<tr><td>$file . in the function</td></tr>";
					$query = "Select item_number, jar_file, jar_name FROM jars WHERE jar_name ='$file'";
					$result = mysql_query($query) or die('Error, query failed');
					list($item_number, $jar_file, $jar_name) =  mysql_fetch_array($result);
						
					header("Content-length: $item_number");
					header("Content-type: $jar_file");
					header("Content-Disposition: attachment; filename=$jar_name");
				}
				if(isset($_GET['file_name']))
				{
					//openning the database
					include 'php\opendb1.php';
					
					//assigning variables
					$e_id = $_GET['email'];
					$f_id = $_GET['file_name'];
					
					//checking to see if the email address is already in the database if not it will be added        to the database	
					$email_query = "SELECT email_address, date FROM email WHERE email_address = '$e_id'";
					$e_result = mysql_query($email_query) or die ('Error, query failed');
					
					//get number of rows returned in the result set
					$num=mysql_numrows($e_result);
					echo "<tr><td>$f_id</td></tr>";
					//if no email address is returned then email address will be added to the database
					if($num == 0)
					{
						$now = date("Ymd");
						$insert_query = "INSERT INTO email VALUES ('$e_id','$now')";
						mysql_query($insert_query) or die('Error, insert query failed');
						echo "<tr><td><font color='white'><em>email address added to database</em></font></td></tr>";
						
					}
					else
					{
						$i=0;
						while ($i < $num) 
						{
							$my_results = mysql_result($e_result,$i,"email_address");
							echo "<tr><td><font color='white'><em>'$my_results' already in our database</em></font></td></tr>";
							$i++;
						}
					}
					getFile($f_id);
					include 'php/closedb.php';
					
					exit;
				}
				
				?>

</table>
      </form>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

A file cannot be downloaded on a page that has output already started (by your HTML above this functionality.) Move this functionality above the start of any HTML or any other output. To get the output where you'd like to display it later on, store it into a variable instead of echoing it. If you want to have the HTML and the download to happen, then you'll need to do the output and add a redirection that requests the file separately.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Or you could assign a session for the validated email addresses, and provide a link to the download page for them to download.
Atl Felipe
Forum Newbie
Posts: 2
Joined: Fri Feb 24, 2006 7:17 am
Contact:

Thanks

Post by Atl Felipe »

Thanks for the help, greatly appericated :D
Post Reply