3

I want to have an email sending system on my site.

The problem is when I try to assign a variable text from my HTML file it does not happen. I want that what is inside the variable should be written in the message of the email. Here's my code:

<html>
    <?php include('form.php'); ?>
    <head>

    </head>

    <body>
        <form action="./form.php" method="post">
        <div name="name"><input type="text" id="name"></div>
        <div name="surname"><input type="text" id="surname"></div>
        <div name="message"><textarea rows="4" cols="50" id="message">Inserisci qui il tuo testo.</textarea></div>
        <div name="subject"><select id="subject">
            <option selected="selected">--Inserisci Oggetto--</option>
            <option>Registrazione al sito</option>
            <option>Recupero credenziali</option>
        </select></div>
        <input type="submit" value="Invia penzolini"/>
        <input type="hidden" name="button_pressed" value="1" />
        </form>
    </body>
</html>

        <?php

    $dochtml = new domDocument();
    $dochtml->loadHTML('index.html');

    if(isset($_POST['button_pressed']))
    {

    //prendi nome
    $name = $dochtml->getElementById('name');

    //prendi cognome
    $surname = $dochtml->getElementById('surname');

    //prendi l'oggetto della mail
    $subject = $dochtml->getElementById('subject');

    //msg<70 caratteri
    $msg = "Inviato da" . ' ' . $name . $surname . ' ' . $dochtml->getElementById('message'); // /n=spazio  

    // manda mail
    mail("panzersit@gmail.com",$subject,$msg);
    echo 'Email inviata.';
    }
    ?>
  • What is the name of this file that we are looking at here? – kojow7 Feb 08 '19 at 21:36
  • 2
    What you are probably looking for is `$_POST['name']`. Not a dom document. These are your post variables. – Ibu Feb 08 '19 at 21:37
  • `$name = $dochtml->getElementById('name');` this is like a mix of PHP and JS (I know it's DOM, but it looks like a JS way of accessing data .. lol ) , in PHP we just access them by name in the `$_POST` data. `$_POST['name']` – ArtisticPhoenix Feb 08 '19 at 21:37
  • The confusing thing though is the `include('form.php')`. What file is being included here? It's also being included above the `head` section which is also problematic. – kojow7 Feb 08 '19 at 21:41

3 Answers3

6

PHP cannot directly access your DOM. PHP runs only the server and on simple terms takes requests and gives a response.

Upon submit of this form to it's action page ./form.php, the values of the input forms are stored in the $_POST in a key named after it's name attribute. In your HTML code, add name attributes to the input tags like so:

<form action="./form.php" method="post">
    <div name="name"><input type="text" name="name"></div>
    <div name="surname"><input type="text" name="surname"></div>
</form>

Now if I submit this form and input Zachary for name input tag and Taylor for surname input tag, I can grab these values like so:

in ./form.php file:

$name = $_POST['name']; // "Zachary"

$surname = $_POST['surname']; // "Taylor"

To validate if anything was input in the first place, use: isset($_POST['key']) since SOMETIMES input values with a null value are not even sent to the action page. This prevents PHP from throwing errors if you reference a $_POST key that does not exist.

Zac
  • 1,719
  • 3
  • 27
  • 48
-2

Looking at documentation ( http://php.net/manual/pt_BR/domdocument.getelementbyid.php ) i'm locate a pontual observation:

"Please note that if your HTML does not contain a doctype declaration, then getElementById will always return null."

Post the contents of form.php will help to recreate the scenario.

William
  • 19
  • 2
-3

To get the posted data from your submitted form, you can do it using $_POST['fieldname']

Just try as below and see what you are getting after form submission.

//echo "<pre>";
//print_r($_POST);

Uncomment above 2 lines, see what you are getting and COMMENT IT AGAIN.

if( isset($_POST['name']) )
{
    $name = $_POST['name'];
}
if( isset($_POST['surname']) )
{
    $surname = $_POST['surname'];
}
if( isset($_POST['subject']) )
{
    $subject = $_POST['subject'];
}
G_real
  • 1,137
  • 1
  • 18
  • 28
  • This should be a comment – Ice76 Feb 08 '19 at 21:41
  • @Ice76. Did you read my whole answer? I wrote that just see what you are getting and comment above 3 lines. – G_real Feb 08 '19 at 21:43
  • not my question, and also i did not downvote. as you can see my rep hasnt changed. trying to do you a favor before getting flamed – Ice76 Feb 08 '19 at 21:45
  • @kojow7 **I know this MUST be comment**. But it's the way to find out what that user is submitting the form. He can access the data using `$_POST['fieldname']` – G_real Feb 08 '19 at 21:48
  • I don't think you understood what they meant. They're not talking about the code. They're saying that your answer should have been posted as a comment here on Stack Overflow instead of as an answer. – JJJ Feb 09 '19 at 07:00