Hi all,

I'm trying to get the content of a div with php. I can't find a way to use javascript in php to do it so I was thinking that regex would be the way?

If it helps the div id is "editor".

Thanks in advance, if you need anything more then just ask.

Recommended Answers

You can use Javascript in PHP in the following way:

<?php
		for($i=0; $i<20; $i++)
	{?>
	<script type = 'text/javascript'>
		var hp="<?php echo $i; ?>";
		alert (hp);
		</script>
	<?php
	}
	?>
Jump to Post

Yes, try that!

Jump to Post

Hi all,

I'm trying to get the content of a div with php. I can't find a way to use javascript in php to do it so I was thinking that regex would be the way?

If it helps the div id is "editor".

Thanks in advance, if you need …

Jump to Post

Are you aware that PHP also has a DOM class?
check it out here:
http://us3.php.net/manual/en/book.dom.php

Kidding? That is for XML parsing J! :)
I thought PHP wanted JS out of business but then how …

Jump to Post

All 14 Replies

Member Avatar

You can use Javascript in PHP in the following way:

<?php
		for($i=0; $i<20; $i++)
	{?>
	<script type = 'text/javascript'>
		var hp="<?php echo $i; ?>";
		alert (hp);
		</script>
	<?php
	}
	?>

okay, so could I use

<?php
    $content=
?>
<script type = 'text/javascript'>
    document.getElementById("editor");		
</script>
<?php
    ;
?>

?

Thanks for the speedy reply by the way.

Member Avatar

Yes, try that!

Hi all,

I'm trying to get the content of a div with php. I can't find a way to use javascript in php to do it so I was thinking that regex would be the way?

If it helps the div id is "editor".

Thanks in advance, if you need anything more then just ask.

Are you aware that PHP also has a DOM class?
check it out here:
http://us3.php.net/manual/en/book.dom.php

Are you aware that PHP also has a DOM class?
check it out here:
http://us3.php.net/manual/en/book.dom.php

Kidding? That is for XML parsing J! :)
I thought PHP wanted JS out of business but then how could Server side things be done on Client.

@leegeorg07 ,
I would suggest you learn JQuery. It is really simple to do Some JS fun

Oops! Sorry 'bout that...

Oops! Sorry 'bout that...

Its no big deal buddy ;)

@estevemd is it possible to use jquery in php or to pass a jquery variable to php then?

Nothing special. It is just Javascript library that simplifies things. And yes it can play with PHP very well. What specifically you want to do? You might find it already full implemented there
see this example though a little complex

Thanks, I did a bit of research and I now have a working piece. but I'm having a problem with writing the data to a text file. I have this js code:

<script type = 'text/javascript'>
	var b=document.getElementsByTagName("editor");		
    	$.post("write.php", {"text": b});
</script>

and write.php is:

<?php
	$data = $_POST["text"];
   	$myFile = "testFile.txt";
	$fh = fopen($myFile, 'w') or die("can't open file");
	fwrite($fh, $data);
	fclose($fh);
?>

but when I open the file locally it is a blank file, can you offer any advice?

Scratch that last post, I have fixed the original problem, but I found a new one after fixing it, is says parse error in line 2 of the code below. any help?

<?php
	$data = $_REQUEST"text"];
   	$myFile = "testFile.txt";
	$fh = fopen($myFile, 'w') or die("can't open file");
	fwrite($fh, $data);
	fclose($fh);
?>

you're missing a [ in $_REQUEST"text"] change it to $_REQUEST["text"]

yeah, I saw that, thanks :) I think that this is now solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.