confused and dazed Posted November 18, 2011 Share Posted November 18, 2011 Hello internet.Here is my problem...I have a ton of "if...else if" statments in a javascript function. Once all those requirements have been met I would like to send the user a popup box to confirm if they want to submit the form. IT IS NOT WORKING FOR ME.I am trying to incorperate the following style of pop-up but it is not working. My function either skips right over this and sends the form to my PHP file for processing without this final check or nothing works at all. PLEASE HELP. function show_confirm(){var r=confirm("Press a button");if (r==true) { alert("You pressed OK!"); }else { alert("You pressed Cancel!"); }} Link to comment Share on other sites More sharing options...
Ingolme Posted November 18, 2011 Share Posted November 18, 2011 I don't see how the code you're showing related to a PHP form. Where's the place that the function is called from? Link to comment Share on other sites More sharing options...
thescientist Posted November 18, 2011 Share Posted November 18, 2011 Please provide all the code you are using. The code you provided doesn't show us how you are using it within your form handling. Link to comment Share on other sites More sharing options...
confused and dazed Posted November 18, 2011 Author Share Posted November 18, 2011 I have a form called Tourney<form name=Tourney action="m m_post_process_4.php" method="post" onSubmit="return check()" target="_blank"> The php filem_m_post_process_4.php just simply echos all the button values back for the user to see the check() function has a bunch of if...else if statementshere is a bit of that code... else if (quadAA==1) { alert("You will need to fix Quad A") return false; }else if (quadBB==1) { alert("You will need to fix Quad B") return false;// etc..........and the else ifs go on and on and on... once the value is returned true I would like to send the user a final yes/no pop up box to submit the form to the PHP file. Link to comment Share on other sites More sharing options...
ShadowMage Posted November 18, 2011 Share Posted November 18, 2011 Instead of returning a value on each condition set a variable. At the very beginning of the check function create a variable and initialize it to true: var blnSubmit = true; Then at the bottom of all your if statements, you have something like this: if (blnSubmit) { var result = confirm("Are you sure?"); if (!result) { blnSubmit = false; }}return blnSubmit EDIT: You might also consider adding all your error messages to a single string to alert at the end of the function. It would be more user friendly. Instead of:alert("...");something like:errMsg += "..."; You'd have to initialize it to an empty string at the beginning of the function like you do with the blnSubmit variable.Then you could add an else clause to the if statement I provided above where you would alert the message. Link to comment Share on other sites More sharing options...
confused and dazed Posted November 18, 2011 Author Share Posted November 18, 2011 That worked. AWSOME! thanks - one more question... when you said Instead of returning a value on each condition set a variable. At the very beginning of the check function create a variable and initialize it to true:I know you menat setting a variable for the final results but did you mean I could condense some of my code and not have to say return false; time after time after time...? Link to comment Share on other sites More sharing options...
ShadowMage Posted November 18, 2011 Share Posted November 18, 2011 Instead of returning a value on each condition set a variable. At the very beginning of the check function create a variable and initialize it to true:I know you menat setting a variable for the final results but did you mean I could condense some of my code and not have to say return false; time after time after time...?My original suggestion does not really condense anything. Instead of writing return false; you'd be writing blnSubmit = false; instead.You could however, initialize the variable to false and add an else clause to your if structure which would set blnSubmit to true, thus eliminating the need to write blnSubmit = false; time and time again:var blnSubmit = false;if (condition1) { ...} else if (condition2) { ...} else if (condition3) { ...} else { blnSubmit = true;} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.