PHP page not sending data to .csv
<p>I have created a PHP page (see code below) that is supposed to validate the information entered into the webform prior to submitting it, to ensure that all fields have been completed.  If any fields have been left blank, it is supposed to display an error message; if all fields have been filled in, it is supposed to send the data to a .csv file.  The problem is that it is not validating the fields, nor is it sending the data to the .csv.  I have tried deleting the .csv and then submitting the webform; it creates a .csv with commas separating the entries, but there are no entries (i.e. no text, only commas).</p>

<p>Does anybody know why this might be?</p>

<p>Any assistance would be appreciated.</p>

<p>Thanks in advance!</p>

<p>Here is the code:</p>

<pre><code>&lt;form action="form_mailer.php" method="post"&gt;

&lt;table&gt;
&lt;tr&gt;&lt;td&gt;Which is your name?&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="formName" maxlength="50" value="&lt;?=$varName;?&gt;"&gt;&lt;/td&gt;&lt;/tr&gt;     

&lt;tr&gt;&lt;td&gt;What is your occupation?&lt;/td&gt;              
&lt;td&gt;&lt;input type="text" name="formOccupation" maxlength="50" value="&lt;?=$varOccupation;?&gt;"&gt;&lt;/td&gt;&lt;/tr&gt;  
&lt;/table&gt;
&lt;input type="submit" name="formSubmit" value="Submit"&gt;
&lt;/form&gt; 

&lt;?php
if($_POST['formSubmit'] == "Submit") 
{  
$errorMessage = "";   

if(empty($_POST['formName']))   
{    
$errorMessage .= "&lt;li&gt;You forgot to enter your name!&lt;/li&gt;";  
}  
if(empty($_POST['formOccupation']))   
{    
$errorMessage .= "&lt;li&gt;You forgot to enter your occupation!&lt;/li&gt;";  
}   

$varName = $_POST['formName'];  
$varOccupation = $_POST['formOccupation'];   

if(!empty($errorMessage))   
{    
echo("&lt;p&gt;There was an error with your form:&lt;/p&gt;\n");    
echo("&lt;ul&gt;" . $errorMessage . "&lt;/ul&gt;\n");  
}  

}    
?&gt;

&lt;?php
if($errorMessage != "") 
{  
echo("&lt;p&gt;There was an error:&lt;/p&gt;\n");  
echo("&lt;ul&gt;" . $errorMessage . "&lt;/ul&gt;\n");
} 
else
{  
$fs = fopen("data.csv","a");  
fwrite($fs,$varName . ", " . $varOccupation . "\n");  
fclose($fs);   
}
?&gt;
</code></pre>

<p><strong>UPDATE:</strong></p>

<p>Hi,</p>

<p>Many thanks for the responses.</p>

<p>I've realised where I have gone wrong now........I had the form action as 'form_mailer.php', which is not the form page itself but another page that performs further actions with the data provided on the webform.</p>

<p>I have now brought the 'form_mailer' coding into the webform .php and have changed the 'action' of the webform page so that it effectively submits to itself, but now I need to redirect to the form_mailer page following successful submission of the webform or display a thank you message........<strong>don't suppose anybody knows how I can do this?</strong>  If you could provide instructions for how to do both that would be great as I can then choose the one I prefer that would be best for my needs.</p>

<p>Thanks again!</p>
