Need to convert this JS to PHP: <!DOCTYPE html> <html> <head> <style> a:link {co
ID: 3542629 • Letter: N
Question
Need to convert this JS to PHP:
<!DOCTYPE html>
<html>
<head>
<style>
a:link {color:blue;}
a:hover {color:green;}
</style>
</head>
<script type="text/javascript">
//the first function uses the name attribute of each form element for access.
function ProcessForm()
{
//use the value attribute for a textbox
var output = "Name: "+document.forms[0].name.value+"<br />";
//use checked attributed for a single checkbox
output+="Have PC? "+document.forms[0].havePC.checked+"<br />";
//use the value attribute for a dropdown list
output+="Location: "+document.forms[0].location.value+"<br />";
//radio button is more complicated, as there are three radio buttons with the same name (so they are put in an array); have to check one by one
var education;
if (document.forms[0].education[0].checked)
education=document.forms[0].education[0].value;
else if (document.forms[0].education[1].checked)
education=document.forms[0].education[1].value;
else if (document.forms[0].education[2].checked)
education=document.forms[0].education[2].value;
output+="Education: "+education+"<br />";
var skills=new Array()
var counter=0;
for (i=0;i<document.forms[0].skill.length;i++)
{
if (document.forms[0].skill[i].checked)
{
skills[counter]=document.forms[0].skill[i].value;
counter++;
}
}
var skillsText="";
for (n=0;n<skills.length;n++)
skillsText+=skills[n]+', ';
output+="Skills: "+skillsText+"<br />";
//list box is similar to checkboxes, except using its options as an array, and using the "selected" attribute;
var majors=new Array();
var counter=0;
for (i=0;i<document.forms[0].major.options.length;i++)
{
if (document.forms[0].major.options[i].selected)
{
majors[counter]=document.forms[0].major.options[i].value;
counter++;
}
}
var majorsText="";
for (n=0;n<majors.length;n++)
majorsText+=majors[n]+', ';
output+="Skills: "+majorsText+"<br />";
document.getElementById("confirm").innerHTML = output;
}
</script>
Explanation / Answer
Here you go
<?php
echo "<!DOCTYPE html> ";
echo "<html> ";
echo "<head> ";
echo "<style> ";
echo " ";
echo "a:link {color:blue;} ";
echo "a:hover {color:green;} ";
echo " ";
echo "</style> ";
echo " ";
echo "</head> ";
echo " ";
echo "<script type="text/javascript"> ";
echo "//the first function uses the name attribute of each form element for access. ";
echo "function ProcessForm() ";
echo "{ ";
echo "//use the value attribute for a textbox ";
echo "var output = "Name: "+document.forms[0].name.value+"<br />"; ";
echo "//use checked attributed for a single checkbox ";
echo "output+="Have PC? "+document.forms[0].havePC.checked+"<br />"; ";
echo "//use the value attribute for a dropdown list ";
echo "output+="Location: "+document.forms[0].location.value+"<br />"; ";
echo " ";
echo "//radio button is more complicated, as there are three radio buttons with the same name (so they are put in an array); have to check one by one ";
echo "var education; ";
echo "if (document.forms[0].education[0].checked) ";
echo "education=document.forms[0].education[0].value; ";
echo "else if (document.forms[0].education[1].checked) ";
echo "education=document.forms[0].education[1].value; ";
echo "else if (document.forms[0].education[2].checked) ";
echo "education=document.forms[0].education[2].value; ";
echo "output+="Education: "+education+"<br />"; ";
echo " ";
echo " ";
echo "var skills=new Array() ";
echo "var counter=0; ";
echo "for (i=0;i<document.forms[0].skill.length;i++) ";
echo "{ ";
echo "if (document.forms[0].skill[i].checked) ";
echo "{ ";
echo "skills[counter]=document.forms[0].skill[i].value; ";
echo "counter++; ";
echo "} ";
echo "} ";
echo " ";
echo "var skillsText=""; ";
echo "for (n=0;n<skills.length;n++) ";
echo "skillsText+=skills[n]+', '; ";
echo "output+="Skills: "+skillsText+"<br />"; ";
echo " ";
echo "//list box is similar to checkboxes, except using its options as an array, and using the "selected" attribute; ";
echo "var majors=new Array(); ";
echo "var counter=0; ";
echo "for (i=0;i<document.forms[0].major.options.length;i++) ";
echo "{ ";
echo "if (document.forms[0].major.options[i].selected) ";
echo "{ ";
echo "majors[counter]=document.forms[0].major.options[i].value; ";
echo "counter++; ";
echo "} ";
echo "} ";
echo "var majorsText=""; ";
echo "for (n=0;n<majors.length;n++) ";
echo "majorsText+=majors[n]+', '; ";
echo "output+="Skills: "+majorsText+"<br />"; ";
echo " ";
echo "document.getElementById("confirm").innerHTML = output; ";
echo "} ";
echo " ";
echo "</script> ";
?>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.