www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25

Thread: PHP help needed!

  1. #16
    Join Date
    May 2012
    Posts
    42
    At quick glance it looks like all your code to write to the file comes after you read the file. If you write the information submitted from the form BEFORE you read the file and count the lines, you should be good.
    Keith

    Freelance PHP Web Developer
    http://www.duvalltech.com/

  2. #17
    Join Date
    May 2012
    Posts
    19
    @kbduvall, what you say is correct. Rearranging my code does solve that conflict, however, I am trying to learn how to work with the formatting I have currently. Or if there is no fix to this, I need to know that as well and I will rearrange my code.

  3. #18
    Join Date
    May 2012
    Posts
    19
    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?

  4. #19
    Join Date
    May 2012
    Posts
    19
    My code so far (Part 1)

    Code:
    <html>
    
    <head>
    <meta http-equiv="Content-Language" content="en-us">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Device Manager</title>
    
    <!-- Call JQuery -->
    <script src="http://www.acmeous.com/tutorials/demo/acmeousCollapsibleLists/acmeousCollapsibleLists.js"
    type="text/javascript"></script>
    
    
    <!-- Setup Event Listener -->
    <script type="text/javascript"><!--
    if (window.addEventListener) {
    window.addEventListener("load", function(){makeCollapsible(document.getElementById('addDevice'), 0);}, false);
    } else if (window.attachEvent) {
    window.attachEvent("onload", function(){makeCollapsible(document.getElementById('addDevice'), 1);});
    } else {
    window.onload = function(){makeCollapsible(document.getElementById('addDevice'), 1);};
    } 
    -->
    </script>
    
    </head>
    
    <body>
    
    	<?php		
    		
    		error_reporting(E_ALL); //E_ALL ^ E_NOTICE
    		ini_set('display_errors','1');
    
    	?>
    
    
    <div align="center">
    	<table border="0" cellpadding="0" cellspacing="0" width="750" height="500">
    		<!-- MSTableType="layout" -->
    		<tr>
    			<td valign="top" height="320">
    			<!-- MSCellType="ContentBody" -->
    			<table cellpadding="0" cellspacing="0" width="750" height="320">
    				<!-- MSCellFormattingTableID="1" -->
    				<tr>
    					<td height="310">
    					<table cellpadding="0" cellspacing="0" border="0" width="100&#37;" height="100%">
    						<tr>
    							<td valign="top" height="100%" width="100%">
    							<!-- MSCellFormattingType="content" -->
    							</td>
    						</tr>
    					</table>
    					</td>
    				</tr>
    				
    				<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>

  5. #20
    Join Date
    May 2012
    Posts
    19
    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; } ?>
    <form action = "<? echo $_SERVER['PHP_SELF']?>" method = "post">	
    	<div style="border:2px solid #cccccc;margin:8px;padding:8px;width:600px;">	
    	<table width="80&#37;">	
    	<tr><td><b>Enter Device Name: </b></td><td><input name="name" type="text" style="width:200px;" /></td></tr>
    	<tr><td><b>Enter MDN: </b></td><td><input name="mdn" type="text" style="width:200px;" /></td></tr>
    	<tr><td><b>Enter Carrier: </b></td><td><input name="carrier" type="text" style="width:200px;" /></td></tr>
    	<tr><td><b>Enter MEID/IMEI: </b></td><td><input name="meid" type="text" style="width:200px;" /></td></tr>
    	<tr><td><b>Enter OS Version: </b></td><td><input name="version" type="text" style="width:200px;" /></td></tr>
    	<tr><td><b>Enter Received Date: </b></td><td><input name="date" type="text" style="width:200px;" /></td></tr>
    	<tr><td><b>Enter Notes: </b></td><td><input name="notes" type="text" style="width:200px;" /></td></tr>	
    	<tr><td><b>Is Voice Enabled (Yes/No):</b></td><td>		
    	<select name="voice" style="width:200px;"> 	
    	<option>Yes</option>	
    	<option>No</option>		
    	</select></td></tr>
    
    	<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>
    </form>	
    
    	</tr>
    	</table>
            </ul>
        </li>
    </ul>
    </div>
    
    	<?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>";
    	?>   
    
    </body>
    
    </html>

  6. #21
    Join Date
    May 2012
    Posts
    19
    Instead of posting the code in two sections I will just attach the file. I have tried to setup some code to prevent duplicate entries of my php form data when someone uses refresh and I need some help. I am getting some errors and an if statement that should be "TRUE" is returning "FALSE" and preventing data to be written to my text file. I do not know why this is happening. Some help would be greatly appreciated.

    I believe the conflct is with this set of code
    PHP Code:
    if(isset($_SESSION["FORM_SECRET"])) {
        if(
    strcasecmp($form_secret$_SESSION["FORM_SECRET"]) == 0) { /* if var1 ($form_secret) is equal to var 2 ($_SESSION["FORM_SECRET") then proceed */ 
    Let me retrace all of my steps.

    First, I setup a session. Again, I want to prevent resubmittion of data after the refresh button is pressed on the browser. I setup a session, adn then I created a unique and randon value using md5 and the uniqid. I then setup my variables to be equal to this randon value.

    PHP Code:
    session_start(); //start a session
    $secret=md5(uniqid(rand(), true)); //generate a random value
    $_SESSION['FORM_SECRET'] = $secret//set random value to session 
    Second, I created a hidden field in my form. I wasn't sure about this, but from what I have been reading in various articles, I needed to echo my randon value onto my form, which would then be stored into a variable. So I did that.

    Code:
    	//echo the contents of hiddle field, FORM_SECRET, into the variable $_SESSION 
    	<input type="hidden" name="form_secret" id="form_secret" value="<?php echo $_SESSION['FORM_SECRET'];?>" />
    Third, I retrieved the value in the hidden field, and then setup a direct comparison of the hidden field data and the variable I created earlier with the unique number. I wanted to say, "if these are true then proceed with writing data into my text file". I believe this is where I messed up. This value is returning false, and is therefore running my else clause which is an error message return saying that the values do not match. I want this to occur after someone uses refresh, not after someone uses the submit button. Anyway, here is that code.

    PHP Code:
    //Retrieve the value of the hidden field
    $form_secret = isset($_POST["form_secret"])?$_POST["form_secret"]:'';

    if(isset(
    $_SESSION["FORM_SECRET"])) {
        if(
    strcasecmp($form_secret$_SESSION["FORM_SECRET"]) == 0) { /* if var1 ($form_secret) is equal to var 2 ($_SESSION["FORM_SECRET") then proceed */ 
    Anyone with ideas as to what I am doing wrong or missing?

  7. #22
    Join Date
    May 2012
    Posts
    42
    If all you're trying to do is prevent another add if the user presses the refresh button, then you don't really need to add anything to the form. You can do it all with the session.

    On line one:
    PHP Code:
    session_start(); 
    Where you begin to process the form (from the user clicking the submit button):
    PHP Code:
    if (isset($_POST['submit']) && $_SESSION['alreadySubmitted'] != 1)     
    {
        
    $_SESSION['alreadySubmitted'] = 1;
        
    // rest of code 
    The $_SESSION array persists across page loads as long as you call session_start() on each page you want to use it with. So if you set $_SESSION['alreadySubmitted'] and the user refreshes, or even visits another page that calls session_start(), $_SESSION['alreadySubmitted'] will still have a value of 1.

    Although I would say that for what you're doing, you may want to consider using a database instead of flat files. It would be faster, easier to insert/update/read data, and easier to prevent duplicate entries.
    Keith

    Freelance PHP Web Developer
    http://www.duvalltech.com/

  8. #23
    Join Date
    May 2012
    Posts
    19
    Thank you for the response, but your fix presumes multiple pages. This is only 1 page, and the end user never leaves the one page. Infinite submittion of the form from one page is the intent of the script. Sorry, but your script is not applicable. ANd the file is meant to be sandboxed, so no database was used.

  9. #24
    Join Date
    May 2012
    Posts
    59
    Hello,

    Easiest way to stop the form sending again when pressing F5 (not when clicking back in the browser) is to just do a HTTP header redirect to itself with any confirmation message (if you want one) passed in the URL via GET eg:

    Code:
    header("Location: index.php?msg=hello");die();
    A fail safe way, is to store an MD5 of the whole form in the $_SESSION and then re hash the whole form again when it's sent to see if they match and if so... ignore it if anything changes in the form the hashes wont match and it will be classed as a new submission.

    Kind regards,
    Scott

  10. #25
    Join Date
    May 2012
    Posts
    42
    Sessions work equally as well on one page. It doesn't have to be multiple pages at all.
    Keith

    Freelance PHP Web Developer
    http://www.duvalltech.com/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles