phpcode Posted February 11, 2008 Share Posted February 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90488-post-help/ Share on other sites More sharing options...
Lamez Posted February 11, 2008 Share Posted February 11, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/90488-post-help/#findComment-463931 Share on other sites More sharing options...
phpcode Posted February 11, 2008 Author Share Posted February 11, 2008 The forms fields name are random. Quote Link to comment https://forums.phpfreaks.com/topic/90488-post-help/#findComment-463934 Share on other sites More sharing options...
Lamez Posted February 11, 2008 Share Posted February 11, 2008 Oh, well that is crazy. I cannot help you there. Just because I am curious what are you doing this for? Quote Link to comment https://forums.phpfreaks.com/topic/90488-post-help/#findComment-463936 Share on other sites More sharing options...
phpcode Posted February 11, 2008 Author Share Posted February 11, 2008 A contact form for my site Quote Link to comment https://forums.phpfreaks.com/topic/90488-post-help/#findComment-463938 Share on other sites More sharing options...
play_ Posted February 11, 2008 Share Posted February 11, 2008 You're not even running a loop there <?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. Quote Link to comment https://forums.phpfreaks.com/topic/90488-post-help/#findComment-463940 Share on other sites More sharing options...
aebstract Posted February 11, 2008 Share Posted February 11, 2008 Why do you have random field names? Just curious about this, since it's just a contact form and all. Quote Link to comment https://forums.phpfreaks.com/topic/90488-post-help/#findComment-463942 Share on other sites More sharing options...
Lamez Posted February 11, 2008 Share Posted February 11, 2008 ya me too? Quote Link to comment https://forums.phpfreaks.com/topic/90488-post-help/#findComment-464307 Share on other sites More sharing options...
resago Posted February 12, 2008 Share Posted February 12, 2008 you left out $key in your write to file. Quote Link to comment https://forums.phpfreaks.com/topic/90488-post-help/#findComment-464461 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.