0

I did the simple form.In that i am trying modifying the $_post output.

<?php

    $lhs = array();
    $rhs = array();

    foreach($_POST as $key => $value) {
        echo $key . "=" . $value;
        echo "<br>";
        $lhs[] = $key; //first array for left hand side 
        $rhs[] = $value; //second array for right hand side

    }

?>

<form name="form1" method="post" action="">
    Name: <input type="text" name="name"><br>
    Phone No: <input type="text" name="phone" /><br/>
    Course:<input type="text" name="course" /> <br />
    <input type="submit" name="Submit" value="Sign Up">
</form>

So the output of this could be:

name=xyz
phone=123455453
course=be
Submit=Sign Up

now you can observe see that I am getting the output of the button also:

Submit=Sign Up

Which I don't require in the output. And I require the output something like this:

name=xyz
phone=123455453
course=be

This output I want to store in a text file.

I am not able to modify the output either I am not able to store this output in a text file.

How can I get rid of the submit button output and save the data into a file?

Any Help or advice is appreciated and Thanks in advance.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Awanti
  • 109
  • 10

4 Answers4

1

This should work for you:

Here I first added a if statement to check if your submit button was pressed with isset() and all fields aren't empty().

In the foreach loop I simply added an if statement to check if the current key is the submit button and if yes I just skip the iteration with continue.

To now save the data into a file I used file_put_contents() where I then go through both arrays with array_map() and return the key/value pair as combined elements with a new line character (PHP_EOL) at the end of each element.

<?php

    if(isset($_POST["Submit"]) && !empty($_POST["name"]) && !empty($_POST["phone"]) && !empty($_POST["course"])) {

        foreach($_POST as $key => $value) {
            if($key == "Submit") continue;
            echo $lhs[] = $key; //first array for left hand side 
            echo $rhs[] = $value; //second array for right hand side
        }

        file_put_contents("file.txt", implode(PHP_EOL, array_map(function($v1, $v2){
            return "$v1:$v2";
        }, $lhs, $rhs)), FILE_APPEND);

    }

?>

<form name="form1" method="post" action="">
    Name: <input type="text" name="name"><br>
    Phone No: <input type="text" name="phone" /><br/>
    Course:<input type="text" name="course" /> <br />
    <input type="submit" name="Submit" value="Sign Up">
</form>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

You can access each variable by input name, as follows:

<?php
  echo "Name = " . $_POST["name"];
  echo "Phone = " . $_POST["phone"];
  echo "Course = " . $_POST["course"];
?>

<form name="form1" method="post" action="">
    Name: <input type="text" name="name"><br>
    Phone No: <input type="text" name="phone" /><b>
    Course:<input type="text" name="course" /> <br>
    <input type="submit" name="Submit" value="Sign Up">
</form>
Caio Ladislau
  • 1,257
  • 13
  • 25
0

You can either change your html to be:

<input type="submit" value="Sign Up">

Removing the name attribute, will prevent it from showing up in $_POST

Or you can just unset the value:

unset($_POST['Submit']);
foreach($_POST as $key => $value){...

Which will unset it from the $_POST array

cwurtz
  • 3,177
  • 1
  • 15
  • 15
0

Try with -

$file = fopen("test.txt", "w");
$file_contents = '';
foreach($_POST as $key => $value)
    $file_contents .= $key ."=". $value.'\n' ;

fwrite($file, $file_contents);
fclose($file);
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87