Forms PHP + Javascript Help

I just started learning PHP a couple hours ago using these forums and now I am stuck. Thanks to the good people of this forum I have learned a lot, but I need some direct aide at this moment.

I have used Javascript to hide/reveal a short form. The intent of the form I am creating should store data into a text file, and display the stored content every time the submit button is hit. This is where the conflict lies.

Conflicts

  1. I have to press submit twice in order to display the contents of my text file
  2. Using the browser’s refresh button will duplicate saved data

*I have been experimenting with a few solutions, none of which were correct.

My Code (Below)

Device Manager
<h2>DEVICE INVENTORY: MOBILE TEAM</h2>
<?php		

	$textfile = 'insert.txt';				

	if (file_exists($textfile) && is_readable($textfile))		
	{			
		$data = file($textfile);		
	}


	//if ($i==0)

		//for ($i=0; $i<1; $i++) {
		//}	

	//else	

	
	for($i = 0; $i <= sizeof($data); $i++)		
	{			

		//separate each element and store in temp array			
		$tmp = explode(":", $data[$i]);						


		//assign each element to array			
		$data1[] = array('name' => $tmp[0], 'mdn' => $tmp[1], 'carrier' => $tmp[2], 'meid' => $tmp[3], 'version' => $tmp[4], 'date' => $tmp[5], 'notes' => $tmp[6], 'voice' => $tmp[7]);		
	}		
	echo "<table cellspacing='0' cellpadding='14'>";		
	if ($data1!=""){			
		echo "<tr><th style='background:#6699FF'>Device Name</th><th style='background:#6699FF'>MDN</th><th style='background:#6699FF'>Carrier</th><th style='background:#6699FF'>MEID/IMEI</th><th style='background:#6699FF'>OS Version</th><th style='background:#6699FF'>Received Date</th><th style='background:#6699FF'>Notes</th><th style='background:#6699FF'>Voice</th></tr>";			
		foreach ($data1 as $key => $row)			
		{				
		
			$name[$key]= $row['name'];		
			$mdn[$key]= $row['mdn'];		
			$carrier[$key]= $row['carrier'];		
			$meid[$key]= $row['meid'];
			$version[$key]= $row['version'];
			$date[$key]= $row['date'];
			$notes[$key]= $row['notes'];
			$voice[$key]= $row['voice'];				
			
			echo "<tr><td>{$name[$key]}</td><td>{$mdn[$key]}</td><td>{$carrier[$key]}</td><td>{$meid[$key]}</td><td>{$version[$key]}</td><td>{$date[$key]}</td><td>{$notes[$key]}</td><td>{$voice[$key]}</td></tr>";			
		}		
	}		
	echo "</table>";

?>
			<tr>
				<td height="10" width="750">
				<img alt="" width="1" height="10" src="MsSpacer.gif"></td>
			</tr>
		</table>
		</td>
	</tr>


	<tr>


		<td valign="top" height="180" width="750">
		<!-- MSCellType="ContentBody" -->

		<!-- Create id, hide and finalize expandable content -->
		<ul id="addDevice">
		<li>Add New Device<ul>
<?php if (isset($_POST['submit'])) { //open the file for writing $fh = fopen("insert.txt","a+") or die("File Close"); $name= $_POST['name']; $mdn= $_POST['mdn']; $carrier= $_POST['carrier']; $meid= $_POST['meid']; $version= $_POST['version']; $date= $_POST['date']; $notes= $_POST['notes']; $voice= $_POST['voice']; $notes=stripslashes($notes); //implement validation if (empty($name)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($mdn)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($carrier)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($meid)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($version)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($date)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } else { //assemble the data into a line variable $data= $name . ":" . $mdn . ":" . $carrier . ":" . $meid . ":" . $version . ":" . $date . ":" . $notes . ":" . $voice . ":" . "\r\n"; //write the data to the file if (fwrite($fh, $data)){ $output = " *Successfully Submitted Mobile Device Information!"; } //close the file fclose($fh); } } ?> Device Listing body{ font: 10px Verdana, Arial, Helvetica, sans-serif; } <?php if($output!=""){ echo $output; } ?> " method = "post">
<tr><td><b>Select Device Platform (Android/iOS/MISC):</b></td><td>		
<select name="platform" style="width:200px;"> 	
<option>Android</option>	
<option>iOS</option>
<option>MISC</option>		
</select></td></tr>

<tr><td><b>Select Device Owner (Mobile Team/Biz Dev):</b></td><td>		
<select name="owner" style="width:200px;"> 	
<option>Mobile Team</option>	
<option>Biz Dev</option>		
</select></td></tr>	
</table>	
<input type="submit" name="submit" value="Update" />	
</div>
</tr>
</table>
    </ul>
</li>
Enter Device Name:
Enter MDN:
Enter Carrier:
Enter MEID/IMEI:
Enter OS Version:
Enter Received Date:
Enter Notes:
Is Voice Enabled (Yes/No): Yes No

Well, first, you must place code into the correct tags. “#” tags for HTML/CSS/JS code and PHP tags for PHP code.
Now, to do that it makes it possible for us to copy in one button and place into our editors to review your code.
Looking quickly at your code, there are more than one page, but you lumped it into one long list. If you have more than one file, then, place each in a separate tag and place the name above it. Something like this:

index.php :
[php]
some listing of this file…
[/php]

update.php
[php]
some other file listing…
[/php]

In this way we can make better sense of all that code you posted…

Now a quick look down the page shows two pages, the second has a form that calls itself.
Since PHP is runs on the server and not on the client, this can cause issues when posting to yourself.
But, anyway, explain showing two pages but, talking about one…

Yes, the php does call it self so that it can continously display updated information from the text file that is being populated. And I didn’t notice the code tags for posting, I will try to use them. Although if you copied the code I posted and placed into a notepad document and then uploaded the file to a server you would be able to see how the script works as well.

Beginning Javascript

[code]

Device Manager [/code]

PHP script that reads the contents of a created text file (that acts as a small database), and then echo’s the information back in a loop so long that data exists. *This is where my conflict is
[php]

DEVICE INVENTORY: MOBILE TEAM

<?php
  $textfile = 'insert.txt';            

  if (file_exists($textfile) && is_readable($textfile))      
  {         
     $data = file($textfile);      
  }


  //if ($i==0)

     //for ($i=0; $i<1; $i++) {
     //}   

  //else   

  
  for($i = 0; $i <= sizeof($data); $i++)      
  {         

     //separate each element and store in temp array         
     $tmp = explode(":", $data[$i]);                  


     //assign each element to array         
     $data1[] = array('name' => $tmp[0], 'mdn' => $tmp[1], 'carrier' => $tmp[2], 'meid' => $tmp[3], 'version' => $tmp[4], 'date' => $tmp[5], 'notes' => $tmp[6], 'voice' => $tmp[7]);      
  }      
  echo "<table cellspacing='0' cellpadding='14'>";      
  if ($data1!=""){         
     echo "<tr><th style='background:#6699FF'>Device Name</th><th style='background:#6699FF'>MDN</th><th style='background:#6699FF'>Carrier</th><th style='background:#6699FF'>MEID/IMEI</th><th style='background:#6699FF'>OS Version</th><th style='background:#6699FF'>Received Date</th><th style='background:#6699FF'>Notes</th><th style='background:#6699FF'>Voice</th></tr>";         
     foreach ($data1 as $key => $row)         
     {            
     
        $name[$key]= $row['name'];      
        $mdn[$key]= $row['mdn'];      
        $carrier[$key]= $row['carrier'];      
        $meid[$key]= $row['meid'];
        $version[$key]= $row['version'];
        $date[$key]= $row['date'];
        $notes[$key]= $row['notes'];
        $voice[$key]= $row['voice'];            
        
        echo "<tr><td>{$name[$key]}</td><td>{$mdn[$key]}</td><td>{$carrier[$key]}</td><td>{$meid[$key]}</td><td>{$version[$key]}</td><td>{$date[$key]}</td><td>{$notes[$key]}</td><td>{$voice[$key]}</td></tr>";         
     }      
  }      
  echo "</table>";

?>

[/php]

The following code is setting up the table, the table is still under the java tag so that it can hide/show. The table is created with php as well. So there is a mix of the two languages, this portion of the code works fine. The table displays, the dimensions are correct, and the submit button works. There is some validation as well. It is the code I posted before that needs tweaking.

[code]

        <tr>
           <td height="10" width="750">
           <img alt="" width="1" height="10" src="MsSpacer.gif"></td>
        </tr>
     </table>
     </td>
  </tr>


  <tr>


     <td valign="top" height="180" width="750">
     <!-- MSCellType="ContentBody" -->

     <!-- Create id, hide and finalize expandable content -->
     <ul id="addDevice">
      <li>Add New Device<ul>
<?php if (isset($_POST['submit'])) { //open the file for writing $fh = fopen("insert.txt","a+") or die("File Close"); $name= $_POST['name']; $mdn= $_POST['mdn']; $carrier= $_POST['carrier']; $meid= $_POST['meid']; $version= $_POST['version']; $date= $_POST['date']; $notes= $_POST['notes']; $voice= $_POST['voice']; $notes=stripslashes($notes); //implement validation if (empty($name)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($mdn)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($carrier)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($meid)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($version)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($date)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } else { //assemble the data into a line variable $data= $name . ":" . $mdn . ":" . $carrier . ":" . $meid . ":" . $version . ":" . $date . ":" . $notes . ":" . $voice . ":" . "\r\n"; //write the data to the file if (fwrite($fh, $data)){ $output = " *Successfully Submitted Mobile Device Information!"; } //close the file fclose($fh); } } ?> Device Listing body{ font: 10px Verdana, Arial, Helvetica, sans-serif; } <?php if($output!=""){ echo $output; } ?> " method = "post">
Enter Device Name:
Enter MDN:
Enter Carrier:
Enter MEID/IMEI:
Enter OS Version:
Enter Received Date:
Enter Notes:
Is Voice Enabled (Yes/No): Yes No
Select Device Platform (Android/iOS/MISC): Android iOS MISC
Select Device Owner (Mobile Team/Biz Dev): Mobile Team Biz Dev
[/code] [code][/code]

I still do not understand your code. You have lots of unrelated code not one looks like a real web page.

Okay, let’s go back to basics.

First, on ANY web page, you can send headers only ONE (1) time. You have two groups of headers in your code. ( )

Then, on ANY web page, you can have only ONE (1) title for the page. You have two. ( )

Next, on ANY web page, you can have only ONE (1) body of HTML. You have two. ( )

PHP code can be placed anywhere on a page within PHP tags. You have done this correctly.

Javascript or JQuery is usually placed into the headers just to keep it away from the body of the HTML so that it is easier to view. You have done this correctly.

Placing your original code into my editor, showed many errors. First, you have extra tags. Extra tags. And, two unopened (not unclosed, but, unopened!) tags. These UL errors may be because of the header tags as they it appears that the first open header tag is running to the LAST ending header tag. This is causing some structure errors. It appears that the full page your posted was copied-pasted from two other pages and thus created extra invalid layouts.

So, to start remove the extra headers and body tags and fix the UL/LI matching tags. Then, let us know what you get for errors and we will attack it again for you. And, if this is just one page, only place the entire page into one PHP tag above. Thanks for the reposting, I am sure we can figure this out for you!

Thank you for the tips on how to clean up my code. Hopefully now it will be read easier. I actually did not know the rules about 1 header, title and body per html page. But that is why I am here, to learn. This is my code all cleaned up. It is only one file so I will paste the entire code. Hopefully from this point a fix to my problem can be derived. Thank you in advance.

[code]

Device Manager
<h2>DEVICE INVENTORY: MOBILE TEAM</h2>
<?php		

	$textfile = 'insert.txt';				

	if (file_exists($textfile) && is_readable($textfile))		
	{			
		$data = file($textfile);		
	}



	for($i = 0; $i <= sizeof($data); $i++)		
	{			

		//separate each element and store in temp array			
		$tmp = explode(":", $data[$i]);						


		//assign each element to array			
		$data1[] = array('name' => $tmp[0], 'mdn' => $tmp[1], 'carrier' => $tmp[2], 'meid' => $tmp[3], 'version' => $tmp[4], 'date' => $tmp[5], 'notes' => $tmp[6], 'voice' => $tmp[7]);		
	}		
	echo "<table cellspacing='0' cellpadding='14'>";		
	if ($data1!=""){			
		echo "<tr><th style='background:#6699FF'>Device Name</th><th style='background:#6699FF'>MDN</th><th style='background:#6699FF'>Carrier</th><th style='background:#6699FF'>MEID/IMEI</th><th style='background:#6699FF'>OS Version</th><th style='background:#6699FF'>Received Date</th><th style='background:#6699FF'>Notes</th><th style='background:#6699FF'>Voice</th></tr>";			
		foreach ($data1 as $key => $row)			
		{				
		
			$name[$key]= $row['name'];		
			$mdn[$key]= $row['mdn'];		
			$carrier[$key]= $row['carrier'];		
			$meid[$key]= $row['meid'];
			$version[$key]= $row['version'];
			$date[$key]= $row['date'];
			$notes[$key]= $row['notes'];
			$voice[$key]= $row['voice'];				
			
			echo "<tr><td>{$name[$key]}</td><td>{$mdn[$key]}</td><td>{$carrier[$key]}</td><td>{$meid[$key]}</td><td>{$version[$key]}</td><td>{$date[$key]}</td><td>{$notes[$key]}</td><td>{$voice[$key]}</td></tr>";			
		}		
	}		
	echo "</table>";

?>
			<tr>
				<td height="10" width="750">
				<img alt="" width="1" height="10" src="MsSpacer.gif"></td>
			</tr>
		</table>
		</td>
	</tr>


	<tr>


		<td valign="top" height="180" width="750">
		<!-- MSCellType="ContentBody" -->

		<!-- Create id, hide and finalize expandable content -->
		<ul id="addDevice">
		<li>Add New Device<ul>
<?php if (isset($_POST['submit'])) { //open the file for writing $fh = fopen("insert.txt","a+") or die("File Close"); $name= $_POST['name']; $mdn= $_POST['mdn']; $carrier= $_POST['carrier']; $meid= $_POST['meid']; $version= $_POST['version']; $date= $_POST['date']; $notes= $_POST['notes']; $voice= $_POST['voice']; $notes=stripslashes($notes); //implement validation if (empty($name)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($mdn)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($carrier)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($meid)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($version)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } elseif (empty($date)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored
"; } else { //assemble the data into a line variable $data= $name . ":" . $mdn . ":" . $carrier . ":" . $meid . ":" . $version . ":" . $date . ":" . $notes . ":" . $voice . ":" . "\r\n"; //write the data to the file if (fwrite($fh, $data)){ $output = " *Successfully Submitted Mobile Device Information!"; } //close the file fclose($fh); } } ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />	
<style type="text/css">		
	body{			
		font: 10px Verdana, Arial, Helvetica, sans-serif;		
	}	
</style>
<?php if($output!=""){ echo $output; } ?> " method = "post">
<tr><td><b>Select Device Platform (Android/iOS/MISC):</b></td><td>		
<select name="platform" style="width:200px;"> 	
<option>Android</option>	
<option>iOS</option>
<option>MISC</option>		
</select></td></tr>

<tr><td><b>Select Device Owner (Mobile Team/Biz Dev):</b></td><td>		
<select name="owner" style="width:200px;"> 	
<option>Mobile Team</option>	
<option>Biz Dev</option>		
</select></td></tr>	
</table>	
<input type="submit" name="submit" value="Update" />	
</div>
</tr>
</table>
    </ul>
</li>
[/code]
Enter Device Name:
Enter MDN:
Enter Carrier:
Enter MEID/IMEI:
Enter OS Version:
Enter Received Date:
Enter Notes:
Is Voice Enabled (Yes/No): Yes No

This is probably only going to make you angry, but it’s good advice: If you just started learning a language a couple hours ago, you shouldn’t start on such large projects. You should probably learn some by reading and watching tutorials, and start out small. Then when you want to do a large project, build it incrementally, so it’s easy to know what went wrong. If you really have as much experience as you say, you have no business taking on a project like this as your first. It’s bound to be riddled with errors, and the debugging will take 10-20x as long as the writing.

Now, after cleaning up the code, do you still have the same problems when you try to run it? If you have to click something twice still, that sounds like it’s a JS issue. How many hours ago did you start learning that? ;D

So, your new version is better. Thanks for the clean post of it. Much easier to load into my DW editor…

Now, so far as I see, you have to sort out your

    and
  • tags. They are messed up and causing issues.
    So, here is a sample from your code:

    • Add New Device

        Normally, this would be something like:


        • Add New Device
        • (With as many of these as needed!)

        Here you can read about this: http://www.w3schools.com/tags/tag_ul.asp
        (Also, on that site, there are tutorials on everything!)

        Now, your “unordered lists” are not matched and can mess up the rest of the page…
        So, go thru your code and look for each starting

          and the ending tags
        .
        Make sure each one is matched. If you have two opening ones, there should be two endings…
        Next, INSIDE one pair of these UL’s, you would have LI’s. Make sure all of these are matched.
      • ,
      • Then, test your page and tell us what error you are receiving.
        Lastly, if you add these two lines after your first <?PHP line, PHP will show all errors.
        Not all errors from PHP are shown by default! These lines will show ALL PHP errors and may show some
        error that is not being displayed. (Although, your PHP code looks good as far as I read it.)
        I am letting you solve this one, but, will help when you get stuck…
        [php]
        error_reporting(E_ALL);
        ini_set(‘display_errors’, ‘1’);
        [/php]
        Let us know the verdict… (And, please repost your code, it saves us time! thanks!)

Now that is what I call help! Thank you ErnieAlex!! But now I have a question. I do have ending

    and
  • tags. Although I may not have them in the correct spot, but I was not sure if it mattered at all. This is what I did. I have


    • Add New Device

      as you pointed out, but at the end of my form implementation I added the closing

        and
      • tags. I have evolved my code a bit more, although I still have the same two conflicts I mentioned at the start of this post. I will continue to keep reading tutorials and keeping my eyes out on other forum posts. I think I am close to a resolution.

        In any case, should I move my closing

          and
        • ?

    Well, not sure what you want the lists (or unordered lists) for. If you have this format:

    • Add New Device

        The UL is opened and then you place a LI (list item) into the UL. But, then, you close the UL and not the LI…
        As I said before, normally the lists would be handled something like this…

        • Add New Device
        • Delete Device
        • some other list item

        So, I am not sure what you are attempting to do with this list, but, I think this section:

        • Add New Device

            Needs to be:

            • Add New Device
            • And, at the end, where you have:

        It would be:

      I would guess that you are trying to make an ordered-list of items from the PHP code.
      If this is true, then in the code where you print out the item, you would add the

    • tags in that code,
      not in the HTML code. Doubt that makes sense to you. But, give it a try and post the new code again
      and we will try to get it working for you… Good luck…

    Ok, here is the update on my progress. I am still running into issues, but following the code should be easier for everyone. This is what I am attempting to do. Depending upon what options are selected in my form, a different text file will be written to. My coding can already access what has been chosen, and generate the appropriate file. The conflict is getting the data written. I added the error checking code and the error I’m receiving is “undefined offset” It is in regards to the array I have setup. I want to display the contents of this array, which was working before but does not work now. Well, actually it may work but since nothing is being written to the file, nothing is being output in the array. Any suggestions on what code changes I need to make?

    [code]

    Device Manager
    <h2>DEVICE INVENTORY (Android): MOBILE TEAM</h2>
    <?php		
    
    	error_reporting(E_ALL); //E_ALL ^ E_NOTEICE
    	ini_set('display_errors','1');
    
    	$textfile = 'mtand.txt';
    	$data = ''; //defining variable before use
    
    
    	if (file_exists($textfile) && is_readable($textfile))		
    	{			
    		$data = file($textfile);		
    	}
    
    	for($i = 0; $i < sizeof($data); $i++)		
    	{			
    
    		//separate each element and store in temp array.		
    		$tmp = explode(":", $data[$i]);					
    
    		//assign each element to array. Line 51			
    		$data[] = array('name' => $tmp[0], 'mdn' => $tmp[1], 'carrier' => $tmp[2], 'meid' => $tmp[3], 'version' => $tmp[4], 'date' => $tmp[5], 'notes' => $tmp[6], 'voice' => $tmp[7]);		
    	}		
    	echo "<table cellspacing='0' cellpadding='14'>";		
    	if ($data!=""){			
    		echo "<tr><th style='background:#6699FF'>Device Name</th><th style='background:#6699FF'>MDN</th><th style='background:#6699FF'>Carrier</th><th style='background:#6699FF'>MEID/IMEI</th><th style='background:#6699FF'>OS Version</th><th style='background:#6699FF'>Received Date</th><th style='background:#6699FF'>Notes</th><th style='background:#6699FF'>Voice</th></tr>";			
    		foreach ($data as $key => $row)			
    		{				
    		
    			$name[$key]= $row['name'];		
    			$mdn[$key]= $row['mdn'];		
    			$carrier[$key]= $row['carrier'];		
    			$meid[$key]= $row['meid'];
    			$version[$key]= $row['version'];
    			$date[$key]= $row['date'];
    			$notes[$key]= $row['notes'];
    			$voice[$key]= $row['voice'];				
    			
    			echo "<tr><td>{$name[$key]}</td><td>{$mdn[$key]}</td><td>{$carrier[$key]}</td><td>{$meid[$key]}</td><td>{$version[$key]}</td><td>{$date[$key]}</td><td>{$notes[$key]}</td><td>{$voice[$key]}</td></tr>";			
    		}
    
    	}		
    	echo "</table>";
    
    ?>
    
    			<tr>
    				<td height="10" width="750">
    				<img alt="" width="1" height="10" src="MsSpacer.gif"></td>
    			</tr>
    		</table>
    		</td>
    	</tr>
    
    
    	<tr>
    
    
    		<td valign="top" height="180" width="750">
    		<!-- MSCellType="ContentBody" -->
    
    		<!-- Create id, hide and finalize expandable content -->
    		<ul id="addDevice">
    		<li>Add New Device<ul>
    
    <?php if (isset($_POST['submit'])) { $platform= $_POST['platform']; //defining variable before use $owner= $_POST['owner']; //defining variable before use if ($platform=="Android") { if ($owner=="Mobile Team") { //prepare 'mtand.txt' file for data $fh = fopen("mtand.txt","a+") or die("File Close"); } } if ($platform=="iOS") { if ($owner=="Mobile Team") { //prepare 'mtios.txt' file for data $fh = fopen("mtios.txt","a+") or die("File Close"); } } if ($platform=="MISC") { if ($owner=="Mobile Team") { //prepare 'mtmisc.txt' file for data $fh = fopen("mtmisc.txt","a+") or die("File Close"); } } if ($platform=="Android") { if ($owner=="Biz Dev") { //prepare 'bdand.txt' file for data $fh = fopen("bdand.txt","a+") or die("File Close"); } } if ($platform=="iOS") { if ($owner=="Biz Dev") { //prepare 'bdios.txt' file for data $fh = fopen("bdios.txt","a+") or die("File Close"); } } if ($platform=="MISC") { if ($owner=="Biz Dev") { //prepare 'bdmisc.txt' file for data $fh = fopen("bdmisc.txt","a+") or die("File Close"); } } $name= $_POST['name']; $mdn= $_POST['mdn']; $carrier= $_POST['carrier']; $meid= $_POST['meid']; $version= $_POST['version']; $date= $_POST['date']; $notes= $_POST['notes']; $voice= $_POST['voice']; /* $platform= $_POST['platform'] */ /* $owner= $_POST['owner'] */ //setup implementation rules for notes $notes=stripslashes($notes); //implement validation if (empty($name)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } elseif (empty($mdn)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } elseif (empty($carrier)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } elseif (empty($meid)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } elseif (empty($version)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } elseif (empty($date)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } else { //assemble the data into a line variable $data= $name . ":" . $mdn . ":" . $carrier . ":" . $meid . ":" . $version . ":" . $date . ":" . $notes . ":" . $voice . ":" . "\r\n"; //Figure out what choice was made so that data can be written to correct text file if ($platform=="Android") { if ($owner=="Mobile Team") { /* $output = " *Successfully Added Device To The $owner ($platform) Category!" */ print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //Figure out what choice was made so that data can be written to correct text file if ($platform=="iOS") { if ($owner=="Mobile Team") { print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //Figure out what choice was made so that data can be written to correct text file if ($platform=="MISC") { if ($owner=="Mobile Team") { print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //Figure out what choice was made so that data can be written to correct text file if ($platform=="Android") { if ($owner=="Biz Dev") { print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //Figure out what choice was made so that data can be written to correct text file if ($platform=="iOS") { if ($owner=="Biz Dev") { print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //Figure out what choice was made so that data can be written to correct text file if ($platform=="MISC") { if ($owner=="Biz Dev") { print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //close the file fclose($fh); } } ?>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />	
    <style type="text/css">		
    	body{			
    		font: 10px Verdana, Arial, Helvetica, sans-serif;		
    	}	
    </style>
    
    <?php $output = ''; //defining variable before use. Line 255 if($output!=""){ echo $output; } ?> " method = "post">
    <tr><td><b>Select Device Platform (Android/iOS/MISC):</b></td><td>		
    <select name="platform" style="width:200px;"> 	
    <option>Android</option>	
    <option>iOS</option>
    <option>MISC</option>		
    </select></td></tr>
    
    <tr><td><b>Select Device Owner (Mobile Team/Biz Dev):</b></td><td>		
    <select name="owner" style="width:200px;"> 	
    <option>Mobile Team</option>	
    <option>Biz Dev</option>		
    </select></td></tr>	
    </table>	
    <input type="submit" name="submit" value="Update" />	
    </div>
    
    </tr>
    </table>
        </ul>
    </li>
    
    [/code]
    Enter Device Name:
    Enter MDN:
    Enter Carrier:
    Enter MEID/IMEI:
    Enter OS Version:
    Enter Received Date:
    Enter Notes:
    Is Voice Enabled (Yes/No): Yes No

    Sorry, I was gone for a few days… I looked at your code and fixed a few errors. It still needs some work on the collapse/hide parts and not sure what that part is for, but, here is a new version to test. I did find some minor errors in the file reading section. You had a variable $data that you used as an array and then reloaded it with the data from each line. So, this would never work correctly. I changed that section. There are no errors now, but, the collapse/show section needs further work. Let us know when you get stuck… Good luck!

    [php]

    Device Manager body{ font: 10px Verdana, Arial, Helvetica, sans-serif; }

    DEVICE INVENTORY (Android): MOBILE TEAM

    <?php
      error_reporting(E_ALL); //E_ALL ^ E_NOTEICE
      ini_set('display_errors','1');
    
      $textfile = 'mtand.txt';
      $data = ''; //defining variable before use
    
      if (file_exists($textfile) && is_readable($textfile))      
      {         
         $file = file($textfile);      
    
         foreach ($file as $line)
         {
            //separate each element and store in temp array.      
            $tmp = explode(":", $line);               
    
            //assign each element to array. Line 51         
            $data[] = array('name' => $tmp[0], 'mdn' => $tmp[1], 'carrier' => $tmp[2], 'meid' => $tmp[3], 'version' => $tmp[4], 'date' => $tmp[5], 'notes' => $tmp[6], 'voice' => $tmp[7]);      
         }  
      }
      
      echo "<table cellspacing='0' cellpadding='14'>";      
      if ($data!=''){         
         echo "<tr><th style='background:#6699FF'>Device Name</th><th style='background:#6699FF'>MDN</th><th style='background:#6699FF'>Carrier</th><th style='background:#6699FF'>MEID/IMEI</th><th style='background:#6699FF'>OS Version</th><th style='background:#6699FF'>Received Date</th><th style='background:#6699FF'>Notes</th><th style='background:#6699FF'>Voice</th></tr>";         
         foreach ($data as $key => $row)         
         {   
            $name[$key]= $row['name'];      
            $mdn[$key]= $row['mdn'];      
            $carrier[$key]= $row['carrier'];      
            $meid[$key]= $row['meid'];
            $version[$key]= $row['version'];
            $date[$key]= $row['date'];
            $notes[$key]= $row['notes'];
            $voice[$key]= $row['voice'];            
            
            echo "<tr><td>{$name[$key]}</td><td>{$mdn[$key]}</td><td>{$carrier[$key]}</td><td>{$meid[$key]}</td><td>{$version[$key]}</td><td>{$date[$key]}</td><td>{$notes[$key]}</td><td>{$voice[$key]}</td></tr>";         
         }
    
      }      
      echo "</table>";
    

    ?>

            <tr>
               <td height="10" width="750">
               <img alt="" width="1" height="10" src="MsSpacer.gif"></td>
            </tr>
         </table>
         </td>
      </tr>
    
      <tr>
    
         <td valign="top" height="180" width="750">
         <!-- MSCellType="ContentBody" -->
    
         <!-- Create id, hide and finalize expandable content -->
         <ul id="addDevice"> <li>Add New Device</li> </ul>
    
    <?php if (isset($_POST['submit'])) { $platform= $_POST['platform']; //defining variable before use $owner= $_POST['owner']; //defining variable before use if ($platform=="Android") { if ($owner=="Mobile Team") { //prepare 'mtand.txt' file for data $fh = fopen("mtand.txt","a+") or die("File Close"); } } if ($platform=="iOS") { if ($owner=="Mobile Team") { //prepare 'mtios.txt' file for data $fh = fopen("mtios.txt","a+") or die("File Close"); } } if ($platform=="MISC") { if ($owner=="Mobile Team") { //prepare 'mtmisc.txt' file for data $fh = fopen("mtmisc.txt","a+") or die("File Close"); } } if ($platform=="Android") { if ($owner=="Biz Dev") { //prepare 'bdand.txt' file for data $fh = fopen("bdand.txt","a+") or die("File Close"); } } if ($platform=="iOS") { if ($owner=="Biz Dev") { //prepare 'bdios.txt' file for data $fh = fopen("bdios.txt","a+") or die("File Close"); } } if ($platform=="MISC") { if ($owner=="Biz Dev") { //prepare 'bdmisc.txt' file for data $fh = fopen("bdmisc.txt","a+") or die("File Close"); } } $name= $_POST['name']; $mdn= $_POST['mdn']; $carrier= $_POST['carrier']; $meid= $_POST['meid']; $version= $_POST['version']; $date= $_POST['date']; $notes= $_POST['notes']; $voice= $_POST['voice']; /* $platform= $_POST['platform'] */ /* $owner= $_POST['owner'] */ //setup implementation rules for notes $notes=stripslashes($notes); //implement validation if (empty($name)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } elseif (empty($mdn)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } elseif (empty($carrier)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } elseif (empty($meid)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } elseif (empty($version)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } elseif (empty($date)) { print " *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.
    "; } else { //assemble the data into a line variable $data= $name . ":" . $mdn . ":" . $carrier . ":" . $meid . ":" . $version . ":" . $date . ":" . $notes . ":" . $voice . ":" . "\r\n"; //Figure out what choice was made so that data can be written to correct text file if ($platform=="Android") { if ($owner=="Mobile Team") { /* $output = " *Successfully Added Device To The $owner ($platform) Category!" */ print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //Figure out what choice was made so that data can be written to correct text file if ($platform=="iOS") { if ($owner=="Mobile Team") { print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //Figure out what choice was made so that data can be written to correct text file if ($platform=="MISC") { if ($owner=="Mobile Team") { print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //Figure out what choice was made so that data can be written to correct text file if ($platform=="Android") { if ($owner=="Biz Dev") { print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //Figure out what choice was made so that data can be written to correct text file if ($platform=="iOS") { if ($owner=="Biz Dev") { print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //Figure out what choice was made so that data can be written to correct text file if ($platform=="MISC") { if ($owner=="Biz Dev") { print " *Successfully Added Device To The $owner ($platform) Category!
    "; } } //close the file fclose($fh); } } $output = ''; //defining variable before use. Line 255 if($output!=""){ echo $output; } ?>
    Enter Device Name:
    Enter MDN:
    Enter Carrier:
    Enter MEID/IMEI:
    Enter OS Version:
    Enter Received Date:
    Enter Notes:
    Is Voice Enabled (Yes/No): Yes No
    Select Device Platform (Android/iOS/MISC): Android iOS MISC
    Select Device Owner (Mobile Team/Biz Dev): Mobile Team Biz Dev

    [/php]

    Ok, so I took a look at your changes. The moving of my

      and
    • tags is the reason why the Javascript does not work. And I use the javascript because it looks neat, and Im more familiar with JS. Anyway, lets talk about the changes I made and the changes you have made. Well, before that let me say thank you first. Alright!

      The changes you made still does not write to any files. I was able to fix this on my own, however. Silly me, I did not add a fwrite script. So that is done. Files are being written to. I added a counter, as you will see when I post my code, and I changed the way I display text. No longer am I switching back and forth between html and php just to have one line posing as a categorical title.

      I implemented the way you suggested to arrange/write my array. That works perfect and does get rid of all the errors. But I still have some conflicts, although I am almost done here. Please view the next post.

    Code will be displayed in two parts again. The changes you will find in the first part is a defined variable called “$mtandcount”. I created this variable to count the items listed in any one of the six text files. And then display it in text in the title. This does not work properly. The actual counter and incrementing of the variable takes place in the second half of the code, and if you use a print line the script works perfectly. The problem is that I do not beleive my print statement is being re-processed as the variable is being updated. Perhaps you can help me understand the logic of how php is reading my script.

    [code]

    Device Manager
    <?php		
    	
    	error_reporting(E_ALL); //E_ALL ^ E_NOTICE
    	ini_set('display_errors','1');
    
    	$mtandcount = ''; //defining variable before use. Variable is used to count devices
    
    	if ($mtandcount == null) { //setting initial value to 0, will increment with every added device
    		$mtandcount = 0; }
    
    	print "<font size='6'>DEVICE INVENTORY (Android): MOBILE TEAM ($mtandcount)</font><br>";
    
    	$textfile = 'mtand.txt';
    	$data = ''; //defining variable before use
    	$data1 = ''; //defining variable before use		
    
      if (file_exists($textfile) && is_readable($textfile))      
      {         
         $file = file($textfile);      
    
         foreach ($file as $line)
         {
            //separate each element and store in temp array.      
            $tmp = explode(":", $line);               
    
            //assign each element to array.        
            $data[] = array('name' => $tmp[0], 'mdn' => $tmp[1], 'carrier' => $tmp[2], 'meid' => $tmp[3], 'version' => $tmp[4], 'date' => $tmp[5], 'notes' => $tmp[6], 'voice' => $tmp[7]);      
         }  
    

    }

    	echo "<table cellspacing='0' cellpadding='14'>";		
    	if ($data!=""){			
    		echo "<tr><th style='background:#6699FF'>Device Name</th><th style='background:#6699FF'>MDN</th><th style='background:#6699FF'>Carrier</th><th style='background:#6699FF'>MEID/IMEI</th><th style='background:#6699FF'>OS Version</th><th style='background:#6699FF'>Received Date</th><th style='background:#6699FF'>Notes</th><th style='background:#6699FF'>Voice</th></tr>";			
    		foreach ($data as $key => $row)			
    		{				
    
    			$name[$key]= $row['name'];		
    			$mdn[$key]= $row['mdn'];		
    			$carrier[$key]= $row['carrier'];		
    			$meid[$key]= $row['meid'];
    			$version[$key]= $row['version'];
    			$date[$key]= $row['date'];
    			$notes[$key]= $row['notes'];
    			$voice[$key]= $row['voice'];				
    			
    			echo "<tr><td>{$name[$key]}</td><td>{$mdn[$key]}</td><td>{$carrier[$key]}</td><td>{$meid[$key]}</td><td>{$version[$key]}</td><td>{$date[$key]}</td><td>{$notes[$key]}</td><td>{$voice[$key]}</td></tr>";			
    		}
    	}
    	echo "</table>";
    ?>
    
    			<tr>
    				<td height="10" width="750">
    				<img alt="" width="1" height="10" src="MsSpacer.gif"></td>
    			</tr>
    		</table>
    		</td>
    	</tr>
    
    
    	<tr>
    
    
    		<td valign="top" height="180" width="750">
    		<!-- MSCellType="ContentBody" -->
    
    		<!-- Create id, hide and finalize expandable content -->
    		<ul id="addDevice">
    		<li>Add New Device<ul>[/code]

    In the second half of the code you will notice that I have added fwrite($fh, $data), so now the writing of data works. It does not work properly, and this is where I need help from you. I have to submit my form twice before the first entry of data displays. And this continues. So if I filled out the form twice, I will not see the most recent information I filled out until I fill out and submit the form a third time. That is really the only problem left, aside from the counter. Which if I can get an understanding of the reading logic I can probably figure this conflict out. Should I loop the print statement?

    Second half of code

    [code]<?php

    if (isset($_POST['submit']))	 
    {				
    
    $platform= $_POST['platform']; //defining variable before use
    $owner= $_POST['owner']; //defining variable before use
    
    if ($platform=="Android") {
    	if ($owner=="Mobile Team") {
    //prepare 'mtand.txt' file for data	
    $fh = fopen("mtand.txt","a+") or die("File Close"); }
    }
    
    if ($platform=="iOS") {
    	if ($owner=="Mobile Team") {
    //prepare 'mtios.txt' file for data	
    $fh = fopen("mtios.txt","a+") or die("File Close"); }
    }
    
    if ($platform=="MISC") {
    	if ($owner=="Mobile Team") {
    //prepare 'mtmisc.txt' file for data	
    $fh = fopen("mtmisc.txt","a+") or die("File Close"); }
    }
    
    if ($platform=="Android") {
    	if ($owner=="Biz Dev") {
    //prepare 'bdand.txt' file for data	
    $fh = fopen("bdand.txt","a+") or die("File Close"); }
    }
    
    if ($platform=="iOS") {
    	if ($owner=="Biz Dev") {
    //prepare 'bdios.txt' file for data	
    $fh = fopen("bdios.txt","a+") or die("File Close"); }
    }
    
    if ($platform=="MISC") {
    	if ($owner=="Biz Dev") {
    //prepare 'bdmisc.txt' file for data	
    $fh = fopen("bdmisc.txt","a+") or die("File Close"); }
    }
    
    
    $name= $_POST['name'];		
    $mdn= $_POST['mdn'];		
    $carrier= $_POST['carrier'];		
    $meid= $_POST['meid'];
    $version= $_POST['version'];
    $date= $_POST['date'];
    $notes= $_POST['notes'];
    $voice= $_POST['voice'];
    
    /* $platform= $_POST['platform'] */
    /* $owner= $_POST['owner'] */
    
    //setup implementation rules for notes
    $notes=stripslashes($notes);
    
    //implement validation
    if (empty($name)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    elseif (empty($mdn)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    elseif (empty($carrier)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    elseif (empty($meid)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    elseif (empty($version)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    elseif (empty($date)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    
    else {
    
    
    //assemble the data into a line variable		
    $data= $name . ":" . $mdn . ":" . $carrier . ":" . $meid . ":" . $version . ":" . $date . ":" . $notes . ":" . $voice . ":" . "\r\n";		
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="Android") {	
    	 if ($owner=="Mobile Team") { 		
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    	
    		//write the data to the file		
    		fwrite($fh, $data);
    
    		
    		//counter for devices
    		if ($fh = fopen('mtand.txt', 'r')) { 
    			while (!feof($fh)) {
    				if (fgets($fh)) {
    					$mtandcount++;
    							}
    						}
    					}
    		/* adding a print or echo line here will show that $mtandcount is increasing properly, however, it does not reflect at the top of the script */
    
    				}			
    			}
    
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="iOS") {	
    	 if ($owner=="Mobile Team") { 
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    
    		//write the data to the file		
    		fwrite($fh, $data);
    				}			
    			}
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="MISC") {	
    	 if ($owner=="Mobile Team") { 
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    
    		//write the data to the file		
    		fwrite($fh, $data);
    				}			
    			}
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="Android") {	
    	 if ($owner=="Biz Dev") { 
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    
    		//write the data to the file		
    		fwrite($fh, $data);
    				}			
    			}
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="iOS") {	
    	 if ($owner=="Biz Dev") { 
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    
    		//write the data to the file		
    		fwrite($fh, $data);
    				}			
    			}
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="MISC") {	
    	 if ($owner=="Biz Dev") { 
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    
    		//write the data to the file		
    		fwrite($fh, $data);
    				}			
    			}
    
    //close the file		
    fclose($fh);	     
    
    }
    

    }
    ?>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />	
    <style type="text/css">		
    	body{			
    		font: 10px Verdana, Arial, Helvetica, sans-serif;		
    	}	
    </style>
    
    <?php $output = ''; //defining variable before use. Line 255 if($output!=""){ echo $output; } ?> " method = "post">
    <tr><td><b>Select Device Platform (Android/iOS/MISC):</b></td><td>		
    <select name="platform" style="width:200px;"> 	
    <option>Android</option>	
    <option>iOS</option>
    <option>MISC</option>		
    </select></td></tr>
    
    <tr><td><b>Select Device Owner (Mobile Team/Biz Dev):</b></td><td>		
    <select name="owner" style="width:200px;"> 	
    <option>Mobile Team</option>	
    <option>Biz Dev</option>		
    </select></td></tr>	
    </table>	
    <input type="submit" name="submit" value="Update" />	
    </div>
    
    </tr>
    </table>
        </ul>
    </li>
    
    [/code]
    Enter Device Name:
    Enter MDN:
    Enter Carrier:
    Enter MEID/IMEI:
    Enter OS Version:
    Enter Received Date:
    Enter Notes:
    Is Voice Enabled (Yes/No): Yes No

    Also, thank you in advance. I am very close to having this file completed. This forum has helped a lot in me learning PHP and me completing this project.

    I have rearranged my code, and now my counter and posting with printing works fine. But I still another conflict. Refreshing the page will resubmit the form data, creating duplicates. How do I solve this issue? I’ve been looking into md5, creating an invisible form field and attaching a randomized number into the field, and then creating if statements to make sure that the values match (which they won’t if you refresh). But I do not know how to really implement it. Everyone says it is easy, but it seems a bit difficult. Any suggestions?

    My code so far (Part 1)

    [code]

    Device Manager
    <?php		
    	
    	error_reporting(E_ALL); //E_ALL ^ E_NOTICE
    	ini_set('display_errors','1');
    
    ?>
    
    			<tr>
    				<td height="10" width="750">
    				<img alt="" width="1" height="10" src="MsSpacer.gif"></td>
    			</tr>
    		</table>
    		</td>
    	</tr>
    
    
    	<tr>
    
    
    		<td valign="top" height="180" width="750">
    		<!-- MSCellType="ContentBody" -->
    
    		<!-- Create id, hide and finalize expandable content -->
    		<ul id="addDevice">
    		<li>Add New Device<ul>[/code]

    Code part 2

    [code]<?php

    if (isset($_POST['submit']))	 
    {				
    
    $platform= $_POST['platform']; //defining variable before use
    $owner= $_POST['owner']; //defining variable before use
    
    if ($platform=="Android") {
    	if ($owner=="Mobile Team") {
    //prepare 'mtand.txt' file for data	
    $fh = fopen("mtand.txt","a+") or die("File Close"); }
    }
    
    if ($platform=="iOS") {
    	if ($owner=="Mobile Team") {
    //prepare 'mtios.txt' file for data	
    $fh = fopen("mtios.txt","a+") or die("File Close"); }
    }
    
    if ($platform=="MISC") {
    	if ($owner=="Mobile Team") {
    //prepare 'mtmisc.txt' file for data	
    $fh = fopen("mtmisc.txt","a+") or die("File Close"); }
    }
    
    if ($platform=="Android") {
    	if ($owner=="Biz Dev") {
    //prepare 'bdand.txt' file for data	
    $fh = fopen("bdand.txt","a+") or die("File Close"); }
    }
    
    if ($platform=="iOS") {
    	if ($owner=="Biz Dev") {
    //prepare 'bdios.txt' file for data	
    $fh = fopen("bdios.txt","a+") or die("File Close"); }
    }
    
    if ($platform=="MISC") {
    	if ($owner=="Biz Dev") {
    //prepare 'bdmisc.txt' file for data	
    $fh = fopen("bdmisc.txt","a+") or die("File Close"); }
    }
    
    
    $name= $_POST['name'];		
    $mdn= $_POST['mdn'];		
    $carrier= $_POST['carrier'];		
    $meid= $_POST['meid'];
    $version= $_POST['version'];
    $date= $_POST['date'];
    $notes= $_POST['notes'];
    $voice= $_POST['voice'];
    
    /* $platform= $_POST['platform'] */
    /* $owner= $_POST['owner'] */
    
    //setup implementation rules for notes
    $notes=stripslashes($notes);
    
    //implement validation
    if (empty($name)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    elseif (empty($mdn)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    elseif (empty($carrier)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    elseif (empty($meid)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    elseif (empty($version)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    elseif (empty($date)) {
    print "<font color=#E00000> *Error: Must Fill Out All Fields! Mobile Device Information Not Stored.<br>"; }
    
    else {
    
    
    //assemble the data into a line variable		
    $data= $name . ":" . $mdn . ":" . $carrier . ":" . $meid . ":" . $version . ":" . $date . ":" . $notes . ":" . $voice . ":" . "\r\n";		
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="Android") {	
    	 if ($owner=="Mobile Team") { 		
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    	
    		//write the data to the file		
    		fwrite($fh, $data);
    
    		$mtandcount = ''; //defining variable before use. Variable is used to count devices
    		
    		if ($mtandcount == null) { //setting initial value to 0, will increment with every added device
    			$mtandcount = 0; }
    		
    		//counter for devices
    		if ($fh = fopen('mtand.txt', 'r')) { 
    			while (!feof($fh)) {
    				if (fgets($fh)) {
    					$mtandcount++;
    							}
    						}
    					}
    		/* adding a print or echo line here will show that $mtandcount is increasing properly, however, it does not reflect at the top of the script */
    
    				}			
    			}
    
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="iOS") {	
    	 if ($owner=="Mobile Team") { 
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    
    		//write the data to the file		
    		fwrite($fh, $data);
    				}			
    			}
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="MISC") {	
    	 if ($owner=="Mobile Team") { 
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    
    		//write the data to the file		
    		fwrite($fh, $data);
    				}			
    			}
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="Android") {	
    	 if ($owner=="Biz Dev") { 
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    
    		//write the data to the file		
    		fwrite($fh, $data);
    				}			
    			}
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="iOS") {	
    	 if ($owner=="Biz Dev") { 
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    
    		//write the data to the file		
    		fwrite($fh, $data);
    				}			
    			}
    
    //Figure out what choice was made so that data can be written to correct text file
    if ($platform=="MISC") {	
    	 if ($owner=="Biz Dev") { 
    	print "<font color=#6699FF> *Successfully Added Device To The <font color=#E00000>$owner ($platform)<font color=#6699FF> Category!<br>";
    
    		//write the data to the file		
    		fwrite($fh, $data);
    				}			
    			}
    
    //close the file		
    fclose($fh);	     
    
    }
    

    }
    ?>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />	
    <style type="text/css">		
    	body{			
    		font: 10px Verdana, Arial, Helvetica, sans-serif;		
    	}	
    </style>
    
    <?php $output = ''; //defining variable before use. Line 255 if($output!=""){ echo $output; } ?> " method = "post">
    <tr><td><b>Select Device Platform (Android/iOS/MISC):</b></td><td>		
    <select name="platform" style="width:200px;"> 	
    <option>Android</option>	
    <option>iOS</option>
    <option>MISC</option>		
    </select></td></tr>
    
    <tr><td><b>Select Device Owner (Mobile Team/Biz Dev):</b></td><td>		
    <select name="owner" style="width:200px;"> 	
    <option>Mobile Team</option>	
    <option>Biz Dev</option>		
    </select></td></tr>	
    </table>	
    <input type="submit" name="submit" value="Update" />	
    </div>
    
    </tr>
    </table>
        </ul>
    </li>
    
    <?php		
    
    	if ($mtandcount == null) { //setting initial value to 0, will increment with every added device
    		$mtandcount = 0; }
    
    		$fh = fopen("mtand.txt","a+") or die("File Close"); 
    		
    	//counter for devices
    	if ($fh = fopen('mtand.txt', 'r')) { 
    		while (!feof($fh)) {
    			if (fgets($fh)) {
    						}
    					}
    				}
    	print "<font size='6'>DEVICE INVENTORY (Android): MOBILE TEAM ($mtandcount)</font><br>";
    
    	//close the file		
    	fclose($fh);
    
    	$textfile = 'mtand.txt';
    	$data = ''; //defining variable before use
    	$data1 = ''; //defining variable before use		
    
      if (file_exists($textfile) && is_readable($textfile))      
      {         
         $file = file($textfile);      
    
         foreach ($file as $line)
         {
            //separate each element and store in temp array.      
            $tmp = explode(":", $line);               
    
            //assign each element to array.        
            $data[] = array('name' => $tmp[0], 'mdn' => $tmp[1], 'carrier' => $tmp[2], 'meid' => $tmp[3], 'version' => $tmp[4], 'date' => $tmp[5], 'notes' => $tmp[6], 'voice' => $tmp[7]);      
         }  
    

    }

    	echo "<table cellspacing='0' cellpadding='14'>";		
    	if ($data!=""){			
    		echo "<tr><th style='background:#6699FF'>Device Name</th><th style='background:#6699FF'>MDN</th><th style='background:#6699FF'>Carrier</th><th style='background:#6699FF'>MEID/IMEI</th><th style='background:#6699FF'>OS Version</th><th style='background:#6699FF'>Received Date</th><th style='background:#6699FF'>Notes</th><th style='background:#6699FF'>Voice</th></tr>";			
    		foreach ($data as $key => $row)			
    		{				
    
    			$name[$key]= $row['name'];		
    			$mdn[$key]= $row['mdn'];		
    			$carrier[$key]= $row['carrier'];		
    			$meid[$key]= $row['meid'];
    			$version[$key]= $row['version'];
    			$date[$key]= $row['date'];
    			$notes[$key]= $row['notes'];
    			$voice[$key]= $row['voice'];				
    			
    			echo "<tr><td>{$name[$key]}</td><td>{$mdn[$key]}</td><td>{$carrier[$key]}</td><td>{$meid[$key]}</td><td>{$version[$key]}</td><td>{$date[$key]}</td><td>{$notes[$key]}</td><td>{$voice[$key]}</td></tr>";			
    		}
    	}
    	echo "</table>";
    ?>   
    
    [/code]
    Enter Device Name:
    Enter MDN:
    Enter Carrier:
    Enter MEID/IMEI:
    Enter OS Version:
    Enter Received Date:
    Enter Notes:
    Is Voice Enabled (Yes/No): Yes No

    Ok, I am back! I figured everything out. I wanted to share what the fix was so that others like me will know. If you recall, I was looking for help on how to prevent data from being submitted again after someone refreshed the page. This is for one page, so no redirects or other pages were used. This is also for saving data to a text file.

    First you want to add this before your form.
    [php]if($output!=""){ echo $output; }

    $secret=md5(uniqid(rand(), true)); //generate a random value
    $_SESSION[‘FORM_SECRET’] = $secret; //set the session[/php]

    At the top of the page, as was suggested by some of the people here be sure to start your session. When I say top of the page, I mean literally line 1. (I have to clarify this, because it is rarely clarified anywhere else. I looked! scours google) Start the session by adding the line [php]session_start(); //start a session[/php]. This is all in <?php> tags.

    Now for the final part. Add this before you write data to your file. It goes in that area.
    [php]
    //Retrieve the value of the hidden field
    $form_secret = $_POST[‘form_secret’]; /* isset($_POST[“form_secret”])?$_POST[“form_secret”]:’’; */

    if(isset($_SESSION['FORM_SECRET'])) {
    if(strcasecmp($form_secret, $_SESSION['FORM_SECRET']) === 0) { /* if($_POST['secret'] == $_SESSION['secret']) { */
    

    [/php]

    And unset your session whereever you feel necessary. [php]unset ($_SESSION[“FORM_SECRET”]);[/php]

    Now refresh will not double submit. Took a while, but I got it. And now I am sharing the love.

    Ok, now for round 2! Now that my form can save and display data all on one page, using a text file…I need help with editing bad entries. I did not think about this until now. Now that I have a little handle on things, I tried my hand out with the little knowledge I do have, but it is not working quite right. Where I am having difficulties is that I would like a delete button (I used text though) to appear and remain upon displaying the contents of my textfiles (database). I know that all this will be easier with mySQL, but I want to master this. If anyone can help me, that would be nice. What I have tried so far is this,

    This is the code I came up with…
    [php] $textfile = ‘mtand.txt’;
    if (file_exists($textfile) && is_readable($textfile))
    {

    $delete = @$_GET['delete']; 
    $file_name = "mtand.txt";
    $size = filesize($file_name);
    $textFile = file($file_name);
    $lines = count($textFile);
    
    if($size == "0") 
    { 
    /* Do no action. There is no data written to the file */
    exit; 
    	} 
    }
    

    if($delete != “” && $delete >! $lines || $delete === ‘0’) {
    $textFile[$delete] = “”;
    $fileUpdate = fopen($file_name, “wb”);
    for($a=0; $a<$lines; $a++) {
    fwrite($fileUpdate, $textFile[$a]);
    }
    fclose($fileUpdate);
    header(“Location:indexNew.htm”);

    } else

    foreach($textFile as $key => $val) {

    $line = @$line . $val . "<a href =?delete=$key> [DELETE]
    ";

    }
    echo $line;
    [/php]

    As you can see, I setup a variable for my text file in order to confirm that it exists. I setup some more variables, including one variable that will read whether or not there is actually anything in the text file at all. And then I begin a process that will read my text file, gather the information and then delete. Here is my conflict. (1) I am not sure if I need to repeat this multiple times. (2) data is being deleted, but I actually do not understand how. From my experimentations, I have to link to my page URL + ?delete=x in order for this to work. But while doing this, I cannot reprocess my form afterwards. (3) What am I missing here? I am on the correct track, right? I do not want one instance of delete button (text), I want the opportunity to delete any data that is stored at any time because the button is there always. How do i do this? I will keep tinkering around as usual. But I do need some help. My code is posted in the next sets of posts.

    Sponsor our Newsletter | Privacy Policy | Terms of Service