admin管理员组

文章数量:1026165

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
  • You can check the value of the checkbox using the on-click event of the link. – KIKO Software Commented Nov 17, 2024 at 17:55
  • 2 You'll need javascript for this. Have you researched or attempted anything yet? There's no question or problem statement in your post. See also How to Ask – ADyson Commented Nov 17, 2024 at 18:02
  • 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
  • Are you sure this problem is related to get in any way? – Nico Haase Commented Nov 18, 2024 at 9:32
Add a comment  | 

1 Answer 1

Reset to default 0

Firstly, 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>

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
  • You can check the value of the checkbox using the on-click event of the link. – KIKO Software Commented Nov 17, 2024 at 17:55
  • 2 You'll need javascript for this. Have you researched or attempted anything yet? There's no question or problem statement in your post. See also How to Ask – ADyson Commented Nov 17, 2024 at 18:02
  • 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
  • Are you sure this problem is related to get in any way? – Nico Haase Commented Nov 18, 2024 at 9:32
Add a comment  | 

1 Answer 1

Reset to default 0

Firstly, 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>

本文标签: getPHPValidate checkbox while canceling link redirectStack Overflow