Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
PHP
<meta name="viewport" content="width=device-width, initial-scale=1">
session_start();   
    $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
    $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
echo "<font color=".$color.">".ColorPreview."</font><br />".$color;
?><br /><script>function myfunction(){var a=confirm('Do you really want to change the color?');if(a==1){javascript:history.go(0);}}</script><input type="submit" value="Change Color" onclick="myfunction()">
Posted
Updated 23-Mar-15 18:35pm
v3
Comments
Sergey Alexandrovich Kryukov 22-Mar-15 19:26pm    
Why?
In all cases, there is no such thing as "convert". Decide what you need to achieve, and, if you cannot do it, explain it in your question, don't forget to explain why, what's the ultimate goals. Even your PHP code does not look like anything reasonable. Set the goal and implement what you need in JavaScrip.
—SA
Afzaal Ahmad Zeeshan 23-Mar-15 21:47pm    
The ultimate goal here is totally neither constructive nor helpful. He wants to create an Android application using some third-party framework such as Apache Cordova, to develop Android applications in HTML and JavaScript based style. PHP, in first place, cannot be converted to JavaScript; unless Node.js is used, secondly, JavaScript along would not be enough; he would use third-party libraries, too. Which makes this question totally non-constructive and vague. Please see Solution 1.

Quite possibly there are a lot of points you would first need to consider. PHP is a server-side scripting language (probably based on either C or C++). JavaScript on the other hand is a client-side programming language used in browsers to manipulate the DOM.

They both work separately. You use JavaScript along with PHP, to create a dynamic website; such as ajax requests, loading data etc. They cannot be converted to either one, PHP can never be converted to JavaScript; unless you use Node.js[^] to program your website's server-side script in JavaScript.

Then, Android is programmed in Java; JavaScript is just a prototype for Java language. You cannot create applications in JavaScript. Although now a days software (or simply cross-platform) application development has been made possible through HTML5/CSS3/JavaScript to engage more developers to the platform, using clients or frameworks such as Apache Cordova[^]. But you won't be able to write native JavaScript applications, why? Because you still have to stick to their API. A simple alert('Hello world'); call in browser would create a dialog, but in Android you would have to use Apache Cordova's API to generate dialog boxes and all other stuff. My view about HTML and JavaScript programming is that this is the worst case of programming a software, no efficiency, no full framework support, too many factors slowing down your applications and many other factors are due to which I prefer programming an application in native language; Java would be perfect.

So according to the above points, your point of view of converting PHP code in JavaScript for Android programming is a total vague reason to convert server-side scripting code into a software-level client code. Don't do it!
 
Share this answer
 
JavaScript
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>var color = "#";
for (k = 0; k < 3; k++) {
    color += ("0" + (Math.random()*256|0).toString(16)).substr(-2);
}</script><script>document.write('<font color="+ color +">'+ color +'</font>');</script><br /><script>function myfunction(){var a=confirm('Do you really want to change the color?');if(a==1){javascript:history.go(0);}}</script><input type="submit" value="Change Color" onclick="myfunction()">
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900