Php get html element by id

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:


    
    

    

    
        
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("",$subject,$msg); echo 'Email inviata.'; } ?>

asked Feb 8, 2019 at 21:33

Php get html element by id

4

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:

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.

answered Feb 8, 2019 at 21:50

Php get html element by id

0

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.

answered Feb 8, 2019 at 22:38

Php get html element by id

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 "
";
//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'];
}

answered Feb 8, 2019 at 21:41

G_realG_real

1,0871 gold badge17 silver badges27 bronze badges

5

How can I get HTML id in PHP?

document. getElementById('Msg')..
PHP is server-side, which means it runs on a machine different from the user..
Javascript and HTML are client-side, which means they run on the user's machine..

What does get element by id do?

Document.getElementById() The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

Why is getElementById null?

getelementbyid(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element.

How do I click a document in getElementById?

Add document getElementById onclick Event.
getElementById() method is used to select an element by its id attribute. ... .
To add an onclick even you can follow 2 different ways..
To add an onclick event on an element, you can use addEventListener() method..