954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Run PHP from Javascript

Hey guys,
I'm having a little bit of trouble here.
I have a variable called Latest1 and it's value is some HTML and PHP code.
I have a function that does document.getElementById().innerHTML = Latest1;
So when that happens, it runs the HTML code but for some reason it doesn't run the PHP code.
Because of Javascripts stupid string weirdness, it won't let me type <? because it then treats that as not a string... Which is so annoying.
So, I had to use escape codes:
<?php = %3C%3Fphp
I replaced all my '<?php' to '%3C%3Fphp' and it doesn't work :(

Please help!
Thanks! :-/

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

It might be easier to use PHP to write your javascript.

You can also play around with this to see if you can get what you are after:

Otherwise, AJAX?

SolidSolutions
Junior Poster
136 posts since Jul 2010
Reputation Points: 24
Solved Threads: 24
 

I have a button that does a JavaScript function to change a div's content to some PHP code, that means I can't use PHP to do the JavaScript.

I don't really like AJAX...

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

Can you run that PHP code on page load and "store" that info into a text box value and make that "hidden". Then on the button click you can get that info?

SolidSolutions
Junior Poster
136 posts since Jul 2010
Reputation Points: 24
Solved Threads: 24
 

Uh, how could I store that PHP code into a text box?

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

something like

<?php

$someVal = //some code to get if needed
$someCodeToStore = //some code to get whatever needed

echo "<input type='text' value='$someCodeToStore' />"

?>
SolidSolutions
Junior Poster
136 posts since Jul 2010
Reputation Points: 24
Solved Threads: 24
 

I see...

But how could I get the info from the text field when I click it?

<button onClick="getInfo()" name="codes" />
<script>
function getInfo(){
var code = oForm.element["codes"].value;
}
</script>


???

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

give the input an id:

echo "<input type='text' id="storedCode" value='$someCodeToStore' />"


Use javascript to get the code by retrieving the value:

var textBoxCode = document.getElementById('storedCode').value;
SolidSolutions
Junior Poster
136 posts since Jul 2010
Reputation Points: 24
Solved Threads: 24
 

Thank you!!

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: