Form Emailing in HTML and PHP not working - php

i have created a form on my website that lets others contact me. I used a PHP script to send the email but i always get the error:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
I used this code:
<form method="post" action="contact.php" enctype="text/plain">
Name*:<br>
<input type="text" name="name" placeholder='Steve'><br>
E-mail*:<br>
<input type="text" name="mail" placeholder='john#example.com'><br>
Comment*:<br>
<textarea name="comments" maxlength="400" cols="25" rows="6">
This site is awesome!
</textarea>
<br> <br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>
In html and this:
<?php
if($_POST["message"]) {
mail("myemail#example.com", "MCPEmaps Comment", $_POST["message"], "From: an#email.address");
}
?>
In the PHP file.
Any help?

You are POSTing comments, not message.
Change this:
<textarea name="comments" maxlength="400" cols="25" rows="6">
This site is awesome!
</textarea>
to this:
<textarea name="message" maxlength="400" cols="25" rows="6">
This site is awesome!
</textarea>

Change your html with this, As you are using $_POST["message"] but you are not passing it in your html.. So change your this line
<input type="submit" name="message" value="Send">
complete code below:
<form method="post" action="contact.php" enctype="text/plain">
Name*:<br>
<input type="text" name="name" placeholder='Steve'><br>
E-mail*:<br>
<input type="text" name="mail" placeholder='john#example.com'><br>
Comment*:<br>
<textarea name="comments" maxlength="400" cols="25" rows="6">
This site is awesome!
</textarea>
<br> <br>
<input type="submit" name="message" value="Send">
<input type="reset" value="Reset">
</form>

The PHP will not send the mail because there is no input : message
You can use :
<?php
if(isset($_POST["message"])) {
mail("myemail#example.com", "MCPEmaps Comment", $_POST["message"], "From: an#email.address");
}
?>
And
<input type="submit" name="message" />

Try this code in your contact.php file:
<?php
if($_POST["comments"]) {
mail("myemail#example.com", "MCPEmaps Comment", $_POST["message"], "From: an#email.address");
}
?>
If in case you are using in localhost you need to work lot in your php.ini file. otherwise you get result.

Related

Pressing Enter on a <input> submits the wrong <form> (2 forms on 1 page)

I have got two different forms on the same page.
<form action="" method="post" onsubmit="ajax_send('<?print $userid;?>',this.msg.value); return false;">
<input id="msg" autocomplete="off" type="text" name="msg" stlye="width: 80px;" autofocus="autofocus" placeholder="Chat here" />
<input type="submit" />
</form>
and
<form action="?page=about&id=0#comments" method="post">
<textarea class="editbox" id="exitbox" placeholder="Write a comment ..." name="newcomment"></textarea>
<input type="submit" id="submit1" name="submit1"></div></div>
Now when I enter something in <input id="msg"..> and press enter, it submits the second form instead of the first one.
What am I missing?
Your second form isn't closed.
Your second form should by like that
<form action="?page=about&id=0#comments" method="post">
<textarea class="editbox" id="exitbox" placeholder="Write a comment ..." name="newcomment"></textarea>
<input type="submit" id="submit1" name="submit1"></div></div>
</form>

trying to post to php file - server error

I'm a newbie here to php and Apache.
I have an html form:
<html>
<body>
<?php
<form method="post" action="contact.php"> Email: <input name="email"
type="text"/><br/> Message:<br/> <textarea name="message" rows="15" cols="40">
</textarea><br/> <input type="submit"/> </form>
</body>
</html>
I have contact.php located in what I believe is the right place, but when I submit the query, I get "The HTTP verb POST used to access path '/www/contact.php' is not allowed."
When I access contact.php as a url I get strange results (like a repeating File Download message box asking whether I want to Save or Open)
if you are using which is missing in your code.
for your below code will work.
<html>
<body>
<form method="post" action="contact.php"> Email: <input name="email"
type="text"/><br/> Message:<br/> <textarea name="message" rows="15" cols="40">
</textarea><br/> <input type="submit"/> </form>
</body>
</html>
First Thing Give Name to Submit Button and second remove
<html>
<body>
<form method="post" action="contact.php"> Email: <input name="email"
type="text"/><br/> Message:<br/> <textarea name="message" rows="15" cols="40">
</textarea><br/> <input type="submit" name="submit" /> </form>
</body>
</html>
and if you can still face an error then try to give full file path in action

php form not passing variable through?

Hey guys I am not able to pass the variable "profile" through... This is done with smarty templates btw
{if $commentpossible == true}
<form name="form1" method="get" action="comment.php?profile=5">
<p>
<label for="comment"></label>
<textarea name="comment" id="comment" cols="45" rows="5" ></textarea>
<br>
<input type="submit" name="Submit" id="Submit" value="Submit">
</p>
</form>
{/if}
Basically this page is profile?profile=5 and I want to pass that profile through...I have "5 manaully inputed atm rather than a variable just too see if it would work...it still does not....when submit is hit it just goes to comment.php?comment=&Submit=Submit....(comment is blank intentionally there)...I need to be more comment.php?profile=5comment=blablabla etc etc
Any idea on what could be the problem?
Add profile as a hidden field when you're using the GET method:
{if $commentpossible}
<form name="form1" method="get" action="comment.php">
<input type="hidden" name="profile" value="5">
<p>
<label for="comment"></label>
<textarea name="comment" id="comment" cols="45" rows="5" ></textarea>
<br>
<input type="submit" name="Submit" id="Submit" value="Submit">
</p>
</form>
{/if}

Save Form to JSON

I have a form and need to submit it and save the data as a JSON file.
Is it possible?
If so how please?
Here is my simple form below.
<form action="#" method="">
<div data-role="fieldcontain">
<label for="date">Date Input:</label>
<input type="date" name="date" id="date" value="" /><br /><br />
<label for="textarea">Event:</label>
<textarea cols="40" rows="8" name="textarea" id="textarea"></textarea>
<input type="submit" value="save to json" />
</div>
</form>
<?php
$file = dirname(__FILE__).'/form-data-'.time().'-'.rand(1000,9999);
file_put_contents($file, json_encode($_REQUEST));
?>
Read this post on google http://www.ryancoughlin.com/2009/05/04/how-to-use-jquery-to-serialize-ajax-forms/
I found it by googeling "jquery serialize form"
just use json_encode function like this json_encode($_POST)

HTML contact form in PHP block

I'm relatively new to PHP so forgive me if this seems obvious. How would you integrate a block of HTML code like this:
<form action="form.php" method="post" enctype="multipart/form-data">
<label></label>
<input name="name" required="required" placeholder="Your Name">
<label></label>
<input name="email" type="email" required="required" placeholder="Your Email">
<label></label>
<input name="address" type="name" required="required" placeholder="Your Address">
<label></label>
<textarea name="message" required > Dear Mr. X, Please support us... </textarea>
<input id="cancel" name="cancel" value="Cancel" />
<input id="submit" name="submit" type="submit" value="Submit">
</form>
into here:
if(empty($mpemail)){
echo "<h2>Send your MP our pre-written letter</h2>
<p>Unfortunately, your MP's email address is not listed in our database.</p>";
}
else {
echo "<h2>Send your MP our pre-written letter</h2>";
echo "<p>Please fill out the information required below:</p>";
**Insert Contact Form Here**
echo "<h2>Share this with your followers!</h2>";
echo 'Tweet ';
echo "<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>";
}
Thanks in advance for your help.
So, one option is to echo it out line by line, as your PHP code sample is doing, or in one big block. You can also combine PHP and HTML in the same file by starting and stopping the PHP interpreter. Here's an example of that:
if (empty($mpemail))
{
echo "<h2>Send your MP our pre-written letter</h2>
<p>Unfortunately, your MP's email address is not listed in our database.</p>";
}
else
{
?>
<h2>Send your MP our pre-written letter</h2>
<p>Please fill out the information required below:</p>
<form action="form.php" method="post" enctype="multipart/form-data">
<label></label>
<input name="name" required="required" placeholder="Your Name">
<label></label>
<input name="email" type="email" required="required" placeholder="Your Email">
<label></label>
<input name="address" type="name" required="required" placeholder="Your Address">
<label></label>
<textarea name="message" required > Dear Mr. X, Please support us... </textarea>
<input id="cancel" name="cancel" value="Cancel" />
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<h2>Share this with your followers!</h2>
Tweet
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<?php
}
In a professional production application, the "right way" to do it is to separate your HTML from your PHP by putting the logic of your application in a separate PHP file, and then putting your HTML in a "template" which you load and display from PHP. Here is a bit of info about getting started with template engines and Model View Controller patterns in an application. But, for what you're trying to do, the example above of starting and stopping the interpreter should work for you.

Categories