Apr 21st 2013, 9:45:36
I have a html form atm that is able to write and append data into a csv file that already exists using PHP, but I want to add code in so that it checks to see if the csv file already exists, and if it doesn't, to create it and make it writeable, and insert the relevant column headings.
this is the code atm:
<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";
if(empty($_POST['formMovie']))
{
$errorMessage .= "<li>You forgot to enter a movie!</li>";
}
if(empty($_POST['formName']))
{
$errorMessage .= "<li>You forgot to enter a name!</li>";
}
$varMovie = $_POST['formMovie'];
$varName = $_POST['formName'];
if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,$varName . ", " . $varMovie . "\n");
fclose($fs);
header("Location: thank-you.html");
exit;
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php
if(!empty($errorMessage))
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
?>
<form action="myform1.php" method="post">
<p>
What is your favorite movie?<br>
<input type="text" name="formMovie" maxlength="50" value="<?=$varMovie;?>" />
</p>
<p>
What is your name?<br>
<input type="text" name="formName" maxlength="50" value="<?=$varName;?>" />
</p>
<input type="submit" name="formSubmit" value="Submit" />
</form>
</body>
</html>
this is the code atm:
<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";
if(empty($_POST['formMovie']))
{
$errorMessage .= "<li>You forgot to enter a movie!</li>";
}
if(empty($_POST['formName']))
{
$errorMessage .= "<li>You forgot to enter a name!</li>";
}
$varMovie = $_POST['formMovie'];
$varName = $_POST['formName'];
if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,$varName . ", " . $varMovie . "\n");
fclose($fs);
header("Location: thank-you.html");
exit;
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php
if(!empty($errorMessage))
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
?>
<form action="myform1.php" method="post">
<p>
What is your favorite movie?<br>
<input type="text" name="formMovie" maxlength="50" value="<?=$varMovie;?>" />
</p>
<p>
What is your name?<br>
<input type="text" name="formName" maxlength="50" value="<?=$varName;?>" />
</p>
<input type="submit" name="formSubmit" value="Submit" />
</form>
</body>
</html>
Pain is weakness leaving the body.
MSN:
MSN: