Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with simple confirmation box from PHP

Status
Not open for further replies.

awingnut

Programmer
Feb 24, 2003
759
US
I have a PHP script that will take an action that I want the user to confirm. I am just starting with Javascript because the PHP experts said this is the best way to do it. I really had no need to learn Javascript previous. I've read some examples of confirmation boxes and I think I understand how the examples worked but none clearly help me with my problem. I'll try to explain what I need and hope someone can confirm my theories and/or make suggestions.

I have a form on which the user checks sets of radio buttons then clicks a submit button (client side). The PHP script then processes the radio buttons and takes appropriate actions (server side). If a certain radio button is set, I need to put up a confirmation box. Since Javascript is a client side function it is not clear to me how to accomplish this. Is the technique to put a Javascript 'confirm' on that radio button (how?) then just assume it was confirmed when the posted data is returned? How do I unset the radio button (or set a different one) if the confirmation is negative?

Thanks.
 
Code:
<script language=&quot;javascript&quot;>

function submitmyform(theForm) {
  if (theForm.elements['check2'].checked) {
    if (!confirm(&quot;Are you sure you want to check box #2?&quot;)) return false;
  }
  theForm.submit();
}

</script>

<form name=&quot;myform&quot; method=&quot;POST&quot; action=&quot;myprocess.php&quot;>

<input type=&quot;checkbox&quot; name=&quot;check1&quot; value=&quot;1&quot;>Check 1<br>
<input type=&quot;checkbox&quot; name=&quot;check2&quot; value=&quot;2&quot;>Check 2<br>
<input type=&quot;checkbox&quot; name=&quot;check3&quot; value=&quot;3&quot;>Check 3<br>

<input type=&quot;button&quot; value=&quot;Submit&quot; onClick=&quot;submitmyform(this.form);&quot;>

</form>

You're right, this IS a client-side problem. You simply need to have a function which checks the state of all your input elements, like the checkboxes or whatever, and if the one you want to confirm IS checked, ask the user using the confirm() javascript function. It will display &quot;OK&quot; and &quot;CANCEL&quot; in it, and will return true if they click OK and false if they click CANCEL.

If they return false by clicking CANCEL, then the function exits with &quot;return false;&quot; and does nothing else. If OK is clicked, true is returned, the function goes ahead and submit()'s the form.
 
Thanks for the reply but I'm not sure I understand. Suppose I have 3 sets of 3 radio buttons. If a certain button is on in each set I need confirmation (that means 0-3 separate confirmations). If I issue the 'confirm' on 'submit' how do I get all 3 confirmations? Shouldn't the 'confirm' somehow occur on each of those radio buttons when they are turned on instead? Then if so, a negative reply needs to reset that particular set of radio buttons, does it not? When the submit is clicked shouldn't everything already be set up so the POSTed data is correct?
Code:
<INPUT type=&quot;radio&quot; name=&quot;disposition1&quot; value=&quot;Accept&quot;>
<INPUT type=&quot;radio&quot; name=&quot;disposition1&quot; value=&quot;Reject>
<INPUT type=&quot;radio&quot; name=&quot;disposition1&quot; value=&quot;Defer&quot;>
<INPUT type=&quot;radio&quot; name=&quot;disposition2&quot; value=&quot;Accept&quot;>
<INPUT type=&quot;radio&quot; name=&quot;disposition2&quot; value=&quot;Reject>
<INPUT type=&quot;radio&quot; name=&quot;disposition2&quot; value=&quot;Defer&quot;>
<INPUT type=&quot;radio&quot; name=&quot;disposition3&quot; value=&quot;Accept&quot;>
<INPUT type=&quot;radio&quot; name=&quot;disposition3&quot; value=&quot;Reject>
<INPUT type=&quot;radio&quot; name=&quot;disposition3&quot; value=&quot;Defer&quot;>
If any &quot;Reject&quot; radio button is on I need to confirm it and either leave it (positive) or reset to &quot;Defer&quot; (negative).
 
no, you would put more &quot;confirmation&quot; IF statements into your confirmation/submit function.

Say you had 9 checkboxes, in 3 sets, and wanted to confirm box 2, box 6 and box 7 (if either or all them was checked):

Code:
<script language=&quot;javascript&quot;>

function submitmyform(theForm) {
  if (theForm.elements['check2'].checked) {
    if (!confirm(&quot;Are you sure you want to check box #2?&quot;)) return false;
  }
  if (theForm.elements['check6'].checked) {
    if (!confirm(&quot;Are you sure you want to check box #6?&quot;)) return false;
  }
  if (theForm.elements['check7'].checked) {
    if (!confirm(&quot;Are you sure you want to check box #7?&quot;)) return false;
  }
  theForm.submit();
}

</script>

<form name=&quot;myform&quot; method=&quot;POST&quot; action=&quot;myprocess.php&quot;>

<input type=&quot;checkbox&quot; name=&quot;check1&quot; value=&quot;1&quot;>Check 1<br>
<input type=&quot;checkbox&quot; name=&quot;check2&quot; value=&quot;2&quot;>Check 2<br>
<input type=&quot;checkbox&quot; name=&quot;check3&quot; value=&quot;3&quot;>Check 3<br>
<br>
<input type=&quot;checkbox&quot; name=&quot;check4&quot; value=&quot;1&quot;>Check 4<br>
<input type=&quot;checkbox&quot; name=&quot;check5&quot; value=&quot;2&quot;>Check 5<br>
<input type=&quot;checkbox&quot; name=&quot;check6&quot; value=&quot;3&quot;>Check 6<br>
<br>
<input type=&quot;checkbox&quot; name=&quot;check7&quot; value=&quot;1&quot;>Check 7<br>
<input type=&quot;checkbox&quot; name=&quot;check8&quot; value=&quot;2&quot;>Check 8<br>
<input type=&quot;checkbox&quot; name=&quot;check9&quot; value=&quot;3&quot;>Check 9<br>

<input type=&quot;button&quot; value=&quot;Submit&quot; onClick=&quot;submitmyform(this.form);&quot;>

</form>

The concept used in that confirmation function is that you check ALL conditions that are appropriate, in your specific case, each one of the boxes that needs a confirmation on it, and if all those pass (meaning the person clicked &quot;OK&quot;) THEN you &quot;submit&quot; the form. If they click &quot;CANCEL&quot; in either of the 3 popups, then the confirm(&quot;...&quot;) popup box returns false, and your if statement fails, and the function exits immediately with &quot;return false;&quot;.
 
sorry - I forgot to notice you were talking about radio buttons. That changes the javascript syntax a bit, BUT, the concept of checking all conditions in cascading IF statements remains the same. In your example, the function would look like:

Code:
function submitmyform(theForm) {
  if (theForm.elements['disposition1'][1].checked) {
    if (!confirm(&quot;Are you sure you want to REJECT disposition 1?&quot;)) return false;
  }
  if (theForm.elements['disposition2'][1].checked) {
    if (!confirm(&quot;Are you sure you want to REJECT disposition 2?&quot;)) return false;
  }
  if (theForm.elements['disposition3'][1].checked) {
    if (!confirm(&quot;Are you sure you want to REJECT disposition 3?&quot;)) return false;
  }
  theForm.submit();
}

notice how i reference the individual reject radio button, because in javascript/html, groups of radio buttons with the same name are put into an array... so i reference the element (group) by name with &quot;elements['disposition1']&quot; and then the individual member of the group array (which is zero-based... meaning the indexes go 0,1,2,etc) with a following &quot;[1]&quot; which refers to the second of the 3 elements in each group, which is the order you gave it to me in your response.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top