Hi, I have a problem with my code. When I fill the PHP form and submit on the page, I encounter with 2 problems;

1- It writes all 6 input coming from 6 textareas to next line in csv file as I want. But it writes all of them into the 1st cell of row I want it to write to 6 different cells of course.
2- After submitting the form, if you press backspace the form is opened again with the previous input in textareas, I want them to be empty. I'd appreciate if you help. Thanks.

  1. Show your code.
  2. This is cached by your browser. You can try this but it may not always work.

my code is:

<?php
error_reporting(E_ALL ^ E_NOTICE); 

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

    if(empty($_POST['formCase_No']))
    {
        $errorMessage .= "<li>You forgot to enter a Case Number!</li>";
    }
    if(empty($_POST['formCase_Description']))
    {
        $errorMessage .= "<li>You forgot to enter a Case Description!</li>";
    }
    if(empty($_POST['formCase_Info']))
    {
        $errorMessage .= "<li>You forgot to enter a Case Information!</li>";
    }
    if(empty($_POST['formCase_Customer']))
    {
        $errorMessage .= "<li>You forgot to enter a Case Customer!</li>";
    }
    if(empty($_POST['formCase_Date']))
    {
        $errorMessage .= "<li>You forgot to enter a Case Date!</li>";
    }
    if(empty($_POST['formCase_Note']))
    {
        $errorMessage .= "<li>You forgot to enter a Case Note!</li>";
    }

    $varCase_No = $_POST['formCase_No'];
    $varCase_Description = $_POST['formCase_Description'];
    $varCase_Info = $_POST['formCase_Info'];
    $varCase_Customer = $_POST['formCase_Customer'];
    $varCase_Date = $_POST['formCase_Date'];
    $varCase_Note = $_POST['formCase_Note'];

    if(empty($errorMessage)) 
    {
        $fs = fopen("filename.csv","a");
        fwrite($fs,$varCase_No . ", " . $varCase_Description . ", " . $varCase_Info . ", " . $varCase_Customer . ", " . $varCase_Date . ", " . $varCase_Note . "\n");
        fclose($fs);

        header("Location: thankyou.html");
        exit;
    }
}
?>

fopen

Hiii ,May you like to know about fopen here Click Here,read about it,i think you can use "w" or "x" instead of "a".
but instead of all that , if you use header function is better than file system functions:

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-type: application/csv");
echo $varCase_No . ", " . $varCase_Description . ", " . $varCase_Info . ", " . $varCase_Customer . ", " . $varCase_Date . ", " . $varCase_Note . "\n";
?>

.
.
.
%%^Murtada^%%

Do you mean they all go in one cell in Excel when importing? If so, it's most likely the end-of-line. Try to output with \r\n instead of just \n.

pritaeas:

Do you mean they all go in one cell in Excel when importing? If so, it's most likely the end-of-line. Try to output with \r\n instead of just \n.

That's Okey, I am agree with you.

ok, it is solved. thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.