0

I have a problem with my php code, I am a beginner and i need some help. I tried to make a function that is going to write in a file the input text from 3 variables using write, but it seem that when I am putting everything into a function it is not working.

Here is my code:

<!doctype html>
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<form action="test.php" method="post">
<input type="text" name="text">
<input type="text" name="text1">
<input type="text" name="text2">
<input type="submit" name="submit" value="write">
</form>
<?php



$text = $_POST['text'];
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];
function start($text,$text1,$text2)
{
    if(isset($_POST['submit']))
    {
        $text_total = "$text $text1 $text2 \r\n";
        $file = fopen("text.txt", "a+");
        fwrite($file, $text_total);
        fclose($file);
    }
}

start();

?>
<body>
</body>
</html>
2
  • 2
    You aren't passing your string parameters. Also, whenever something isn't working; error_reporting. Commented Dec 10, 2013 at 15:28
  • start($text,$text1,$text2); to call that function. function having three argument. Commented Dec 10, 2013 at 15:29

3 Answers 3

3

passing your string parameters to the function call

 <?php


    $text = $_POST['text'];
    $text1 = $_POST['text1'];
    $text2 = $_POST['text2'];
    function start($text,$text1,$text2)
    {
        if(isset($_POST['submit']))
        {
            $text_total = "$text $text1 $text2 \r\n";
            $file = fopen("text.txt", "a+");
            fwrite($file, $text_total);
            fclose($file);
        }
    }

    start($text,$text1,$text2);

    ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Your function argument is missing is your problem. Also check the form is submitted. It's for better practice.

<?php

function start($text,$text1,$text2)
{
  if(isset($_POST['submit']))
  {
    $text_total = "$text $text1 $text2 \r\n";
    $file = fopen("text.txt", "a+");
    fwrite($file, $text_total);
    fclose($file);
 }
}

if(isset($_POST['submit'])){

  unset($_POST['submit']); //This is to make sure it's executed one time only

  $text = $_POST['text'];
  $text1 = $_POST['text1'];
  $text2 = $_POST['text2'];
  start($text,$text1,$text2);
}


?>

Comments

-1

You have to add this on the test.php file:

<?php



$text = $_POST['text'];
$text1 = $_POST['text1'];
$text2 = $_POST['text2'];
function start($text,$text1,$text2)
{
    if(isset($_POST['submit']))
    {
        $text_total = "$text $text1 $text2 \r\n";
        $file = fopen("text.txt", "a+");
        fwrite($file, $text_total);
        fclose($file);
    }
}

start();

You cant have it together with the html

3 Comments

You can have PHP and HTML together as long as the file is saved as a .php file extension
@VincentWilkie Not if you want to display the results. Unless you use AJAX, but thats another story
You can detect a submission using $_POST, you would just have to refresh the page. No need for AJAX.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.