2

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 = "myemail@hotmail.com";
$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>
Johnny
  • 95
  • 2
  • 8
  • 1
    make sure you add form method is post – Karim Lahlou Feb 24 '14 at 19:11
  • 3
    And make sure the form fields have names, specifically the names you are using in your PHP script. – putvande Feb 24 '14 at 19:12
  • @putvande has it. Of course, lots of people followed with attempting reputation farming by posting the same thing four times. – ChicagoRedSox Feb 24 '14 at 19:14
  • btw I've seen it done through $_REQUEST or $_GET, what is the difference if you don't mind me asking? – Johnny Feb 24 '14 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: http://stackoverflow.com/a/1924958/2136840 – ChicagoRedSox Feb 24 '14 at 19:52

5 Answers5

2

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>
António Almeida
  • 9,620
  • 8
  • 59
  • 66
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>
sergio
  • 5,210
  • 7
  • 24
  • 46
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>
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

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>
Krish R
  • 22,583
  • 7
  • 50
  • 59
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.

prateekkathal
  • 3,482
  • 1
  • 20
  • 40