Jump to content

some can write to file some cannot


kit_Karson

Recommended Posts

Good Morning everyone!

 

I have a problem with the following pages:

 

--------------page to pick teams---------

<html>

<head>

  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">

  <title>Picks</title>

</head>

<body>

<br>

<form method="post" action="Confirm.php"> Name: <input name="name"

type="text"><br>

  <input name="pick[]" value="Oakland" type="checkbox"> Oakland <br>

  <input name="pick[]" value="Kansas City" type="checkbox"> Kansas City <br>

  <br>

  <input name="pick[]" value="Houston" type="checkbox"> Houston <br>

  <input name="pick[]" value="Tennessee" type="checkbox"> Tennessee <br>

  <br>

  <input name="pick[]" value="New England" type="checkbox"> New England <br>

  <input name="pick[]" value="New York Jets" type="checkbox"> New York

Jets <br>

  <br>

  <input name="pick[]" value="Cincinnati" type="checkbox"> Cincinnati <br>

  <input name="pick[]" value="Green Bay" type="checkbox"> Green Bay <br>

  <br>

  <input name="pick[]" value="Minnesota" type="checkbox"> Minnesota <br>

  <input name="pick[]" value="Detroit" type="checkbox"> Detroit <br>

  <br>

  <input name="pick[]" value="New Orleans" type="checkbox"> New Orleans <br>

  <input name="pick[]" value="Philadelphia" type="checkbox">

Philadelphia <br>

  <br>

  <input name="pick[]" value="Carolina" type="checkbox"> Carolina <br>

  <input name="pick[]" value="Atlanta" type="checkbox"> Atlanta <br>

  <br>

  <input name="pick[]" value="St. Louis" type="checkbox"> St. Louis <br>

  <input name="pick[]" value="Washington" type="checkbox"> Washington <br>

  <br>

  <input name="pick[]" value="Arizona" type="checkbox"> Arizona <br>

  <input name="pick[]" value="Jacksonville" type="checkbox">

Jacksonville <br>

  <br>

  <input name="pick[]" value="Seattle" type="checkbox"> Seattle <br>

  <input name="pick[]" value="San Francisco" type="checkbox"> San

Francisco <br>

  <br>

  <input name="pick[]" value="Tampa Bay" type="checkbox"> Tampa Bay <br>

  <input name="pick[]" value="Buffalo" type="checkbox"> Buffalo <br>

  <br>

  <input name="pick[]" value="Cleveland" type="checkbox"> Cleveland <br>

  <input name="pick[]" value="Denver" type="checkbox"> Denver <br>

  <br>

  <input name="pick[]" value="Baltimore" type="checkbox"> Baltimore <br>

  <input name="pick[]" value="San Diego" type="checkbox"> San Diego <br>

  <br>

  <input name="pick[]" value="Pittsburgh" type="checkbox"> Pittsburgh <br>

  <input name="pick[]" value="Chicago" type="checkbox"> Chicago <br>

  <br>

  <input name="pick[]" value="New York Giants" type="checkbox"> New York

Giants <br>

  <input name="pick[]" value="Dallas" type="checkbox"> Dallas <br>

  <br>

  <input name="pick[]" value="Indianapolis" type="checkbox">

Indianapolis <br>

  <input name="pick[]" value="Miami" type="checkbox"> Miami <br>

  <input name="submit" value="Submit Picks!" type="submit"> </form>

</body>

</html>

 

 

--------------page to write them to a file-----------

<html>

<head>

  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">

  <title>Confirmation</title>

</head>

<body>

<?php

$myFile = "Picks.txt";

$fh = fopen($myFile, 'a') or die("can't open file");

$name = $_POST["name"];

fwrite($fh, $name);

fwrite($fh, ",");

for ($i=0; $i<count($_POST['pick']); $i++){

fwrite($fh, $_POST['pick'][$i]);

fwrite($fh, ",");

}

fwrite($fh, "\n");

 

fclose($fh);

?>

Success!

</body>

</html>

 

 

 

 

The problem I am having is friends can run through this site and the teams will be written to the file as I would like. However when I try it on my computer all I get is the name field and the commas (like the array isn't coming through). I have tried with firefox 3.5.3 and IE 8. They are using IE, I am guessing v7.

 

Am I running into a browser issue?

 

I am quite new to php please take it easy on me!!

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/174799-some-can-write-to-file-some-cannot/
Share on other sites

There is nothing technically wrong with the code, it works for me using the latest FF and ie8. Best guess is that when you try it you are not going through the live site that is running that code, but are doing it locally on your development system that has a different copy of confirm.php, one that does not write the actual values to the file or the form you are using has blank/empty value="..." attributes.

 

If you are getting the correct number of commas, then you are getting an array for $_POST['pick'] as that is where the count for the for() loop comes from.

 

P.S. You should actually be using pairs of radio buttons for each pair of teams so that someone can only pick one choice out of each pair.

I have been uploading the files and retrying via the live site. Works from FF on my work computer.... Could it be a cached page on my local comp? I hope it is and I hope it isn't, because I was messing with this for HOURS yesterday! :X

 

Never thought about the commas. it is defiantly getting the array correctly then (that was my first worry).

 

I would like to ensure one one checkbox can be checked, but didn't even know you could do that! I will do some more learning and change that.

 

Thanks Much! I think I am ready to go live with it for the time being and note any other people with the same issue I am having to chase this out a bit more.

What does a "view source" of the form page in the browser(s) where it does not work show?

 

Actually, how are you creating the form? I hope you are using either a database, a text file, or an array that holds the values and dynamically produces the form using php code instead of hand typing all the value=".." and displayed text.

It is showing some old code! WOOT WOOT!

 

I started the page with an html form (Not dynamically created using php) because I thought I could do the whole think in html (didn't know much about php). Since then I have learned tons about php and plan to do exactly as you suggest. 

 

Thanks much for your help and suggestions!

Here is your existing form dynamically done through php -

<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title>Picks</title>
</head>
<body>
<br>
<?php
$teams = array();
$teams[] = "Oakland";
$teams[] = "Kansas City";
$teams[] = "Houston";
$teams[] = "Tennessee";
$teams[] = "New England";
$teams[] = "New York Jets";
$teams[] = "Cincinnati";
$teams[] = "Green Bay";
$teams[] = "Minnesota";
$teams[] = "Detroit";
$teams[] = "New Orleans";
$teams[] = "Philadelphia";
$teams[] = "Carolina";
$teams[] = "Atlanta";
$teams[] = "St. Louis";
$teams[] = "Washington";
$teams[] = "Arizona";
$teams[] = "Jacksonville";
$teams[] = "Seattle";
$teams[] = "San Francisco";
$teams[] = "Tampa Bay";
$teams[] = "Buffalo";
$teams[] = "Cleveland";
$teams[] = "Denver";
$teams[] = "Baltimore";
$teams[] = "San Diego";
$teams[] = "Pittsburgh";
$teams[] = "Chicago";
$teams[] = "New York Giants";
$teams[] = "Dallas";
$teams[] = "Indianapolis";
$teams[] = "Miami";
  
?>
<form method="post" action="confirm.php"> Name: <input name="name"
type="text"><br>
<?php
$counter = 0; // keep track of pairs of teams
foreach($teams as $team){
echo "<input name='pick[]' value='$team' type='checkbox'> $team <br>\n";
$counter++;
if($counter % 2 == 0){
	echo "<br>\n";
}
}
?>
  <input name="submit" value="Submit Picks!" type="submit"> </form>
</body>
</html>

 

And here is the portion of the above code changed to use radio buttons -

<?php
$counter = 0; // keep track of pairs of teams
$game = 0; // keep track of games (for radio buttons)
foreach($teams as $team){
echo "<input name='pick[$game]' value='$team' type='radio'> $team <br>\n";
$counter++;
if($counter % 2 == 0){
	$game++;
	echo "<br>\n";
}
}
?>

Using the array method, you could specify array index values for each team (instead of just leaving them blank), then order the array by the index before you output the form. For each week, you would just over-type the index values to get the choices in the order you want for that week.  You would need to use some 'special' value for the 'bye' teams and then exclude those from the form.

 

You might also consider a comma separated list as that might be easier to get from the various web site(s) that post the schedule. You could then explode the comma separated list to produce the array that the code uses to produce the form.

You might find this code interesting (assuming you are after the final result and not interested in writing a lot of code) -

<?php
// parse weekx.txt, x = 1,2,3,4... files (copy/paste from http://www.nfl.com/schedules)
// find only lines with @ in them, then find the teams before and after the @. Lookup abbreviation to get full name.
$file = 'week2.txt';

$lookup = array();
$lookup['CAR'] = 'Carolina Panthers';
$lookup['ATL'] = 'Atlanta Falcons';
$lookup['NO'] = 'New Orleans Saints';
$lookup['PHI'] = 'Philadelphia Eagles';
$lookup['STL'] = 'St. Louis Rams';
$lookup['WAS'] = 'Washington Redskins';
$lookup['HOU'] = 'Houston Texans';
$lookup['TEN'] = 'Tennessee Titans';
$lookup['NE'] = 'New England Patriots';
$lookup['NYJ'] = 'New York Jets';
$lookup['CIN'] = 'Cincinnati Bengals';
$lookup['GB'] = 'Green Bay Packers';
$lookup['ARI'] = 'Arizona Cardinals';
$lookup['JAC'] = 'Jacksonville Jaguars';
$lookup['OAK'] = 'Oakland Raiders';
$lookup['KC'] = 'Kansas City Chiefs';
$lookup['MIN'] = 'Minnesota Vikings';
$lookup['DET'] = 'Detroit Lions';
$lookup['TB'] = 'Tampa Bay Buccaneers';
$lookup['BUF'] = 'Buffalo Bills';
$lookup['SEA'] = 'Seattle Seahawks';
$lookup['SF'] = 'San Francisco 49ers';
$lookup['PIT'] = 'Pittsburgh Steelers';
$lookup['CHI'] = 'Chicago Bears';
$lookup['BAL'] = 'Baltimore Ravens';
$lookup['SD'] = 'San Diego Chargers';
$lookup['CLE'] = 'Cleveland Browns';
$lookup['DEN'] = 'Denver Broncos';
$lookup['NYG'] = 'New York Giants';
$lookup['DAL'] = 'Dallas Cowboys';
$lookup['IND'] = 'Indianapolis Colts';
$lookup['MIA'] = 'Miami Dolphins';

$teams = array(); // used by form code
$lines = file($file); // read lines from the file into an array
foreach($lines as $line){
// find only lines with @ in them
$pos = strpos($line,'@');
if($pos !== FALSE){
	// get only the portion before the first \t
	$part = explode("\t",$line);
	// get each team (using simple explode)
	list($teamA,$teamB) = explode('@',$part[0]);
	$teamA = trim($teamA);
	$teamB = trim($teamB);
	// echo "{$lookup[$teamA]} at {$lookup[$teamB]}<br />";
	// put teams into array to be used by the form code
	$teams[] = $lookup[$teamA];
	$teams[] = $lookup[$teamB];
}
}
?>
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title>Picks</title>
</head>
<body>
<br>
<form method="post" action="confirm.php"> Name: <input name="name"
type="text"><br>
<?php
$counter = 0; // keep track of pairs of teams
$game = 0; // keep track of games (for radio buttons)
foreach($teams as $team){
echo "<input name='pick[$game]' value='$team' type='radio'> $team <br>\n";
$counter++;
if($counter % 2 == 0){
	$game++;
	echo "<br>\n";
}
}
?>
  <input name="submit" value="Submit Picks!" type="submit"> </form>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.