Jump to content

PHP array and innerhtml help


alittee

Recommended Posts

hey haven't really ever posted but I cant really spend too much more time on this problem I'm having. Also sorry if this is in the wrong section

 

Is there a way to store values from the innerhtml of a div to an array which I have just written to the innerhtml using javascript, if that makes sense?

Link to comment
Share on other sites

I think you're a bit confused about how things work in the web world. This confusion is making things harder for you to accomplish things, even explaining what you want. There are a number of steps that happen for each page view, starting on the server side:

[*]First the HTTP server receices a request for a page.

This request can contain GET, POST and/or upload (multipart) data, sent by the browser/user. Either by clicking on a link, or sending a form.

[*]PHP starts up, reading its configuation, and inserting any data sent by the browser into the superglobal variables ($_GET, $_POST, etc).

[*]Then PHP parses the PHP code you've written, to act upon the request (and its associated data) as sent by the user. This is where you'll want to do all of the processing, before getting to the next step1.

[*]Right before the server sends content to the browser, it'll send the HTTP headers. This gives the client some meta-information about the server and content it's about to receive.

[*]Then the final step on the server-side is the sending of the actual (HTML) content, the stuff which the gets used in the next batch of steps...

Namely the Client side handling, which the server side has no idea occurs:

[*]The first thing a browser will do after receiving the content, is HTML parsing. Where it takes the markup and displays the basic logical structure of the content.

[*]CSS is then parsed to give the site a proper layout, as per your wishes.

[*]JavaScript parsing, including all of the dynamic stuff that you want to happen.

[*]User actions, which sends a new request to the browser. Which starts the cycle anew, at step 1 on the server-side list.

Incidentally, this new request can be sent via JavaScript as well, through an AJAX call.

 

Now, as you can see from this "flowchart", PHP can output just about anything to the browser as it is the browser that determines what the content it parses actually is. But the only way to get content from the browser to PHP, is via a HTTP request. So to get anything into a PHP array, you have to send it via a GET or POST query.

 

Considering all this, can you try to explain again what it is that you want to do?

 

1 Notice that you can intermingle the HTTP headers and HTML content steps with the PHP parsing step, but you cannot send any more HTTP headers after sending HTML content. That's one of the primary reasons why it's highly recommended to wait until you've completed all of the processing before you exit PHP mode and/or print anything to the client.

Link to comment
Share on other sites

Is there a way to store values from the innerhtml of a div to an array which I have just written to the innerhtml using javascript, if that makes sense?

 

Yes, but if you want to pass data from Javascript to PHP you'll need to use AJAX. Using the jQuery Javascript framework makes this very easy. All you have to do is include the jQuery framework, and then:

<script type="text/javascript">
$(function(){
// select an element
$('div').each(function(){
	// update the innerHTML
	var newHtml = 'This is new HTML';
	$(this).html(newHtml);

	// send the New HTML to a PHP script
	$.ajax({
		url  : 'the_php_script.php',
		data : { html : newHtml },
		type : 'post'
	});
});
});
</script>

 

And then your PHP script:

<?php

if (isset($_POST['html'])) {
$html = $_POST['html'];

// save HTML to a database, or whatever else you want to do
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.