Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Create a data structure containing a list of degrees available (i.e. PhD, MS,

ID: 3552373 • Letter: 1

Question

1)      Create a data structure containing a list of degrees available (i.e. PhD, MS, MA, BS, etc) and a price for each degree.

2)      Create a form that will prompt the user for their name, a credit card number, a check box indicating whether the credit card is visa or mastercard, and a drop-down list of the degrees offered (populated from the data created in step 1).

3)      Upon submission of the form, the code will validate that all of the requested data has been provided. In the event any of the data is missing, the form will be redisplayed with a message indicating what data was missing without losing any previously provided data (sticky form).

4)      If all the requested data has been provided, the webpage will display a confirmation message indicating the user

Explanation / Answer

<!DOCTYPE html>

<html>

<body >

<?php

$_ERRORS=array();

class details{

public $course;

public $fee;

function __construct($coursename, $fees) {

$this->course = $coursename;

$this->fee = $fees;

}

}


$list=array();

$list[0]=new details("PHD",100);

$list[1]=new details("MS",50);

$list[2]=new details("MA",90);

$list[3]=new details("BS",500);

if(!empty($_POST)){

if((!isset($_POST["name"])||empty($_POST["name"]))){

$_ERRORS["name"]="Please enter a name";

}

if((!isset($_POST["cardnumber"])||empty($_POST["cardnumber"]))){

$_ERRORS["cardnumber"]="Please enter a valid Card Number";

} else if(strlen($_POST["cardnumber"])!=16){

$_ERRORS["cardnumber"]="Card number should have 16 digits";

}

if((!isset($_POST["cardtype"])||empty($_POST["cardtype"]))){

$_ERRORS["cardtype"]="Please select card type";

}

if(isset($_POST["2"])){

if($_POST["2"]==$_POST["1"]){

$_ERRORS["2"]="Two courses must be different. If you like only one course, hit 'Remove Course'";

}

}

}



if(count($_POST)==0||(count($_POST)>0&&count($_ERRORS)>0)){

?>

<script>


function addCourse()

{

  

if(document.getElementById("numberCourses").value=="1"){

document.getElementById("courses2").innerHTML = "<select id="course" name="2" >"+document.getElementById("course").innerHTML+"</select></br>";

document.getElementById("numberCourses").value="2";

document.getElementById("addrem").innerHTML="Remove Course";

}else{

document.getElementById("courses2").innerHTML = "";

document.getElementById("numberCourses").value="1";

document.getElementById("addrem").innerHTML="Add Course";

}

}

</script>

<form method="post" id="form" >

Name:<input type="text" name="name" value="<?php if(isset($_POST["name"])){echo $_POST["name"];}?>"/><?php if(isset($_ERRORS["name"])){echo "<font color='red'>".$_ERRORS["name"]."</font>";}?></br>

Card Number: <input type="number" name="cardnumber" maxlength="16" value="<?php if(isset($_POST["cardnumber"])){echo $_POST["cardnumber"];}?>"/><?php if(isset($_ERRORS["cardnumber"])){echo "<font color='red'>".$_ERRORS["cardnumber"]."</font>";}?></br>

Card Type:

<input type="radio" name="cardtype" value="master">Master

<input type="radio" name="cardtype" value="visa">Visa<?php if(isset($_ERRORS["cardtype"])){echo " "."<font color='red'>".$_ERRORS["cardtype"]."</font>";}?></br>

Course(s):


<div id="courses">


<select id="course" name="1" >

<?php

foreach( $list as $i){

echo '<option value="';

echo $i->course;

echo '" ';

if(isset($_POST["1"])&&$_POST["1"]==$i->course)

{

echo 'selected="selected"';

}

echo '>';

echo $i->course;

echo '</option>';

}

?>

</select></br>

</div>

<div id="courses2">

<?php if(isset($_POST["2"])&&!empty($_POST["2"])){ ?>

<select id="course" name="2" >

<?php

foreach( $list as $i){

echo '<option value="';

echo $i->course;

echo '" ';

if(isset($_POST["2"])&&$_POST["2"]==$i->course)

{

echo 'selected="selected"';

}

echo '>';

echo $i->course;

echo '</option>';

}?></select><?php if(isset($_ERRORS["2"])){echo " "."<font color='red'>".$_ERRORS["2"]."</font>";}?></br>

<?php

}?>


</div>

<input type="Submit" value="Submit"/>

<input id="numberCourses" type="hidden" name="numberCourses" value="<?php if(isset($_POST["2"])&&!empty($_POST["2"])){ echo 2; } else { echo 1;} ?>"/>

</form>


<?php if(isset($_POST["2"])&&!empty($_POST["2"])){ ?>

<button id="addrem">Remove Course</button></br>

<?php } else { ?>

<button id="addrem">Add Course</button></br>

<?php }?>



<?php

}

else{

echo "Name: ".$_POST["name"]."</br>";

echo "Card Number: ".$_POST["cardnumber"]."</br>";

if(isset($_POST["2"]))

{

echo "Course 1: ".$_POST["1"]."</br>"."Amount: ";

foreach($list as $i){

if($i->course==$_POST["1"]){

$am1 = $i->fee;

echo $am1."</br>";

}

}

echo "Course 2: ".$_POST["2"]."</br>"."Amount: ";

foreach($list as $i){

if($i->course==$_POST["2"]){

$am2 = $i->fee;

echo $am2."</br>";

}

}

$am = $am1+$am2;

echo "Total: ".$am."</br>";

} else{

echo "Course: ".$_POST["1"]."</br>"."Amount: ";

foreach($list as $i){

if($i->course==$_POST["1"]){

$am1 = $i->fee;

echo $am1."</br>";

}

}

}



}


?>



</select></br></input>

</body>

</html>