Php get value html element

In the past, whenever I needed to get the value of an html element, I would always submit a form to load a different page. Something like:

page1.php


   

page2.php


My question is, is it possible to get the value of 'input1' on the same page [page1.php] without requiring clicking a submit button?

Deduplicator

43.6k6 gold badges62 silver badges110 bronze badges

asked Sep 14, 2011 at 1:29

With jQuery:

 
 
 
 
 
 
 

Response from server:
$.post['test.php',{'data': "value1"}, function [data] { $['#response'].text[data]; }];

Non-jQuery IE doesn't have the XMLHttpRequest object so here is a shim for IE:

Yes, using ajax / javascript:

var formData = new FormData[];

formData.append["input1", "value1"];

var xhr = new XMLHttpRequest[];
xhr.open["POST", "page1.php"];
xhr.send[formData];

if [typeof XMLHttpRequest == "undefined"] XMLHttpRequest = function[] {
    try {
        return new ActiveXObject["Msxml2.XMLHTTP.6.0"];
    }
    catch [e] {}
    try {
        return new ActiveXObject["Msxml2.XMLHTTP.3.0"];
    }
    catch [e] {}
    try {
        return new ActiveXObject["Microsoft.XMLHTTP"];
    }
    catch [e] {}
    //Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is redundant
    throw new Error["This browser does not support XMLHttpRequest."];
};

answered Sep 14, 2011 at 1:31

JoeJoe

78.4k18 gold badges125 silver badges143 bronze badges

11

What for? Do you want to submit it to another file without needing to actually "submit" the form?

You use JavaScript for that.

document.form1.input1.value

answered Sep 14, 2011 at 1:32

Rolando CruzRolando Cruz

2,7841 gold badge15 silver badges24 bronze badges

2

Not the answer you're looking for? Browse other questions tagged php html or ask your own question.

In this particular, How to Get HTML Tag Value in PHP, article we use the PHP DOMDocument Class which represents the HTML or XML document also serves as the root of the document tree.

How to Get HTML Tag Value in PHP

Here we discuss 4 tasks where we use the PHP DOMDocument class to get the value of HTML tags,

  1. How to get a paragraph

    tag value using PHP

  2. How to get tag value using PHP
  3. How to store value in PHP
  4. How to access HTML element in PHP

In This Article

  • 1 How to get a paragraph

    tag value using PHP

  • 2 How to get tag value using PHP
  • 3 How to Store span Value in PHP
  • 4 How to Access HTML Element in PHP
    • 4.1 Related

How to get a paragraph

tag value using PHP

On the below example How to get p tag value using PHP, we create a paragraph with HTML ID attribute and set the text inside the P tag.

loadHTML[$htmlEle];

echo$domdoc->getElementById['paraID']->nodeValue;

Output: Paragraph Sports

Compiler: //phpcoder.tech/php-online-editor/

Code Explanations:

  • Using the PHP DOMDocument Class, call the DOMDocument object.
  • Call the predefined loadHTML[] function with variable parameters.
  • Using the getElementById[] DOM function we get the HTML element value.

How to get tag value using PHP

loadHTML[$htmlEle];

echo$domdoc->getElementById['SpanID']->nodeValue;

Output: Span Sports

This is also the same process and the code with changed ID.

How to Store span Value in PHP

If you want to store only span value then do the same code and create a variable on the last and assign the previous value what you get using PHP DOMDocument.

loadHTML[$htmlEle];

$spanValue=$domdoc->getElementById['SpanID']->nodeValue;

//Store span Value in PHP variable

echo$spanValue;

Output: Span Sports

How to Access HTML Element in PHP

Here is complete quick summary of how to get Html tag value in PHP or access HTML element,

  • getElementsByTagName[‘a’]
  • getElementById[‘SpanID’]

For more you can check on the officials site, //www.php.net/manual/en/class.domdocument.php

Here is a complete reference about access the HTML element in PHP with an example.

Also Check:

  • How to Convert JSON to Array PHP
  • How to Store Data In Cookies Using JavaScript
  • Steps to Upload Multiple Images in PHP with Preview
  • Multiple Barcode Generator in PHP

Happy Coding..!

Was this article helpful?

YesNo

Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

How can get HTML element value in PHP?

php\n$data = $_POST['input1'];\nprint[ "value is: $data" ];\n?>

How get p value from tag in PHP?

$p = $domdoc->getElementById['categories']->getElementsByTagName['p']->item[0];

How can I call HTML ID in PHP?

PHP cannot "call an HTML control". If you want to dynamically modify things in the browser, you'll have to do it client-side using Javascript. It is client-side, so you have to use Javascript, not PHP.

How can change HTML element value in PHP?

If you want change it dynamically in client-side, there is no way but ajax. PHP works at server-side and you have to use post/get requests. Show activity on this post. Form fields sent to php in a $_REQUEST, $_GET or $_POST variables...

Chủ Đề