I have 2 seperate parts on my webpage: a checkbox and an < a > tag that links to another page. Both of them cannot be in a single < form > tag. If a user clicks on the link, I don't want the user to be redirected unless they tick on the checkbox first.
Here is the first part:
<form method="GET">
<input type="checkbox" name="value1" />
<label>Check the box</label>
</form>
$value1 = $_GET['value1'];
// Code to see if checkbox is checked
if(isset($value1)) {
// if checked, work as normal
} else {
// if not checked
// This allows redirect to original page, which should cancel out < a > tag's href
header(Refresh:0);
// display error message / popup box
echo '<script>window.alert("You need to click the checkbox to proceed")</script>;
}
If a user clicks on the < a > tag, it should not redirect unless the checkbox is checked:
< a href="someurl" />
I have 2 seperate parts on my webpage: a checkbox and an < a > tag that links to another page. Both of them cannot be in a single < form > tag. If a user clicks on the link, I don't want the user to be redirected unless they tick on the checkbox first.
Here is the first part:
<form method="GET">
<input type="checkbox" name="value1" />
<label>Check the box</label>
</form>
$value1 = $_GET['value1'];
// Code to see if checkbox is checked
if(isset($value1)) {
// if checked, work as normal
} else {
// if not checked
// This allows redirect to original page, which should cancel out < a > tag's href
header(Refresh:0);
// display error message / popup box
echo '<script>window.alert("You need to click the checkbox to proceed")</script>;
}
If a user clicks on the < a > tag, it should not redirect unless the checkbox is checked:
< a href="someurl" />
Share
Improve this question
edited Nov 17, 2024 at 17:34
JohnLyons
asked Nov 17, 2024 at 17:27
JohnLyonsJohnLyons
331 silver badge4 bronze badges
4
|
1 Answer
Reset to default 0Firstly, Php is a server side language. To handle what you have described in the question, you might want to handle that at client-side i.e before any backend processing in done. For instance, javascript would be a great example in this case. you can add javascript:void(0) to your a tag to prevent redirecting and then check if the checkbox is ticked in a javascript function before handling the redirect in the same function. This function can then be attached to an onclick event tied to the "a" tag. Your a element would then look like as shown below, where someFunction would be the function you've defined within javascript.
<a href="javascript:void(0)" onclick="someFunction()">some text</a>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745629524a4637018.html
Both of them cannot be in a single < form > tag
...why? This seems like a somewhat arbitrary restriction – ADyson Commented Nov 17, 2024 at 19:38