[html code]
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
</head>
<body>
<form method="POST" action="question.php">
<input type="text" name="text-value">
<p id="except"></p>
<input type="submit" value="전송">
</form>
</body>
</html>
[php code]
<?php
$_getVal = $_POST['text-value'];
$_getVal2 = (int)$_getVal;
echo gettype($_getVal2);
if ($_getVal2 < 10){
echo "<script>location.href='https://www.example.com/question.html';</script>";
echo "<script>document.getElementById('except').innerText = 'low';</script>";
} else {
echo "<script>location.href='https://www.example.com/question.html';</script>";
echo "<script>document.getElementById('except').innerText = 'high';</script>";
}
?>
That id value exists in the p tag in the HTML source.
F12 inspection confirmed that there were no errors. The expected problem is that the page refresh due to location.
href="" is not getting the php echo value, or the php source itself is an error.
If you enter the int value with the above source, and then press trans, you want to get high if it is greater than 10 and low if it is smaller.
Please confirm which part is the problem.
2
Answers
It appears that there is a missing closing single quote in your PHP code, and there’s a missing } to close the if statement. Also, you should make sure to properly escape the HTML output to avoid potential issues. Here’s the corrected PHP code:
I added a single quote (‘) after ‘low’ and ‘high’, and I added a semicolon (;) at the end of each line within the echo statements to properly terminate the JavaScript statements.
Additionally, in your HTML, you should add a closing curly brace (}) to close the window.onload function:
With these corrections, your PHP script should correctly output the JavaScript code to set the content of the element with the ID ‘except’ based on the value received from the form. Just make sure that your server supports PHP, and that the PHP script is properly executed when the form is submitted.
php changes
javascript statements also require a colon ; to end properly. A apostrophe ‘ symbol was also missing after the text high
javascript changes
onload command was not closed properly. Added the closing curly bracket.
These changes should work. If it doesn’t works please feel free to comment down any further errors.