How to Get a Div Content in PHP

How can I get a div content in php

Use the php DomDocument class. http://www.php.net/manual/en/class.domdocument.php

$dom = new DOMDocument();

$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$divContent = $xpath->query('//div[@id="product_list"]');

PHP $_GET content in div

You could use a hidden textarea, and add the contents of the div to the textarea on form submit:

<form action="index.php?page=addPost&topic_id={TOPIC_ID}" method="post" onsubmit="getEditorContents(this);">
<div id="editor">
Lorem Ipsum...
</div>
<textarea style="display:none;" name="editor"><!-- --></textarea>
<input type="submit" name="submit" value="senden" >
</form>

<script>
function getEditorContents(form){
var html = document.getElementById("editor").innerHTML;
form.editor.value = html;
return true;
}
</script>

How to extract the contents inside a div based on its class?

Try this code,

$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$div = $xpath->query('//div[@class="mydiv1"]');
$div = $div->item(0);
$result = $dom->saveXML($div);
echo $result;

Get DIV content from external Website

This is what I always use:

$url = 'https://somedomain.com/somesite/';
$content = file_get_contents($url);
$first_step = explode( '<div id="thediv">' , $content );
$second_step = explode("</div>" , $first_step[1] );

echo $second_step[0];

Get content in div to PHP variable

Now i can think of 2 ways: as you said, writing the text from the contenteditable div to the input; and with ajax.

1.hidden input way

so the workflow here is that you copy what html is inside the contenteditable div to a hidden input with javascript. when you click the submit button, $_POST["my-hidden-input"] will store the text .

JS:

var contentText = document.getElementByClassName('no-formatting');
var hiddenInput = document.getElementById('hidden-input');

// copy the text to input when the user writes in contentText div
contentText.onkeyup = function() {
hiddenInput.innerHTML = this.innerHTML; // 'this' is pointing to contentText
}

add HTML to the form:

<input type="hidden" id="hidden-input" name="my-hidden-input">
<!-- note: your hidden input :) -->

add PHP:

$textcontent = $_POST["my-hidden-input"];

note: IMO this is silly becouse you can simply do it with a basic non-hidden type="text" input and you're done.

2.AJAX way

so here the workflow is a little complicated if you are not familiar with ajax. if so, google it(i am not giving you links becouse i learned it from Javascript: the definitive guide, 6th edition -- a really good book)

we create our html, with the contenteditable divs (for name and textcontent) and with a submit button (actually a div, but this doesn't matter). if the user clicks the div, it will send through an XMLHttpRequest (ajax) the text written in those divs to your .php file. the variables are stored in $_POST.

HTML & JS :

<!doctype html>
<html>
<head></head>
<body>

<div id="status"></div>

<div contenteditable="true" id="name"></div>
<div contenteditable="true" id="text"></div>
<div id="submit-button">Submit me</div>
<script>
function requestAjax(){
// Create our XMLHttpRequest object
var request = new XMLHttpRequest();

// the php file handling your file saving and other logic
var url = "my-php-file.php";

// variables later used in your php file
var name = document.getElementById("name").innerHTML;
var text = document.getElementById("text").innerHTML;

// needed in my-php-file.php. this is important becouse based on these parameters it will be stored in $_POST
var $POST = "name="+fn+"&text="+ln;

request.open("POST", url, true);

// Set content type header information for sending url encoded variables in the request
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

// Access the onreadystatechange event for the XMLHttpRequest object
request.onreadystatechange = function() {
if(request.readyState == 4 && request.status == 200) {
var someCallback = request.responseText;
document.getElementById("status").innerHTML = someCallback;
}
}

// Send the data to PHP now... and wait for response to update the status div
request.send($POST); // Actually execute the request

// in case you want to inform the user if it succeeded or failed
document.getElementById("status").innerHTML = "still not finished";
}

var submitButton = document.getElementById('submit-button');
// attach event handler
submitButton.onclick = requestAjax;
</script>

</body>
</html>

PHP:

<?php
//retrieved through AJAX
$name = $_POST['name'];
$textcontent = $_POST['text'];
// insert your code here ...

echo 'Ajax request completed!'; //this will inform the request we've made that the proccesing is done
?>

So basically this is it. now in the php file you have stored in variables the data's retrieved from the html file :D

How to get a div via PHP?

Once you have loaded the document to a DOMDocument instance, you can use XPath queries on it -- which might be easier than going yourself through the DOM.

For that, you can use the DOMXpath class.


For example, you should be able to do something like this :

$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[@class="text"]');
foreach ($tags as $tag) {
var_dump($tag->textContent);
}


(Not tested, so you might need to adapt the XPath query a bit...)



Related Topics



Leave a reply



Submit