What's new

Java PHP Code Need Help

Need Help ppl

Grasshopper
Hi po.Patulong naman po dapat yung submit button is mag output ng "Output: Your Score is: (score 1-5)/5...pero young nagawa kong code is naging alert type siya

The Code:

<!DOCTYPE html>
<html>
<body>

<script>
var answers = ["A","A","B","D","C"],
tot = answers.length;

function getCheckedValue( radioName ){
var radios = document.getElementsByName( radioName ); // Get radio group by-name
for(var y=0; y<radios.length; y++)
if(radios[y].checked) return radios[y].value; // return the checked value
}

function getScore(){
var score = 0;
for (var i=0; i<tot; i++)
if(getCheckedValue("question"+i)===answers) score += 1; // increment only
return score;
}

function returnScore(){
alert("Your score is "+ getScore() +"/"+ tot);
}
</script>

<form>
<h3> 1. It is an American multinational technology company based in Menlo Park, California.</h3>
<input type="radio" name="question0" value="A"> Facebook
<input type="radio" name="question0" value="B"> Twitter
<input type="radio" name="question0" value="C"> Google
<input type="radio" name="question0" value="D"> Netflix

<h3> 2. It is an American microblogging and social networking service on which users post and interact with messages.</h3>
<input type="radio" name="question1" value="A"> Twitter
<input type="radio" name="question1" value="B"> YøùTùbé
<input type="radio" name="question1" value="C"> Google
<input type="radio" name="question1" value="D"> Microsoft

<h3> 3. It is an American multinational technology company that specializes in Internet-related services and products.</h3>
<input type="radio" name="question2" value="A"> Microsoft
<input type="radio" name="question2" value="B"> Google
<input type="radio" name="question2" value="C"> Netflix
<input type="radio" name="question2" value="D"> YøùTùbé

<h3> 4. It is an American online video sharing and social media platform owned by Google.</h3>
<input type="radio" name="question3" value="A"> Microsoft
<input type="radio" name="question3" value="B"> Tiktok
<input type="radio" name="question3" value="C"> Google Play
<input type="radio" name="question3" value="D"> YøùTùbé

<h3> 5. It is an American pay television over-the-top media service and original programming production company.</h3>
<input type="radio" name="question4" value="A"> Microsoft
<input type="radio" name="question4" value="B"> YøùTùbé
<input type="radio" name="question4" value="C"> Netflix
<input type="radio" name="question4" value="D"> Tiktok
<br> <br>
</form>
<button onclick="returnScore()">Submit</button>

</body>
</html>
 
Last edited:
Hi po nasa taas yung code..Patulong naman po dapat yung submit button is mag output ng "Output: Your Score is: (score 1-5)/5...pero young nagawa kong code is naging alert type siya
 
<!DOCTYPE html>
<html>
<body>

<script>
var answers = ["A","A","B","D","C"],
tot = answers.length;

function getCheckedValue( radioName ){
var radios = document.getElementsByName( radioName ); // Get radio group by-name
for(var y=0; y<radios.length; y++)
if(radios[y].checked) return radios[y].value; // return the checked value
}

function getScore(){
var score = 0;
for (var i=0; i<tot; i++)
if(getCheckedValue("question"+i)===answers) score += 1; // increment only
return score;
}

function returnScore(){
alert("Your score is "+ getScore() +"/"+ tot);
}
</script>
<form>
<h3> 1. It is an American multinational technology company based in Menlo Park, California.</h3>
<input type="radio" name="question0" value="A"> Facebook
<input type="radio" name="question0" value="B"> Twitter
<input type="radio" name="question0" value="C"> Google
<input type="radio" name="question0" value="D"> Netflix

<h3> 2. It is an American microblogging and social networking service on which users post and interact with messages.</h3>
<input type="radio" name="question1" value="A"> Twitter
<input type="radio" name="question1" value="B"> YøùTùbé
<input type="radio" name="question1" value="C"> Google
<input type="radio" name="question1" value="D"> Microsoft

<h3> 3. It is an American multinational technology company that specializes in Internet-related services and products.</h3>
<input type="radio" name="question2" value="A"> Microsoft
<input type="radio" name="question2" value="B"> Google
<input type="radio" name="question2" value="C"> Netflix
<input type="radio" name="question2" value="D"> YøùTùbé

<h3> 4. It is an American online video sharing and social media platform owned by Google.</h3>
<input type="radio" name="question3" value="A"> Microsoft
<input type="radio" name="question3" value="B"> Tiktok
<input type="radio" name="question3" value="C"> Google Play
<input type="radio" name="question3" value="D"> YøùTùbé

<h3> 5. It is an American pay television over-the-top media service and original programming production company.</h3>
<input type="radio" name="question4" value="A"> Microsoft
<input type="radio" name="question4" value="B"> YøùTùbé
<input type="radio" name="question4" value="C"> Netflix
<input type="radio" name="question4" value="D"> Tiktok
<br> <br>
</form>
<button onclick="returnScore()">Submit</button>

</body>
</html>
 
Ayos po yung code ang isa ko nalang problema dapat po pag pinindot yung submit button dapat lumabas yung "Your score is " sa same page? Ano po kayang papalitan ko sa code ko?

Pe-prevent mo mag reload yung page?

Dapat hindi submit yan, normal na Button lang dapat.

Then sa event na click ng Button. Doon lang magco-compute.

After compute labas ng Output sa specific na id ng element/tag na gusto mo lagyan.

Edit:

I see ngayon ko lang nakita code mo. Kung ayaw mong nasa Alert.

GetElementById then after compute labas ng Output sa specific na id ng element/tag na gusto mo lagyan.
 
Last edited:
Kung gusto mong idisplay sa same page yung total score, pwede kang gumamit ka ng jquery.

JavaScript:
$("#submit-button").on("click", function(event e){ // yung submit button mo na may id="submit-button".
    e.preventDefault(); // Kung naka input type=submit ka.
    $("#output-div").html("<div>Total Score: " + total_score + "</div>");
});
 
Last edited:
Para di mag reload pag pinindot mo yung button, lagyan mo ng type="button" kasi by default naka type="submit" yan.
HTML:
<button type="button" onclick="returnScore()">Submit</button>

Yung sa javascript naman hindi naman siya "naging alert type". Kasi talagang alert yung ginawa mo.

Gawin mo mag lagay ka muna ng atleast p tag na may class or id or kahit empty lang pwede naman <p class="score"></p> sa baba ng button.

Then ito yung gawin mo sa return score function mo:
JavaScript:
function returnScore(){
    document.querySelector(".score").innerText = "Your score is " + getScore() + "/" + tot;
}
 
Para di mag reload pag pinindot mo yung button, lagyan mo ng type="button" kasi by default naka type="submit" yan.
HTML:
<button type="button" onclick="returnScore()">Submit</button>

Yung sa javascript naman hindi naman siya "naging alert type". Kasi talagang alert yung ginawa mo.

Gawin mo mag lagay ka muna ng atleast p tag na may class or id or kahit empty lang pwede naman <p class="score"></p> sa baba ng button.

Then ito yung gawin mo sa return score function mo:
JavaScript:
function returnScore(){
    document.querySelector(".score").innerText = "Your score is " + getScore() + "/" + tot;
}
OK NA PO SALAMAT
 

Similar threads

Back
Top