Jump to content

Recommended Posts

I'm trying to create a contact form but I'm stuck on this:

 

<?php

$file = fopen("contact.txt", "a+");

foreach($_POST as $key => $value); 

fwrite($file, $value. "\n\n===========================\n\n");

fclose($file);


?>

 

What I want it to do is get all the forms field names and what the user entered into them and write it to the contact file but it only shows me the last field name. I've tried putting the foreach() in fwrite() but that just gives me an error.

Link to comment
https://forums.phpfreaks.com/topic/90488-post-help/
Share on other sites

when you set the name that will be your post variable

 

here is an example:

 

<form id="form1" name="form1" method="post" action="">

  <input name="name" type="text" id="name" />

  <input type="submit" name="Submit" value="Submit" />

</form>

<?php
$name = $_POST['name'];
echo $name;
?>

Instead of writing it to a file, you should do a database!

Link to comment
https://forums.phpfreaks.com/topic/90488-post-help/#findComment-463931
Share on other sites

You're not even running a loop there :P

 

<?php

$file = fopen("contact.txt", "a+");

foreach($_POST as $key => $value) {

fwrite($file, $value. "\n\n===========================\n\n");
}

fclose($file);


?>

 

This is an odd way of doing contact forms.

Keep in mind that the button is also a $_POST. so that's gonna show up on your file.

Link to comment
https://forums.phpfreaks.com/topic/90488-post-help/#findComment-463940
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.