1

I come from C# background and im new to php, I have an html form submits it's values to the following php file and for some reason the values are not set, what am I doing wrong?

<?php
include('Mail.php');
$recipients = "[email protected]";
$name = $_POST["txtName"] ;
$from = $_POST["txtEmail"] ;
$subject = $_POST["txtAppName"] ;
$comments = $_POST["txtComment"] ;

$headers = array (
    'From' => $from,
    'To' => $recipients,
    'Subject' => $subject,
);
$body = "Name: ".$name."\r\n"."Comments: "."\r\n".$comments;
$mail_object =& Mail::factory('smtp',
    array(
        'host' => 'prwebmail',
        'auth' => true,
        'username' => 'username',
        'password' => 'pass', # As set on your project's config page
        'debug' => true, # uncomment to enable debugging
    ));
$mail_object->send($recipients, $headers, $body);
?>

And here is the html form:

<form action="sendfeedback.php" method="post">
            <div><input placeholder="Name" id="txtName" type="text" /></div>
            <div><input placeholder="Email (Optional)" id="txtEmail" type="text" /></div>
            <div><input placeholder="Application Name (Type in the application name you're using)" id="txtAppName" type="text" /></div>
            <div style="height:4px"></div>
            <div><textarea placeholder="Comments..." id="txtComment"></textarea></div>
            <div style="width:407px; text-align:right;"><input id="submitComment" type="submit" value="Send" /></div>
        </form>
5
  • 1
    make sure you add form method is post Commented Feb 24, 2014 at 19:11
  • 3
    And make sure the form fields have names, specifically the names you are using in your PHP script. Commented Feb 24, 2014 at 19:12
  • @putvande has it. Of course, lots of people followed with attempting reputation farming by posting the same thing four times. Commented Feb 24, 2014 at 19:14
  • btw I've seen it done through $_REQUEST or $_GET, what is the difference if you don't mind me asking? Commented Feb 24, 2014 at 19:19
  • $_GET refers to the querystring in the URL. So if your form uses GET instead of POST, the url will look like sendfeedback.php?txtName=ABC&txtEmail=XYZ..., and the $_GET variable will contain those parameters. $_REQUEST combines $_POST, $_GET, and $_COOKIE by default. More here: stackoverflow.com/a/1924958/2136840 Commented Feb 24, 2014 at 19:52

5 Answers 5

2

There is no name attribute in your input fields, You have used id attributes, but you should use name attribute for posting the form values.

<form action="sendfeedback.php" method="post">
        <div><input name="txtName" placeholder="Name" id="txtName" type="text" /></div>
                    ^^^^^^^^^^^^^^
        <div><input name="txtEmail"  placeholder="Email (Optional)" id="txtEmail" type="text" /></div>
        <div><input  name="txtAppName"  placeholder="Application Name (Type in the application name you're using)" id="txtAppName" type="text" /></div>
        <div style="height:4px"></div>
        <div><textarea name="txtComment" placeholder="Comments..." id="txtComment"></textarea></div>
        <div style="width:407px; text-align:right;"><input id="submitComment" type="submit" value="Send" /></div>
    </form>
Sign up to request clarification or add additional context in comments.

Comments

1

Your form inputs should have the attribute name, like this:

<form action="sendfeedback.php" method="post">
    <div><input placeholder="Name" id="txtName" name='txtName' type="text" /></div>
    <div><input placeholder="Email (Optional)" id="txtEmail" name="txtEmail" type="text" /></div>
    <div><input placeholder="Application Name (Type in the application name you're using)" id="txtAppName" name="txtAppName" type="text" /></div>
    <div style="height:4px"></div>
    <div><textarea placeholder="Comments..." id="txtComment" name='txtComment'></textarea></div>
    <div style="width:407px; text-align:right;"><input id="submitComment" type="submit" value="Send" /></div>
</form>

Comments

1

The reason that in your did not set attribute name for your inputs, so chanhe it to

<form action="sendfeedback.php" method="post">
            <div><input name = 'txtName' placeholder="Name" id="txtName" type="text" /></div>
            <div><input name = 'txtEmail'  placeholder="Email (Optional)" id="txtEmail" type="text" /></div>
            <div><input name ='txtAppName' placeholder="Application Name (Type in the application name you're using)" id="txtAppName" type="text" /></div>
            <div style="height:4px"></div>
            <div><textarea name ='txtComment' placeholder="Comments..." id="txtComment"></textarea></div>
            <div style="width:407px; text-align:right;"><input id="submitComment" type="submit" value="Send" /></div>
        </form>

Comments

1

You're missing the name attributes of your inputs:

    <form action="sendfeedback.php" method="post">
        <div><input placeholder="Name" name="txtName" id="txtName" type="text" /></div>
        <div><input placeholder="Email (Optional)" name="txtEmail" id="txtEmail" type="text" /></div>
        <div><input placeholder="Application Name (Type in the application name you're using)" name="txtAppName" id="txtAppName" type="text" /></div>
        <div style="height:4px"></div>
        <div><textarea placeholder="Comments..." name="txtComment"  id="txtComment"></textarea></div>
        <div style="width:407px; text-align:right;"><input id="submitComment" type="submit" value="Send" /></div>
    </form>

Comments

1

Add the name attribute to each of the input tags as shown in the above answers.

Also, I would appreciate if u use the form this way

if(isset($_POST['txtName'] || isset($_POST['txtPass'])) {
    $txtName = $_POST['txtName'];
    $txtPass = $_POST['txtPass'];
    //Everything else here instead require('mail.php');
}

I hope it solves ur issues and helps u in debugging and securing ur codes as well.

Comments

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.