简体   繁体   中英

PHP page not sending data to .csv

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 (ie no text, only commas).

Does anybody know why this might be?

Any assistance would be appreciated.

Thanks in advance!

Here is the code:

<form action="form_mailer.php" method="post">

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

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

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

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

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

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

}    
?>

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

UPDATE:

Hi,

Many thanks for the responses.

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.

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........ don't suppose anybody knows how I can do this? 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.

Thanks again!

我已经检查了您的代码,它工作正常,并且还将数据存储在csv文件中。

Probably you should make these vars global:

<?php
global $varName, $varOccupation
...

... probably in both <?php blocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM